improvenments to the string input loop

This commit is contained in:
nova
2025-06-22 02:06:36 +02:00
parent 3eb1c0f93e
commit c29870998e

View File

@ -26,7 +26,7 @@ extern char *btm_buffer;
extern unsigned int status; extern unsigned int status;
void open_with_pass_2(); void open_with_pass_2();
void rename_hovered_pass_2(); void rename_hovered_pass_2();
int read_string(char *str); int read_string(WINDOW *win, int y, int x, char *str);
void user_interactions(char *input, WINDOW *win_b) { void user_interactions(char *input, WINDOW *win_b) {
void (*func_ptr)(); void (*func_ptr)();
@ -112,8 +112,8 @@ void jump_top(){
pthread_mutex_unlock(&mutex_selection); pthread_mutex_unlock(&mutex_selection);
} }
int read_string(char *str){ int read_string(WINDOW *win, int y, int x, char *str){
echo(); noecho();
curs_set(1); curs_set(1);
timeout(-1); /* negative numbers block until enter is pressed */ timeout(-1); /* negative numbers block until enter is pressed */
@ -121,17 +121,27 @@ int read_string(char *str){
unsigned int pass = 0; unsigned int pass = 0;
char ch; char ch;
char err = 0; char err = 0;
wmove(win, y, x);
while(1) { while(1) {
ch = getch(); /*ch = mvwgetch(win, y, x + pass);*/
ch = wgetch(win);
if (ch == '\n') { if (ch == '\n') {
err = 0; err = 0;
break; break;
} else if (ch == 27) { /* esc key */ } else if (ch == 27) { /* esc key */
err = 1; err = 1;
break; break;
} } else if (ch == 127) { /* backspace */
str[pass] = ch; if (pass > 0) {
pass++; pass--;
mvwdelch(win, y, pass);
}
} else {
mvwaddch(win, y, x +pass, ch);
str[pass] = ch;
pass++;
}
} }
str[pass] = '\0'; str[pass] = '\0';
@ -157,7 +167,7 @@ void open_with_pass_2(WINDOW *win_b){
/* TODO(2025-06-22T01:24:36) fix fixed buffer size */ /* TODO(2025-06-22T01:24:36) fix fixed buffer size */
char *str = malloc(255); char *str = malloc(255);
memset(str, ' ', 255); memset(str, ' ', 255);
int err = read_string(str); int err = read_string(win_b, local_height - 1, 0 , str);
if (!err) { if (!err) {
@ -178,7 +188,7 @@ void open_with_pass_2(WINDOW *win_b){
free(str); free(str);
} }
void rename_hovered(WINDOW *win_b){ void rename_hovered(){
btm_buffer = concat("rename \"", file_current.file_name); btm_buffer = concat("rename \"", file_current.file_name);
btm_buffer = concat(btm_buffer, "\" to:"); btm_buffer = concat(btm_buffer, "\" to:");
@ -195,7 +205,7 @@ void rename_hovered_pass_2(WINDOW *win_b){
/* TODO(2025-06-22T01:24:30) fix fixed buffer size */ /* TODO(2025-06-22T01:24:30) fix fixed buffer size */
char *str = malloc(255); char *str = malloc(255);
memset(str, ' ', 255); memset(str, ' ', 255);
int err = read_string(str); int err = read_string(win_b, local_height - 1, 0, str);
if (!err) { if (!err) {