1
0
mirror of https://gittea.dev/nova/th.git synced 2025-10-21 18:30:15 -04:00
Files
th/interactions.c
2025-09-28 10:45:33 +02:00

853 lines
23 KiB
C

#include <curses.h>
#include <pthread.h>
#include <dirent.h>
#include <string.h>
#include "file_previews.h"
#include "backend.h"
#include "defines.h"
#include "config.h"
#include "dir.h"
extern volatile unsigned long selected_file_current;
extern volatile unsigned long selected_file_last;
extern unsigned int file_modifiers;
extern pthread_mutex_t mutex_selection;
extern pthread_mutex_t mutex_rgt;
extern pthread_mutex_t mutex_mid;
extern pthread_cond_t cond_rgt;
extern file *mid_content;
extern file *lft_content;
extern file *rgt_content;
extern unsigned int terminal_height;
extern unsigned int terminal_width;
extern WINDOW *win_b;
extern char *rgt_buffer;
extern char *btm_buffer;
extern unsigned long mid_file_count;
extern unsigned int status;
extern char *start_path;
extern char *input;
extern time_t seed;
char search_buffer[255];
unsigned int timeout_time = 0;
unsigned int input_pass;
int parsed_input_number;
yank yank_files = { 0 };
int read_string(WINDOW *win, int y, int x, char *str);
int strcmp_offset(char *in0, char *in1, char offset);
extern void render_pass();
extern int (*order_func)();
void FAIL(char *function, char *str){
noraw();
endwin();
curs_set(1);
echo();
printf("ERROR in function %s: %s", function, str);
}
void user_interactions() {
char ch;
unsigned long i;
unsigned long binding_matches = 0;
static char binding_pass = 0;
ch = getch();
if(ch != ERR) {
timeout(1); /* blocking timeout of getch() */
input[input_pass] = ch;
mvaddstr(terminal_height-1, (terminal_width/3)*2, input);
input_pass++;
if (ch == 27) { /* esc key */
memset(input, ' ', terminal_width);
mvaddstr(terminal_height-1, (terminal_width/3)*2, input);
memset(input, 0, 255);
input_pass = 0;
timeout(100); /* blocking timeout of getch() */
}
binding_pass = 0;
status |= STATUS_UPDATE_SCREEN_0;
}
void (*func_ptr)(int, int);
unsigned long 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++;
}
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, i);
} else if (strncmp(input + number_length, key_binding[i].key, cmp_len) == 0) {
binding_matches++;
mvwprintw(stdscr, terminal_height-binding_matches-1, 0, "\t\t\t");
mvwprintw(stdscr, terminal_height-binding_matches-1, 0, "%s\t%s", key_binding[i].key, key_binding[i].comment);
status |= STATUS_INPUT_MATCH;
}
}
if (status & STATUS_INPUT_MATCH) {
attron(A_UNDERLINE);
mvaddstr(terminal_height-binding_matches-2, 0, "input\tcommand\t\t");
attroff(A_UNDERLINE);
status &= ~STATUS_INPUT_MATCH;
} else if (number_length != strlen(input)) {
memset(input, 0, 255);
input_pass = 0;
binding_pass = 0;
number_length = 0;
timeout(100); /* blocking timeout of getch() */
}
}
}
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 == '\t') { /* tab */
memcpy(str + pass, mid_content[selected_file_current].file_name, strlen(mid_content[selected_file_current].file_name));
mvwaddstr(win, y, x +pass, mid_content[selected_file_current].file_name);
pass += strlen(mid_content[selected_file_current].file_name);
} else if (ch == 127) { /* backspace */
if (pass > 0) {
pass--;
mvwdelch(win, y, pass);
}
} else if (ch == 27) { /* esc key */
err = 1;
break;
} 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(){
status = STATUS_QUIT_PROGRAM;
}
void toggle_selection(){
pthread_mutex_lock(&mutex_selection);
pthread_mutex_lock(&mutex_mid);
mid_content[selected_file_current].status ^= FILE_STATUS_SELECTED;
status |= (STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_mid);
pthread_mutex_unlock(&mutex_selection);
}
void move_down(int passes){
pthread_mutex_lock(&mutex_selection);
if (passes == 0) {
passes++;
}
selected_file_current += passes;
update_selected_file();
dir_set_selected_file_current(selected_file_current);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0);
pthread_mutex_unlock(&mutex_selection);
}
void move_up(int passes){
pthread_mutex_lock(&mutex_selection);
if (passes == 0) {
passes++;
}
unsigned long tmp = selected_file_current;
selected_file_current -= passes;
if (tmp < selected_file_current) {
selected_file_current = 0;
}
update_selected_file();
dir_set_selected_file_current(selected_file_current);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_selection);
}
void move_left(int passes){
if (passes == 0) {
passes++;
}
int i;
for (i = 0; i < passes; i++) {
if (chdir("..") != 0) {
/* TODO(2025-07-09T00:30:05) fix */
FAIL("move_left", "unhandled error of chdir");
} else {
selected_file_current = dir_get_selected_file_current();
}
}
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
}
void move_right(){
if (mid_content->file_name[0] == '\0') {
return;
}
if (mid_content[selected_file_current].file_type == FILE_TYPE_DIR || mid_content[selected_file_current].file_type == FILE_TYPE_SYMLINK) {
if (chdir(mid_content[selected_file_current].file_name) != 0) {
FAIL("move_right", "unhandled error of chdir");
} else {
selected_file_current = dir_get_selected_file_current();
}
} else {
unsigned long i = 0;
char match = 0;
char *mime = get_mimetype(mid_content[selected_file_current].file_name);
char *extension = strrchr(mid_content[selected_file_current].file_name, '.');
for (i = 0; i < file_extension_default_count; i++) {
if (strstr(extension, file_extension_default_cmd[i].file_extension)) {
char *cmd = concat(file_extension_default_cmd[i].command, " ./\"");
cmd = concat(cmd, mid_content[selected_file_current].file_name);
cmd = concat(cmd, "\"");
if (system(cmd) == -1) {
/*do nothing*/
}
curs_set(1); /*for some reason, 1 here turns it invisible once again */
match = 1;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
break;
}
}
if (match == 0) {
for (i = 0; i < mimetype_default_count; i++) {
if (strstr(mime, mimetype_default_cmd[i].mimetype)) {
char *cmd = concat(mimetype_default_cmd[i].command, " ./\"");
cmd = concat(cmd, mid_content[selected_file_current].file_name);
cmd = concat(cmd, "\"");
btm_buffer = malloc(strlen(cmd));
strcpy(btm_buffer, cmd-1);
if (system(cmd) == -1) {
/*do nothing*/
}
curs_set(1); /*for some reason, 1 here turns it invisible once again */
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
break;
}
}
}
free(mime);
}
update_selected_file();
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
}
void toggle_hidden_files(){
file_modifiers ^= FILE_MODIFIERS_HIDDEN_FILES;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
}
void jump_bottom(){
pthread_mutex_lock(&mutex_selection);
selected_file_current = 0 - 1;
update_selected_file();
dir_set_selected_file_current(selected_file_current);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_selection);
}
void jump_top(){
pthread_mutex_lock(&mutex_selection);
selected_file_current = 0;
update_selected_file();
dir_set_selected_file_current(selected_file_current);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_selection);
}
void open_with(){
btm_buffer = concat("open \"", mid_content[selected_file_current].file_name);
btm_buffer = concat(btm_buffer, "\" with:");
status |= STATUS_UPDATE_SCREEN_0;
werase(win_b);
mvwin(win_b, terminal_height-6, 0);
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
render_pass();
unsigned long local_height;
local_height = getmaxy(win_b);
/* TODO(2025-06-22T01:24:36) fix fixed buffer size */
char *str = malloc(255);
memset(str, ' ', 255);
int err = read_string(win_b, local_height - 1, 0 , str);
if (!err) {
char *cmd = concat(str, " ./\"");
cmd = concat(cmd, mid_content[selected_file_current].file_name);
cmd = concat(cmd, "\"");
if (system(cmd) == -1) {
FAIL("open_with", "creating subcommand failed unhandled");
}
free(btm_buffer);
btm_buffer = cmd;
}
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
free(str);
}
void rename_hovered(){
btm_buffer = concat("rename \"", mid_content[selected_file_current].file_name);
btm_buffer = concat(btm_buffer, "\" to:");
status |= STATUS_UPDATE_SCREEN_0;
werase(win_b);
mvwin(win_b, terminal_height-6, 0);
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
render_pass();
unsigned long local_height;
local_height = getmaxy(win_b);
/* TODO(2025-06-22T01:24:30) fix fixed buffer size */
char *str = malloc(255);
memset(str, ' ', 255);
int err = read_string(win_b, local_height - 1, 0, str);
if (!err) {
char *cmd = concat("mv ./\"", mid_content[selected_file_current].file_name);
cmd = concat(cmd, "\" ./\"");
cmd = concat(cmd, str);
cmd = concat(cmd, "\"");
if (system(cmd) == -1) {
FAIL("rename_hovered", "mv or creating subcommand failed");
};
btm_buffer = cmd;
}
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
free(str);
}
void delete(){
unsigned int i = 0;
unsigned int hits = 0;
char *file_str = " ";
for (i = 0; i < mid_file_count; i++) {
if (mid_content[i].status & FILE_STATUS_SELECTED) {
file_str = concat(file_str, "\"");
file_str = concat(file_str, mid_content[i].file_name);
file_str = concat(file_str, "\" ");
hits++;
}
}
if (hits) {
btm_buffer = concat("delete:", file_str);
} else {
btm_buffer = concat("delete: \"", mid_content[selected_file_current].file_name);
btm_buffer = concat(btm_buffer, "\"");
}
btm_buffer = concat(btm_buffer, "?");
btm_buffer = concat(btm_buffer, "\n\n");
btm_buffer = concat(btm_buffer, "(y/N)");
status |= STATUS_UPDATE_SCREEN_0;
werase(win_b);
mvwin(win_b, terminal_height-6, 0);
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
render_pass();
timeout(-1); /* negative numbers block until enter is pressed */
/* TODO(2025-06-22T01:24:30) fix fixed buffer size */
char ch = wgetch(win_b);
if (ch == 'y' || ch == 'Y') {
/* TODO(2025-06-30T02:27:06) IMPORTANT: this really fucks up when the file has a quotation mark in its name */
if (hits) {
for (i = 0; i < mid_file_count; i++) {
if (mid_content[i].status & FILE_STATUS_SELECTED) {
recursive_delete(mid_content[i]);
}
}
free(btm_buffer);
btm_buffer = concat("deleted: ", file_str);
} else {
free(btm_buffer);
if (mid_content[selected_file_current].file_type & (FILE_TYPE_SYMLINK | FILE_TYPE_DIR)) {
recursive_delete(mid_content[selected_file_current]);
}
remove(mid_content[selected_file_current].file_name);
btm_buffer = concat("deleted: \"", mid_content[selected_file_current].file_name);
btm_buffer = concat(btm_buffer, "\"");
}
/*system(cmd);*/
} else {
free(btm_buffer);
btm_buffer = "cancled deletion";
}
timeout(10);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
if (hits) {
free(file_str);
}
}
void makedir(){
btm_buffer = "create dir: ";
status |= STATUS_UPDATE_SCREEN_0;
werase(win_b);
mvwin(win_b, terminal_height-6, 0);
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
render_pass();
unsigned long local_height;
local_height = getmaxy(win_b);
/* TODO(2025-07-03T01:19:55) fix fixed buffer size */
char *str = malloc(255);
memset(str, ' ', 255);
int err = read_string(win_b, local_height - 1, 0, str);
if (!err) {
btm_buffer = concat(btm_buffer, str);
mode_t mask = umask(0);
mkdir(str, 0755); /*magic number from default permissions as created by mkdir*/
umask(mask);
}
free(str);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
}
void makefile(){
btm_buffer = "create file: ";
status |= STATUS_UPDATE_SCREEN_0;
werase(win_b);
mvwin(win_b, terminal_height-6, 0);
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
render_pass();
unsigned long local_height;
local_height = getmaxy(win_b);
/* TODO(2025-07-03T01:19:49) fix fixed buffer size */
char *str = malloc(255);
memset(str, ' ', 255);
int err = read_string(win_b, local_height - 1, 0, str);
if (!err) {
btm_buffer = concat(btm_buffer, str);
FILE *fp;
fp = fopen(str, "w");
fclose(fp);
}
free(str);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
}
void update(){
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
}
void enter_shell(int passes, int index){
(void)passes;
endwin();
if (system(key_binding[index].black_magic) != 0) {
/*do nothing*/
}
initscr();
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
}
void not_implemented(int passes, int index){
(void)passes;
mvaddstr(terminal_height-1, 0, key_binding[index].comment);
mvaddstr(terminal_height-1, strlen(key_binding[index].comment), "\t");
mvaddstr(terminal_height-1, strlen(key_binding[index].comment) + strlen("\t"), "is not yet implemented");
}
void jump_to_dir(int passes, int index){
(void)passes;
char *ch = (char*)key_binding[index].black_magic;
char slash = 0;
unsigned int env_len = 0;
while (*ch != '\0') {
if (*ch == '/') {
slash = 1;
break;
}
env_len++;
ch++;
}
char *env_str = NULL;
char *env_parsed = NULL;
char *path = NULL;
ch = (char*)key_binding[index].black_magic;
if (*ch == '/') {
path = malloc(strlen((char*)key_binding[index].black_magic));
strcpy(path, (char*)key_binding[index].black_magic);
} else if (slash) {
env_str = malloc(env_len * sizeof(char));
memcpy(env_str, (char*)key_binding[index].black_magic +1, env_len);
env_str[env_len-1] = '\0';
env_parsed = getenv(env_str);
if (env_parsed) {
path = concat(env_parsed, (char*)key_binding[index].black_magic + env_len);
} else {
path = malloc(strlen((char*)key_binding[index].black_magic));
strcpy(path, (char*)key_binding[index].black_magic);
}
} else {
env_parsed = getenv((char*)key_binding[index].black_magic +1);
if (env_parsed) {
path = malloc(strlen(env_parsed)+1);
strcpy(path, env_parsed);
} else {
path = malloc(strlen((char*)key_binding[index].black_magic));
strcpy(path, (char*)key_binding[index].black_magic);
}
}
if (chdir(path) != 0) {
FAIL("jump_to_dir", "jumping to black_magic in config.h failed");
} else {
selected_file_current = dir_get_selected_file_current();
}
/*env_parsed shall not be modified (read: free'd) - the man page*/
if (env_str) {
free(env_str);
}
if(path) {
free(path);
}
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
}
void order_by(int passes, int index){
(void)passes;
seed = time(NULL);
srand(seed);
order_func = key_binding[index].black_magic;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
}
void cmd_on_selected(int passes, int index){
(void)passes;
unsigned int i = 0;
unsigned int hits = 0;
char *file_str = " ";
for (i = 0; i < mid_file_count; i++) {
if (mid_content[i].status & FILE_STATUS_SELECTED) {
file_str = concat(file_str, "\"");
file_str = concat(file_str, mid_content[i].file_name);
file_str = concat(file_str, "\" ");
hits++;
}
}
if (hits) {
btm_buffer = concat(key_binding[index].black_magic, file_str);
} else {
btm_buffer = concat(key_binding[index].black_magic, "\"");
btm_buffer = concat(btm_buffer, mid_content[selected_file_current].file_name);
btm_buffer = concat(btm_buffer, "\"");
}
btm_buffer = concat(btm_buffer, "?");
btm_buffer = concat(btm_buffer, "\n\n");
btm_buffer = concat(btm_buffer, "(y/N)");
status |= STATUS_UPDATE_SCREEN_0;
werase(win_b);
mvwin(win_b, terminal_height-6, 0);
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
render_pass();
timeout(-1); /* negative numbers block until enter is pressed */
/* TODO(2025-07-06T07:22:49) fix fixed buffer size */
char ch = wgetch(win_b);
if (ch == 'y' || ch == 'Y') {
/* the second loop is used to add "./", wich is not being printed" */
char *cmd = malloc(sizeof(char));
/* TODO(2025-07-06T07:23:05) IMPORTANT: this really fucks up when the file has a quotation mark in its name */
if (hits) {
for (i = 0; i < mid_file_count; i++) {
if (mid_content[i].status & FILE_STATUS_SELECTED) {
free(cmd);
cmd = concat((char*)key_binding[index].black_magic, " \"");
cmd = concat(cmd, mid_content[i].file_name);
cmd = concat(cmd, "\"");
if (system(cmd) != 0) {
/*do nothing*/
}
}
}
free(btm_buffer);
btm_buffer = concat("completed: ", key_binding[index].black_magic);
} else {
free(btm_buffer);
free(cmd);
cmd = concat((char*)key_binding[index].black_magic, " \"");
cmd = concat(cmd, mid_content[selected_file_current].file_name);
cmd = concat(cmd, "\"");
if (system(cmd) != 0) {
/*do nothing*/
}
mvaddstr(10,10, cmd);
}
/*system(cmd);*/
free(cmd);
} else {
free(btm_buffer);
btm_buffer = "cancled deletion";
}
timeout(10);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
if (hits) {
free(file_str);
}
}
void yank_file(int passes, int index){
(void)passes;
unsigned long i;
if (yank_files.status & YANK_IS_USED) {
free(yank_files.path);
for (i = 0; i < yank_files.count; i++) {
free(yank_files.list[i]);
}
free(yank_files.list);
yank_files.count = 0;
}
yank_files.path=getcwd(NULL, 0);
yank_files.count = 0;
for (i = 0; i < mid_file_count; i++) {
if (mid_content[i].status & FILE_STATUS_SELECTED) {
yank_files.count++;
}
}
if (yank_files.count == 0) {
yank_files.count = 1;
yank_files.list = (char**)malloc(yank_files.count * sizeof(char*));
*yank_files.list = malloc(strlen(mid_content[selected_file_current].file_name)+1);
strcpy(*yank_files.list, mid_content[selected_file_current].file_name);
} else {
yank_files.list = malloc(yank_files.count * sizeof(char*));
for (i = 0; i < mid_file_count && i < yank_files.count; i++) {
if (mid_content[i].status & FILE_STATUS_SELECTED) {
*yank_files.list = malloc(strlen(mid_content[i].file_name)+1);
strcpy(*yank_files.list, mid_content[i].file_name);
yank_files.list += 1;
}
}
yank_files.list -= yank_files.count;
}
yank_files.status |= YANK_IS_USED;
if (strncmp((char*)key_binding[index].black_magic, "cut", 3) == 0) {
yank_files.status |= YANK_CUT;
yank_files.status &= ~YANK_COPY;
} else {
yank_files.status |= YANK_COPY;
yank_files.status &= ~YANK_CUT;
}
}
void paste(){
unsigned long i;
for (i = 0; i < yank_files.count; i++) {
/*TODO(2025-08-14T22:10:44) escape path*/
char *cmd;
if (yank_files.status & YANK_COPY) {
cmd = concat("false | cp -r -i ", yank_files.path);
} else {
cmd = concat("mv ", yank_files.path);
}
cmd = concat(cmd, "/");
cmd = concat(cmd, *yank_files.list);
cmd = concat(cmd, " ./");
mvprintw(i, 0, cmd);
if (system(cmd) != 0) {
cmd = concat(cmd, *yank_files.list);
while (1) {
cmd = concat(cmd, "_");
if (system(cmd) == 0) {
break;
} }
}
yank_files.list++;
}
yank_files.list -= yank_files.count;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
}
void search(){
unsigned long local_height;
local_height = getmaxy(win_b);
memset(search_buffer, '\0', 255);
render_pass();
curs_set(1);
timeout(-1); /* negative numbers block until enter is pressed */
unsigned int pass = 0;
char ch;
wmove(win_b, local_height-1, 1);
while(1) {
/*ch = mvwgetch(win, y, x + pass);*/
werase(win_b);
mvwaddch(win_b, local_height-1, 0, '/');
mvwaddstr(win_b, local_height-1, 1, search_buffer);
ch = wgetch(win_b);
if (ch == '\n') {
break;
} else if (ch == '\t') { /* tab */
memcpy(search_buffer, mid_content[selected_file_current].file_name, strlen(mid_content[selected_file_current].file_name));
mvwaddstr(win_b, local_height-1, pass, mid_content[selected_file_current].file_name);
pass = strlen(mid_content[selected_file_current].file_name);
} else if (ch == 127) { /* backspace */
mvwdelch(win_b, local_height-1, 1);
wdelch(win_b);
if (pass != 0) {
search_buffer[pass-1] = '\0';
pass--;
}
} else if (ch == 27) { /* esc key */
break;
} else {
search_buffer[pass] = ch;
pass++;
unsigned long i;
for (i = 0; i < mid_file_count; i++) {
if (smartstrcasestr(mid_content[i].file_name, search_buffer)) {
selected_file_current = i;
if (update_selected_file()) {
pthread_cond_signal(&cond_rgt);
}
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0);
break;
}
render_pass();
}
}
}
search_buffer[pass] = '\0';
timeout(10);
curs_set(0);
dir_set_selected_file_current(selected_file_current);
update_selected_file();
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0);
}
void search_next(){
unsigned long i;
for (i = selected_file_current+1; i < mid_file_count; i++) {
if (smartstrcasestr(mid_content[i].file_name, search_buffer)) {
selected_file_current = i;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0);
update_selected_file();
render_pass();
break;
}
}
}
void search_previous(){
unsigned long i;
for (i = selected_file_current-1;; i--) {
if(i > selected_file_current) {
break;
}
if (smartstrcasestr(mid_content[i].file_name, search_buffer)) {
selected_file_current = i;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0);
update_selected_file();
render_pass();
break;
}
}
}