Compare commits

..

2 Commits

Author SHA1 Message Date
nova
539af5fd65 creating new files and dirs possile, dedicated update screen key 2025-07-03 01:24:57 +02:00
nova
f320449572 propper input handling 2025-07-03 00:58:31 +02:00
5 changed files with 132 additions and 27 deletions

View File

@@ -18,8 +18,14 @@ static mimetype mimetype_default_cmd[] = {
static binding key_binding[] = { static binding key_binding[] = {
/*key action */ /*key action */
/*you cannot add bindings that include other bindings in its entirety
* possible: mk, mf
* not possible: gg, ggg
* trying to use ggg will always fail as it will execute gg first instead, resetting the input buffer, thus never
* reaching ggg */
{ "q", quit_program }, { "q", quit_program },
{ " ", toggle_selection }, /* on hovered file/directory */ { " ", toggle_selection }, /* on hovered file/directory */
{ "u", update }, /* executes the entire backend and redrawing of the screen */
{ "h", move_right }, /* moves one dir up */ { "h", move_right }, /* moves one dir up */
{ "t", move_down }, { "t", move_down },
@@ -34,6 +40,9 @@ static binding key_binding[] = {
{ "gg", jump_top }, { "gg", jump_top },
{ "G", jump_bottom }, { "G", jump_bottom },
{ "mk", makedir },
{ "mf", makefile },
{ "a", toggle_hidden_files }, { "a", toggle_hidden_files },
}; };
static unsigned long binding_count = sizeof(key_binding) / sizeof(binding); static unsigned long binding_count = sizeof(key_binding) / sizeof(binding);

View File

@@ -2,6 +2,7 @@
#include <pthread.h> #include <pthread.h>
#include <dirent.h> #include <dirent.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include "defines.h" #include "defines.h"
@@ -31,14 +32,43 @@ 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); int read_string(WINDOW *win, int y, int x, char *str);
unsigned int timeout_time = 0;
extern char *input;
unsigned int input_pass;
int parsed_input_number;
void user_interactions(char *input, WINDOW *win_b) { void user_interactions() {
void (*func_ptr)();
char ch;
move(terminal_height, terminal_width);
ch = getch();
if(ch != ERR) {
timeout(10); /* blocking timeout of getch() */
mvaddch(terminal_height-1, (terminal_width/3)*2 +input_pass, ch);
input[input_pass] = ch;
input_pass++;
}
void (*func_ptr)(int);
unsigned long i = 0; unsigned long i = 0;
unsigned int j = 0;
for (i = 0; i < binding_count; i++) { for (i = 0; i < binding_count; i++) {
if (*input == key_binding[i].key[0]) { 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 = key_binding[i].func;
func_ptr(); func_ptr(parsed_input_number);
memset(input, 0, 255);
input_pass = 0;
timeout(100); /* blocking timeout of getch() */
} }
} }
} }
@@ -54,23 +84,38 @@ void toggle_selection(){
pthread_mutex_unlock(&mutex_mid); pthread_mutex_unlock(&mutex_mid);
pthread_mutex_unlock(&mutex_selection); pthread_mutex_unlock(&mutex_selection);
} }
void move_down(){ void move_down(int passes){
pthread_mutex_lock(&mutex_selection); pthread_mutex_lock(&mutex_selection);
if (passes == 0) {
passes++;
}
/*capping the maximum file is done inside thread_mid */ /*capping the maximum file is done inside thread_mid */
selected_file_current++; selected_file_current += passes;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_selection); pthread_mutex_unlock(&mutex_selection);
} }
void move_up(){ void move_up(int passes){
pthread_mutex_lock(&mutex_selection); pthread_mutex_lock(&mutex_selection);
if (passes == 0) {
passes++;
}
int i;
for (i = 0; i < passes; i++) {
if (selected_file_current != 0) { if (selected_file_current != 0) {
selected_file_current--; selected_file_current--;
} }
}
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_selection); pthread_mutex_unlock(&mutex_selection);
} }
void move_right(){ void move_right(int passes){
if (passes == 0) {
passes++;
}
int i;
for (i = 0; i < passes; i++) {
chdir(".."); chdir("..");
}
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
} }
void move_left(){ void move_left(){
@@ -118,7 +163,6 @@ void jump_top(){
} }
int read_string(WINDOW *win, int y, int x, char *str){ int read_string(WINDOW *win, int y, int x, char *str){
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 */
@@ -151,7 +195,6 @@ int read_string(WINDOW *win, int y, int x, char *str){
str[pass] = '\0'; str[pass] = '\0';
timeout(10); timeout(10);
noecho();
curs_set(0); curs_set(0);
return err; return err;
@@ -311,3 +354,55 @@ void delete(){
} }
} }
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_width;
unsigned long local_height;
getmaxyx(win_b, local_height, local_width);
/* 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);
mkdir(str, 755); /*magic number from default permissions as created by mkdir*/
}
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_width;
unsigned long local_height;
getmaxyx(win_b, local_height, local_width);
/* 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);
}

View File

@@ -6,12 +6,12 @@
#endif #endif
void user_interactions(char *input, WINDOW *win_b); void user_interactions();
void quit_program(); void quit_program();
void toggle_selection(); void toggle_selection();
void move_right(); void move_right(int pssses);
void move_up(); void move_up(int passes);
void move_down(); void move_down(int passes);
void move_left(); void move_left();
void jump_bottom(); void jump_bottom();
void jump_top(); void jump_top();
@@ -19,3 +19,6 @@ void toggle_hidden_files();
void open_with(); void open_with();
void rename_hovered(); void rename_hovered();
void delete(); void delete();
void makedir();
void makefile();
void update();

15
main.c
View File

@@ -17,8 +17,6 @@ unsigned int temp_width = 0;
unsigned int settings; unsigned int settings;
unsigned int file_modifiers; unsigned int file_modifiers;
unsigned int status; unsigned int status;
unsigned int timeout_time = 0;
char input = 0;
WINDOW *win_t; WINDOW *win_t;
WINDOW *win_b; WINDOW *win_b;
@@ -26,6 +24,7 @@ WINDOW *win_l;
WINDOW *win_m; WINDOW *win_m;
WINDOW *win_r; WINDOW *win_r;
char *input; /*used in user_interactions*/
void render_pass(); void render_pass();
void init(); void init();
@@ -82,13 +81,7 @@ int main(){
status &= ~(STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY); status &= ~(STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY);
status |= STATUS_UPDATE_SCREEN_0; status |= STATUS_UPDATE_SCREEN_0;
} }
if ((input = getch())) { user_interactions();
user_interactions(&input, win_b);
timeout_time = 5;
} else {
timeout_time += 10;
}
timeout(timeout_time); /* blocking timeout of getch() */
render_pass(); render_pass();
@@ -169,11 +162,13 @@ void init() {
initscr(); /* start ncurses */ initscr(); /* start ncurses */
noecho(); /* hide keyboard input */ noecho(); /* hide keyboard input */
timeout(timeout_time); /* blocking timeout of getch() */ timeout(0); /* blocking timeout of getch() */
keypad(stdscr, TRUE); keypad(stdscr, TRUE);
curs_set(0); curs_set(0);
/*file_modifiers = (FILE_MODIFIERS_HIDDEN_FILES | FILE_MODIFIERS_SORT_BITMASK);*/ /*file_modifiers = (FILE_MODIFIERS_HIDDEN_FILES | FILE_MODIFIERS_SORT_BITMASK);*/
input = malloc(sizeof(char)*255); /* size of input buffer, out of bounds access will not be accounted for */
memset(input, 0, 255);
status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY); status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
if (getuid() == 0) { if (getuid() == 0) {
status += STATUS_USER_ROOT; status += STATUS_USER_ROOT;

View File

@@ -4,6 +4,7 @@
#include "defines.h" #include "defines.h"
extern unsigned int status; extern unsigned int status;
extern char *input;
extern unsigned int timeout_time; extern unsigned int timeout_time;
extern unsigned int color_count; extern unsigned int color_count;
@@ -56,7 +57,9 @@ void window_btm(WINDOW *win){
} else { } else {
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*/
} }
} }
void window_lft(WINDOW *win){ void window_lft(WINDOW *win){
werase(win); werase(win);