pressing a key now prints all correlated bindings

This commit is contained in:
nova
2025-07-06 03:36:43 +02:00
parent 142f75d98a
commit 36fd160ccb
3 changed files with 119 additions and 57 deletions

View File

@ -31,47 +31,143 @@ extern char *btm_buffer;
extern unsigned long mid_file_count; extern unsigned long mid_file_count;
extern unsigned int status; extern unsigned int status;
int read_string(WINDOW *win, int y, int x, char *str);
unsigned int timeout_time = 0; unsigned int timeout_time = 0;
extern char *input; extern char *input;
unsigned int input_pass; unsigned int input_pass;
int parsed_input_number; int parsed_input_number;
extern char *terminal_width_empty_line;
int read_string(WINDOW *win, int y, int x, char *str);
int strcmp_offset(char *in0, char *in1, char offset);
void user_interactions() { void user_interactions() {
char ch; char ch;
unsigned long i;
unsigned long binding_matches = 0;
static char binding_pass = 0;
free(terminal_width_empty_line);
terminal_width_empty_line = malloc(terminal_width);
memset(terminal_width_empty_line, ' ', terminal_width);
move(terminal_height, terminal_width); move(terminal_height, terminal_width);
ch = getch(); ch = getch();
if(ch != ERR) { if(ch != ERR) {
timeout(10); /* blocking timeout of getch() */ timeout(1); /* blocking timeout of getch() */
mvaddch(terminal_height-1, (terminal_width/3)*2 +input_pass, ch);
input[input_pass] = ch; input[input_pass] = ch;
mvaddstr(terminal_height-1, (terminal_width/3)*2, input);
input_pass++; input_pass++;
} if (ch == 27) { /* esc key */
void (*func_ptr)(int);
unsigned long i = 0;
unsigned int j = 0;
for (i = 0; i < binding_count; i++) {
if (strstr(input, key_binding[i].key)) {
parsed_input_number = 0;
while((*input >= '0') && (*input <= '9')) {
parsed_input_number = (parsed_input_number * 10) + (*input - '0');
input++;
}
func_ptr = key_binding[i].func;
func_ptr(parsed_input_number);
memset(input, 0, 255); memset(input, 0, 255);
input_pass = 0; input_pass = 0;
timeout(100); /* blocking timeout of getch() */ timeout(100); /* blocking timeout of getch() */
status |= STATUS_UPDATE_SCREEN_0;
}
binding_pass = 0;
status |= STATUS_UPDATE_SCREEN_0;
}
void (*func_ptr)(int);
int number_length = 0;
if (!binding_pass) {
parsed_input_number = 0;
while((*input >= '0') && (*input <= '9')) {
parsed_input_number = (parsed_input_number * 10) + (*input - '0');
input++;
number_length++;
mvaddch(terminal_height-5, 5, number_length+48);
}
input -= number_length;
binding_pass = 1;
char cmp_len = strlen(input);
if(strlen(input) < 1) {
cmp_len++;
}
for (i = 0; i < binding_count; i++) {
if (strcmp(input + number_length, key_binding[i].key) == 0) {
func_ptr = key_binding[i].func;
func_ptr(parsed_input_number);
memset(input, 0, 255);
input_pass = 0;
binding_pass = 0;
number_length = 0;
timeout(100); /* blocking timeout of getch() */
} else if (strncmp(input+number_length, key_binding[i].key, cmp_len) == 0) {
binding_matches++;
attron(A_UNDERLINE);
mvaddstr(terminal_height-binding_matches-2, 0, terminal_width_empty_line);
mvaddstr(terminal_height-binding_matches-2, 0, "input");
mvaddstr(terminal_height-binding_matches-2, sizeof("input"), "\t\t\t");
mvaddstr(terminal_height-binding_matches-2, sizeof("input") + sizeof("\t\t\t"), "command");
attroff(A_UNDERLINE);
mvaddstr(terminal_height-binding_matches-1, 0, terminal_width_empty_line);
mvaddstr(terminal_height-binding_matches-1, 0, key_binding[i].key);
mvaddstr(terminal_height-binding_matches-1, 0, key_binding[i].key);
mvaddstr(terminal_height-binding_matches-1, sizeof(key_binding[i].key), "\t");
mvaddstr(terminal_height-binding_matches-1, sizeof(key_binding[i].key) + sizeof("\t"), key_binding[i].comment);
}
} }
} }
} }
int read_string(WINDOW *win, int y, int x, char *str){
curs_set(1);
timeout(-1); /* negative numbers block until enter is pressed */
unsigned int pass = 0;
char ch;
char err = 0;
wmove(win, y, x);
while(1) {
/*ch = mvwgetch(win, y, x + pass);*/
ch = wgetch(win);
if (ch == '\n') {
err = 0;
break;
} else if (ch == 27) { /* esc key */
err = 1;
break;
} else if (ch == 127) { /* backspace */
if (pass > 0) {
pass--;
mvwdelch(win, y, pass);
}
} else {
mvwaddch(win, y, x +pass, ch);
str[pass] = ch;
pass++;
}
}
str[pass] = '\0';
timeout(10);
curs_set(0);
return err;
}
int strcmp_offset(char *in0, char *in1, char offset){
int i = 0;
while (in0[i] != '\0' && in1[i] != '\0') {
if (in0[i+offset] != in1[i]) {
return 1;
}
i++;
in1++;
}
return 0;
}
void quit_program(){ void quit_program(){
status = STATUS_QUIT_PROGRAM; status = STATUS_QUIT_PROGRAM;
} }
@ -162,43 +258,6 @@ void jump_top(){
pthread_mutex_unlock(&mutex_selection); pthread_mutex_unlock(&mutex_selection);
} }
int read_string(WINDOW *win, int y, int x, char *str){
curs_set(1);
timeout(-1); /* negative numbers block until enter is pressed */
unsigned int pass = 0;
char ch;
char err = 0;
wmove(win, y, x);
while(1) {
/*ch = mvwgetch(win, y, x + pass);*/
ch = wgetch(win);
if (ch == '\n') {
err = 0;
break;
} else if (ch == 27) { /* esc key */
err = 1;
break;
} else if (ch == 127) { /* backspace */
if (pass > 0) {
pass--;
mvwdelch(win, y, pass);
}
} else {
mvwaddch(win, y, x +pass, ch);
str[pass] = ch;
pass++;
}
}
str[pass] = '\0';
timeout(10);
curs_set(0);
return err;
}
void open_with(){ void open_with(){
btm_buffer = concat("open \"", file_current->file_name); btm_buffer = concat("open \"", file_current->file_name);
btm_buffer = concat(btm_buffer, "\" with:"); btm_buffer = concat(btm_buffer, "\" with:");

2
main.c
View File

@ -25,6 +25,7 @@ WINDOW *win_m;
WINDOW *win_r; WINDOW *win_r;
char *input; /*used in user_interactions*/ char *input; /*used in user_interactions*/
char *terminal_width_empty_line; /* used in user_interactions */
void render_pass(); void render_pass();
void init(); void init();
@ -53,6 +54,7 @@ int main(){
pthread_t thread_r; pthread_t thread_r;
char threading = 0; char threading = 0;
terminal_width_empty_line = malloc(terminal_width);
while(!(status & STATUS_QUIT_PROGRAM)){ while(!(status & STATUS_QUIT_PROGRAM)){

View File

@ -58,6 +58,7 @@ void window_btm(WINDOW *win){
mvwprintw(win, 0, 0, "%s", btm_buffer); mvwprintw(win, 0, 0, "%s", btm_buffer);
pthread_mutex_unlock(&mutex_btm); pthread_mutex_unlock(&mutex_btm);
/*the printing of the input char is done in user_interactions*/ /*the printing of the input char is done in user_interactions*/
/*the printing of all possible inputs are done in user_interactions */
} }
} }