1
0
mirror of https://gittea.dev/nova/th.git synced 2025-10-21 18:30:15 -04:00

Compare commits

...

2 Commits

Author SHA1 Message Date
nova
1d54169c2d implemented file search 2025-08-17 21:29:49 +02:00
nova
f0a2f14966 improvenments to the renderer 2025-08-17 15:35:00 +02:00
7 changed files with 180 additions and 77 deletions

View File

@@ -42,7 +42,9 @@ static const binding key_binding[] = {
{ " ", toggle_selection, NULL, "toggle file selection" }, /* on hovered file/directory */
{ "e", update, NULL, "rerun all backend" }, /* executes the entire backend and redrawing of the screen */
{ "B", enter_shell, "$SHELL", "enter $SHELL shell" },
{ "/", not_implemented, NULL, "search" },
{ "/", search, NULL, "search" },
{ "l", search_next, NULL, "search next" },
{ "L", search_previous, NULL, "search previous" },
{ "h", move_left, NULL, "move left" }, /* moves one dir up */
{ "t", move_down, NULL, "move down" },

103
dir.c
View File

@@ -16,6 +16,7 @@ extern unsigned int file_modifiers;
extern unsigned int color_count;
extern unsigned int terminal_height;
extern volatile unsigned long selected_file_current;
extern volatile unsigned long selected_file_last;
extern color *colors;
int (*order_func)() = sort_natural;
@@ -187,46 +188,9 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
}
for (i = offset_vertical; i < *dir_file_count && i < (terminal_height + offset_vertical); i++) {
if (print_info) {
file_size = dir_content[i].file_size;
char size_index = 0;
while (file_size > 1) {
printed_size=file_size;
file_size /= 1024;
size_index++;
if (size_index >= 6) {
break;
}
}
size_char = sizes[size_index-1];
if (dir_content[i].file_type == FILE_TYPE_DIR || dir_content[i].file_type == FILE_TYPE_SYMLINK) {
offset_back = line_width - (snprintf(NULL,0,"%ld", dir_content[i].file_size) + 1);
} else if (size_char =='B') {
offset_back = line_width - (snprintf(NULL,0,"%0.0lf %c", printed_size, size_char) + 1);
} else {
offset_back = line_width - (snprintf(NULL,0,"%0.2lf %c", printed_size, size_char) + 1);
}
}
if (dir_content[i].status & FILE_STATUS_SELECTED) {
is_selected = 1;
} else {
is_selected = 0;
}
if (dir_content[i].status & FILE_STATUS_SELECTED) {
wattron(win, COLOR_PAIR(8));
} else {
wattron(win, COLOR_PAIR(dir_content[i].color_pair));
}
if (dir_content[i].status & FILE_STATUS_HOVER) {
wattron(win, A_REVERSE);
}
/* shortens the printed file name if it is too long
* example input: aaaaaaaa.txt
* example output: aaa~.txt
* example output: aaa~.txt
* if no extension is found, the name will truncate */
char *file_name;
unsigned long file_name_width = strlen(dir_content[i].file_name);
@@ -258,6 +222,45 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
file_name[file_name_width] = '\0';
}
if (print_info) {
file_size = dir_content[i].file_size;
char size_index = 0;
while (file_size > 1) {
printed_size=file_size;
file_size /= 1024;
size_index++;
if (size_index >= 6) {
break;
}
}
size_char = sizes[size_index-1];
if (dir_content[i].file_type == FILE_TYPE_DIR || dir_content[i].file_type == FILE_TYPE_SYMLINK) {
offset_back = line_width - (snprintf(NULL,0,"%ld", dir_content[i].file_size) + 1);
} else if (size_char =='B') {
offset_back = line_width - (snprintf(NULL,0,"%0.0lf %c", printed_size, size_char) + 1);
} else {
offset_back = line_width - (snprintf(NULL,0,"%0.2lf %c", printed_size, size_char) + 1);
}
}
if (dir_content[i].status & FILE_STATUS_SELECTED) {
is_selected = 1;
} else {
is_selected = 0;
}
if (dir_content[i].status & FILE_STATUS_SELECTED) {
wattron(win, COLOR_PAIR(8));
} else {
wattron(win, COLOR_PAIR(dir_content[i].color_pair));
}
if (dir_content[i].status & FILE_STATUS_HOVER) {
wattron(win, A_REVERSE);
} else {
wattroff(win, A_REVERSE);
}
mvwaddstr(win, i-offset_vertical, 0, bg);
if(print_info) {
#if SETTINGS_LINE_NUMBERS == 2
@@ -298,9 +301,6 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
free(file_name);
}
if (dir_content[i].status & FILE_STATUS_HOVER) {
wattroff(win, A_REVERSE);
}
if (dir_content[i].status & FILE_STATUS_SELECTED) {
wattroff(win, COLOR_PAIR(8));
} else {
@@ -311,3 +311,24 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
free(bg);
}
void update_selected_file(){
if (selected_file_current >= mid_file_count) {
selected_file_current = mid_file_count-1;
}
if (selected_file_current != selected_file_last) {
mid_content[selected_file_last].status &= ~FILE_STATUS_HOVER;
}
selected_file_last = selected_file_current;
free(file_current->file_name);
file_current->file_name = malloc(strlen(mid_content[selected_file_current].file_name)+1);
strcpy(file_current->file_name, mid_content[selected_file_current].file_name);
file_current->file_name[strlen(mid_content[selected_file_current].file_name)] = '\0';
file_current->file_size = mid_content[selected_file_current].file_size;
file_current->file_type = mid_content[selected_file_current].file_type;
file_current->color_pair = mid_content[selected_file_current].color_pair;
file_current->permissions = mid_content[selected_file_current].permissions;
mid_content[selected_file_current].status |= FILE_STATUS_HOVER;
file_current->status = mid_content[selected_file_current].status;
}

7
dir.h
View File

@@ -3,6 +3,7 @@
#include "dir.c"
#endif
extern unsigned long get_dir_size(char *path);
extern void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content);
extern void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file *dir_content);
unsigned long get_dir_size(char *path);
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content);
void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file *dir_content);
void update_selected_file();

View File

@@ -1,11 +1,13 @@
#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;
@@ -33,6 +35,7 @@ extern unsigned int status;
extern char *start_path;
extern char *input;
char search_buffer[255];
unsigned int timeout_time = 0;
unsigned int input_pass;
int parsed_input_number;
@@ -191,9 +194,11 @@ void move_down(int passes){
if (passes == 0) {
passes++;
}
/*capping the maximum file is done inside thread_mid */
selected_file_current += passes;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
update_selected_file();
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0);
pthread_mutex_unlock(&mutex_selection);
}
void move_up(int passes){
@@ -201,12 +206,14 @@ void move_up(int passes){
if (passes == 0) {
passes++;
}
int i;
for (i = 0; i < passes; i++) {
if (selected_file_current != 0) {
selected_file_current--;
}
unsigned long tmp = selected_file_current;
selected_file_current -= passes;
if (tmp < selected_file_current) {
selected_file_current = 0;
}
update_selected_file();
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_selection);
}
@@ -273,6 +280,7 @@ void move_right(){
}
free(mime);
}
update_selected_file();
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
}
void toggle_hidden_files(){
@@ -282,12 +290,14 @@ void toggle_hidden_files(){
void jump_bottom(){
pthread_mutex_lock(&mutex_selection);
selected_file_current = 0 - 1;
update_selected_file();
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();
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_selection);
}
@@ -724,3 +734,91 @@ void paste(){
yank_files.list -= yank_files.count;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
}
void search(){
mvprintw(terminal_height - 1, 0, "/");
status |= STATUS_UPDATE_SCREEN_0;
render_pass();
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 == 27) { /* esc key */
break;
} 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 {
search_buffer[pass] = ch;
pass++;
unsigned long i;
for (i = 0; i < mid_file_count; i++) {
if (strstr(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;
}
}
}
}
search_buffer[pass] = '\0';
timeout(10);
curs_set(0);
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 (strstr(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 (strstr(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;
}
}
}

View File

@@ -27,3 +27,6 @@ void order_by(int passes, int index);
void cmd_on_selected(int passes, int index);
void yank_file(int passes, int index);
void paste();
void search();
void search_next();
void search_previous();

View File

@@ -97,30 +97,11 @@ void *thread_mid(void *data){
pthread_mutex_unlock(&mutex_mid);
pthread_exit(0);
}
pthread_mutex_lock(&mutex_selection);
update_selected_file();
pthread_mutex_unlock(&mutex_selection);
}
pthread_mutex_lock(&mutex_selection);
if (selected_file_current >= mid_file_count) {
selected_file_current = mid_file_count-1;
selected_file_current = mid_file_count-1;
}
if (selected_file_current != selected_file_last) {
mid_content[selected_file_last].status &= ~FILE_STATUS_HOVER;
}
selected_file_last = selected_file_current;
free(file_current->file_name);
file_current->file_name = malloc(strlen(mid_content[selected_file_current].file_name)+1);
strcpy(file_current->file_name, mid_content[selected_file_current].file_name);
file_current->file_name[strlen(mid_content[selected_file_current].file_name)] = '\0';
file_current->file_size = mid_content[selected_file_current].file_size;
file_current->file_type = mid_content[selected_file_current].file_type;
file_current->color_pair = mid_content[selected_file_current].color_pair;
file_current->permissions = mid_content[selected_file_current].permissions;
mid_content[selected_file_current].status |= FILE_STATUS_HOVER;
file_current->status = mid_content[selected_file_current].status;
pthread_mutex_unlock(&mutex_selection);
while(wait_count < 2){
/*wait for thread_rgt and thread_btm to lock*/

View File

@@ -65,7 +65,6 @@ void window_btm(WINDOW *win){
}
void window_lft(WINDOW *win){
werase(win);
box(win, 0, 0);
unsigned long local_width;
unsigned long local_height;
@@ -81,7 +80,6 @@ void window_lft(WINDOW *win){
}
void window_mid(WINDOW *win){
werase(win);
box(win, 0, 0);
unsigned long local_width;
unsigned long local_height;
@@ -100,7 +98,6 @@ void window_mid(WINDOW *win){
}
void window_rgt(WINDOW *win){
werase(win);
box(win, 0, 0);
unsigned long local_width;
unsigned long local_height;