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

Compare commits

...

55 Commits

Author SHA1 Message Date
nova
df726e2f32 delta time no longer reloading images 2025-10-13 23:09:57 +02:00
nova
140457e4b7 fixed copy/paste 2025-10-13 22:47:38 +02:00
nova
23654741bc fixed sort_extension, changes too small to list to other sort methods 2025-10-13 21:44:19 +02:00
nova
fa544204ba implemented jmp_file_index & int passes -> unsigned long 2025-10-12 01:44:27 +02:00
nova
61023ce42e temporary fixes & removal of unused function 2025-10-11 21:49:28 +02:00
nova
3fa16fd8b2 changes too small for proper commit message 2025-10-09 01:01:52 +02:00
nova
ac6d0e8408 avoiding useless memory read/writes & fixing race condition in btm_status 2025-10-09 00:13:25 +02:00
nova
731bfe722a fixing move_right on extensionless files 2025-10-08 23:49:34 +02:00
nova
3082fed378 fixed some race conditions and improved parsing storage size 2025-10-08 21:33:06 +02:00
nova
23ff0b07ec adding forgotten mutex unlocks 2025-10-05 21:56:56 +02:00
nova
c2d88f4909 moving stuff around to avoid 'previous implicit declaration' 2025-10-05 21:46:31 +02:00
nova
0b98e8eb68 removal of stray printf 2025-10-05 14:35:33 +02:00
nova
87ae2a5e8f refactoring of name shortening 2025-10-05 14:34:35 +02:00
nova
f0ad6295f7 removal of rand() in sort_rand 2025-10-05 00:57:18 +02:00
nova
e979209de3 slight likely branch improvements 2025-10-03 16:53:18 +02:00
nova
8fade0c4a8 profiling target added 2025-10-02 20:15:14 +02:00
nova
333d4d1df6 slight speed optimization 2025-10-02 20:14:53 +02:00
nova
c6763233bf resolved all compiler warnings so far 2025-09-30 22:05:26 +02:00
nova
7ee16c8f4a now accounting for hardlinks & refactored sorting 2025-09-30 21:54:56 +02:00
nova
b32848ad3d accounted for some race conditions 2025-09-29 21:40:35 +02:00
nova
4d9dc46691 yank_name now implemented 2025-09-29 21:10:34 +02:00
nova
8dcf88baea sum the size of the current dir 2025-09-29 20:10:40 +02:00
nova
8839f737c5 addded select_all 2025-09-29 19:29:31 +02:00
nova
36012b1a71 fixed image previews sticking around longer than expected 2025-09-29 19:17:23 +02:00
nova
4784ec3f64 fixed infinite loop on copy/paste 2025-09-29 19:13:37 +02:00
nova
d3e46846cd improvements to sort_rand 2025-09-28 10:45:33 +02:00
nova
21a11d8be1 fixed images not being flushed when going ontop of a directory 2025-09-24 23:14:20 +02:00
nova
b46d2da308 delta time to reload directory 2025-09-24 23:07:29 +02:00
nova
fe781c2d3c image previews can now be disabled 2025-09-24 22:44:13 +02:00
nova
fb1af6d2d2 now printing left storage & related refactoring 2025-09-24 22:20:07 +02:00
nova
14948ba573 fixed wrong condition being send 2025-09-22 19:20:14 +02:00
nova
e0a5871bc2 relative line numbers now show the index of the current file 2025-09-21 21:32:45 +02:00
nova
395c5fcb09 improvements to sort_natural 2025-09-21 21:30:15 +02:00
nova
525f205d67 recursive file deletion 2025-09-08 23:18:25 +02:00
nova
c923497f01 cast to unsigned char for proper sorting 2025-08-30 21:04:30 +02:00
nova
69f9a609e5 removal of socal_status 2025-08-30 21:01:40 +02:00
nova
5afabfb412 improvenments to random sort 2025-08-29 22:42:46 +02:00
nova
9f3042ccac improvenments to natural sort 2025-08-28 23:00:54 +02:00
nova
a05c3ecd96 search file function now does file previews 2025-08-24 20:09:32 +02:00
nova
4a9c38d034 handling of empty dirs 2025-08-24 19:45:08 +02:00
nova
ba1e95bca3 fixed images_clear ordering 2025-08-24 00:37:06 +02:00
nova
922881cd45 maximum size of file_previews 2025-08-24 00:04:24 +02:00
nova
59964cf828 moved left and rigth thread signaling into thread_mid 2025-08-23 23:21:30 +02:00
nova
e1c298ae50 removal of global file_current 2025-08-23 23:19:11 +02:00
nova
a57869a7a1 imrovenments to rendering 2025-08-23 22:48:46 +02:00
nova
520d4eca13 further thread pool stuff 2025-08-23 22:06:10 +02:00
nova
963a018419 removed the recreation and destruction of threads 2025-08-23 21:44:03 +02:00
nova
a0a102e5f6 tab completion in search and read_string 2025-08-23 20:58:48 +02:00
nova
0d2310cd7c searching now writes selected file in linked_dir 2025-08-23 20:48:19 +02:00
nova
3f90802663 refractoring, fixed enter_shell 2025-08-18 22:01:16 +02:00
nova
f25768d522 linked list to keep track of past directory indexes 2025-08-18 21:07:18 +02:00
nova
55cd4077b1 smart case search in file search 2025-08-17 23:21:28 +02:00
nova
3a2c226ed9 removed unnecessary render_pass 2025-08-17 21:54:35 +02:00
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
16 changed files with 1243 additions and 636 deletions

View File

@@ -3,6 +3,7 @@ CFLAGS := -Wall -Wextra -O2 -flto=auto
CURSES := -lncursesw -ltinfow #utf8 support CURSES := -lncursesw -ltinfow #utf8 support
#CURSES := -lncurses -tinfo #no utf8 #CURSES := -lncurses -tinfo #no utf8
CFLAGS_DEBUG := $(CFLAGS) -g CFLAGS_DEBUG := $(CFLAGS) -g
CFLAGS_PROFILE := $(CFLAGS) -pg
GDB := gdb --tui ./th GDB := gdb --tui ./th
VALGRIND := valgrind --leak-check=full --track-origins=yes --show-leak-kinds=all --log-fd=9 9>>valgrind.log ./th VALGRIND := valgrind --leak-check=full --track-origins=yes --show-leak-kinds=all --log-fd=9 9>>valgrind.log ./th
HELGRIND := valgrind --tool=helgrind --log-fd=9 9>>helgrind.log ./th HELGRIND := valgrind --tool=helgrind --log-fd=9 9>>helgrind.log ./th
@@ -15,6 +16,9 @@ d:
$(CC) ./main.c -o th -std=c89 $(CFLAGS_DEBUG) $(CURSES) $(CC) ./main.c -o th -std=c89 $(CFLAGS_DEBUG) $(CURSES)
$(GDB) $(GDB)
p:
$(CC) ./main.c -o th -std=c89 $(CFLAGS_PROFILE) $(CURSES)
v: v:
$(CC) ./main.c -o th -std=c89 $(CFLAGS_DEBUG) $(CURSES) $(CC) ./main.c -o th -std=c89 $(CFLAGS_DEBUG) $(CURSES)
$(VALGRIND) $(VALGRIND)

View File

@@ -1,3 +1,6 @@
#include <stdlib.h>
#include <string.h>
char* concat(const char *s1, const char *s2){ char* concat(const char *s1, const char *s2){
const size_t len1 = strlen(s1); const size_t len1 = strlen(s1);
const size_t len2 = strlen(s2); const size_t len2 = strlen(s2);
@@ -6,5 +9,46 @@ char* concat(const char *s1, const char *s2){
memcpy(result + len1, s2, len2 + 1); memcpy(result + len1, s2, len2 + 1);
return result; return result;
} }
char* smartstrcasestr(char *haystack, const char *needle){
char smart = 0;
char *ret;
char passes = 0;
while (*needle) {
if (*needle >= 'A' && *needle <= 'Z') {
smart = 1;
break;
}
passes++;
needle++;
}
needle -= passes;
if (smart == 0) {
char *needle_case = malloc(strlen(needle)+1);
strcpy(needle_case, needle);
passes = 0;
while (*needle_case) {
*needle_case = *needle_case | ' ';
needle_case++;
passes++;
}
needle_case -= passes;
char *haystack_case = malloc(strlen(haystack)+1);
strcpy(haystack_case, haystack);
passes = 0;
while (*haystack_case) {
*haystack_case = *haystack_case | ' ';
haystack_case++;
passes++;
}
haystack_case -= passes;
ret = strstr(haystack_case, needle_case);
free(needle_case);
free(haystack_case);
} else {
ret = strstr(haystack, needle);
}
return ret;
}

View File

@@ -6,3 +6,4 @@
char* concat(const char *s1, const char *s2); char* concat(const char *s1, const char *s2);
char* smartstrcasestr(char *haystack, const char *needle);

View File

@@ -1,10 +1,14 @@
#define SETTINGS_LINE_NUMBERS 2 /* 0 is disabled, 1 is enabled, 2 is relative */
#define SETTINGS_UEBERZUG_IMAGE_PREVIEW 1 /* 0 is disabled, 1 is enabled, 2 is with caching */
#define SETTINGS_RELOAD_DIR_DELTA 10 /* 0 is disabled, time in seconds of how often the directory should be reload */
/* {{{ */
#ifndef CONFIG_GUARD #ifndef CONFIG_GUARD
#define CONFIG_GUARD #define CONFIG_GUARD
#include "defines.h" #include "defines.h"
#include "sorting.h" #include "sorting.h"
#include "interactions.h" #include "interactions.h"
/* }}} */
#define SETTINGS_LINE_NUMBERS 2 /* 0 is disabled, 1 is enabled, 2 is relative */
static const mimetype mimetype_default_cmd[] = { static const mimetype mimetype_default_cmd[] = {
/* mimetype shell command /* mimetype shell command
@@ -40,9 +44,13 @@ static const binding key_binding[] = {
/* blackmagic holds a modifier of an action, either as string or as function pointer depending on the action */ /* blackmagic holds a modifier of an action, either as string or as function pointer depending on the action */
{ "q", quit_program, NULL, "quit" }, { "q", quit_program, NULL, "quit" },
{ " ", toggle_selection, NULL, "toggle file selection" }, /* on hovered file/directory */ { " ", toggle_selection, NULL, "toggle file selection" }, /* on hovered file/directory */
{ "v", select_all, NULL, "select all files in dir" },
{ "e", update, NULL, "rerun all backend" }, /* executes the entire backend and redrawing of the screen */ { "e", update, NULL, "rerun all backend" }, /* executes the entire backend and redrawing of the screen */
{ "B", enter_shell, "$SHELL", "enter $SHELL shell" }, { "B", enter_shell, "$SHELL", "enter $SHELL shell" },
{ "/", not_implemented, NULL, "search" }, { "/", search, NULL, "search" },
{ ":", jmp_file_index, NULL, "jump to file on input number" },
{ "l", search_next, NULL, "search next" },
{ "L", search_previous, NULL, "search previous" },
{ "h", move_left, NULL, "move left" }, /* moves one dir up */ { "h", move_left, NULL, "move left" }, /* moves one dir up */
{ "t", move_down, NULL, "move down" }, { "t", move_down, NULL, "move down" },
@@ -53,6 +61,8 @@ static const binding key_binding[] = {
{ "r", rename_hovered, NULL, "rename hovered file" }, /* renames currently hovered file/directory */ { "r", rename_hovered, NULL, "rename hovered file" }, /* renames currently hovered file/directory */
{ "dD", delete, NULL, "delete file" }, /* deletes currently hovered OR selected file/directory { "dD", delete, NULL, "delete file" }, /* deletes currently hovered OR selected file/directory
* this means that it does not delete the hovered files if files are already selected */ * this means that it does not delete the hovered files if files are already selected */
{ "yn", yank_text, "name", "yank filename of hovered file" },
{ "yp", yank_text, "path", "yank path of hovered file" },
{ "yy", yank_file, "copy", "copy/yank file/directory" }, { "yy", yank_file, "copy", "copy/yank file/directory" },
{ "dd", yank_file, "cut", "cut file/directory" }, { "dd", yank_file, "cut", "cut file/directory" },
{ "pp", paste, NULL, "paste" }, { "pp", paste, NULL, "paste" },
@@ -60,10 +70,14 @@ static const binding key_binding[] = {
{ "G", jump_bottom, NULL, "jump to last file in dir" }, { "G", jump_bottom, NULL, "jump to last file in dir" },
{ "gg", jump_top, NULL, "jump to first file in dir" }, { "gg", jump_top, NULL, "jump to first file in dir" },
{ "gh", jump_to_dir, "$HOME", "jump to $HOME" }, { "gh", jump_to_dir, "$HOME", "jump to $HOME" },
{ "gs", jump_to_dir, "$START_PATH", "jump to $START_PATH" }, { "gs", jump_to_dir, "$START_PATH", "jump to $START_PATH" }, /* the path you started th in */
{ "gD", jump_to_dir, "$HOME/Downloads", "jump to $HOME/Downloads" }, { "gD", jump_to_dir, "$HOME/Downloads", "jump to $HOME/Downloads" },
{ "gC", jump_to_dir, "$HOME/Documents", "jump to $HOME/Documents" },
{ "gP", jump_to_dir, "$HOME/Pictures", "jump to $HOME/Pictures" },
{ "gM", jump_to_dir, "$HOME/Music", "jump to $HOME/Music" },
{ "gd", jump_to_dir, "/dev", "jump to /dev" }, { "gd", jump_to_dir, "/dev", "jump to /dev" },
{ "ge", jump_to_dir, "/etc", "jump to /etc" }, { "ge", jump_to_dir, "/etc", "jump to /etc" },
{ "gp", jump_to_dir, "/etc/portage", "jump to /etc/portage" },
{ "gm", jump_to_dir, "/mnt", "jump to /mnt" }, { "gm", jump_to_dir, "/mnt", "jump to /mnt" },
{ "go", jump_to_dir, "/opt", "jump to /opt" }, { "go", jump_to_dir, "/opt", "jump to /opt" },
{ "gt", jump_to_dir, "/tmp", "jump to /tmp" }, { "gt", jump_to_dir, "/tmp", "jump to /tmp" },
@@ -77,11 +91,10 @@ static const binding key_binding[] = {
{ "uz", cmd_on_selected, "unzip ", "unzip zip" }, { "uz", cmd_on_selected, "unzip ", "unzip zip" },
{ "on", order_by, sort_natural, "order natural" }, { "on", order_by, sort_natural, "order natural" },
{ "or", not_implemented, "", "order reverse" }, { "oe", order_by, sort_extension, "order extension" },
{ "oe", not_implemented, "", "order extension" },
{ "os", order_by, sort_size, "order size" }, { "os", order_by, sort_size, "order size" },
{ "ot", order_by, sort_type, "order type" }, { "ot", order_by, sort_type, "order type" },
{ "oz", order_by, sort_random, "order random" }, { "or", order_by, sort_random, "order random" },
{ "oa", order_by, sort_alpha, "order alphabetically" }, { "oa", order_by, sort_alpha, "order alphabetically" },
{ "mk", makedir, NULL, "create directory" }, { "mk", makedir, NULL, "create directory" },
@@ -90,15 +103,27 @@ static const binding key_binding[] = {
{ "a", toggle_hidden_files, NULL, "toggle hidden files" }, { "a", toggle_hidden_files, NULL, "toggle hidden files" },
{ "\x7F", toggle_hidden_files, NULL, "toggle hidden files" }, /* backspace key */ { "\x7F", toggle_hidden_files, NULL, "toggle hidden files" }, /* backspace key */
}; };
static const char size_unit[] = { 'B', 'K', 'M', 'G', 'T', 'P' }; /* this defines the maximum size unit, deleting everything except B results in all sizes being displayed in byte */
static const char clipboard_cmd[] = "xsel -ib --trim"; /* assembles the following shell cmd: echo "hovered_file" | clipboard_cmd */
static const char ui_btm_text_storage_left[] = "total free";
static const char ui_btm_current_dir_size[] = "sum of dir,";
/* {{{ */
static const unsigned long binding_count = sizeof(key_binding) / sizeof(binding); static const unsigned long binding_count = sizeof(key_binding) / sizeof(binding);
static const unsigned long mimetype_default_count = sizeof(mimetype_default_cmd) / sizeof(mimetype); static const unsigned long mimetype_default_count = sizeof(mimetype_default_cmd) / sizeof(mimetype);
static const unsigned long file_extension_default_count = sizeof(file_extension_default_cmd) / sizeof(extension); static const unsigned long file_extension_default_count = sizeof(file_extension_default_cmd) / sizeof(extension);
static const char size_unit_count = (sizeof(size_unit) / sizeof(size_unit[0])) - 1;
#else #else
static const char clipboard_cmd[];
static const mimetype mimetype_default_cmd[]; static const mimetype mimetype_default_cmd[];
static const extension file_extension_default_cmd[]; static const extension file_extension_default_cmd[];
static const binding key_binding[]; static const binding key_binding[];
static const unsigned long binding_count; static const unsigned long binding_count;
static const unsigned long mimetype_default_count; static const unsigned long mimetype_default_count;
static const unsigned long file_extension_default_count; static const unsigned long file_extension_default_count;
static const char size_unit[];
static const char size_unit_count;
#endif #endif
/* }}} */

View File

@@ -9,6 +9,7 @@
#define STATUS_UPDATE_SCREEN_RELOAD_FULL 32 #define STATUS_UPDATE_SCREEN_RELOAD_FULL 32
#define STATUS_USER_ROOT 64 #define STATUS_USER_ROOT 64
#define STATUS_INPUT_MATCH 128 #define STATUS_INPUT_MATCH 128
#define STATUS_DELTA_TIME 256
#define SETTINGS_HAS_COLOR 1 #define SETTINGS_HAS_COLOR 1
@@ -25,8 +26,7 @@
#define FILE_STATUS_HOVER 1 #define FILE_STATUS_HOVER 1
#define FILE_STATUS_SELECTED 2 #define FILE_STATUS_SELECTED 2
#define FILE_STATUS_IS_REGULAR_FILE 4 #define FILE_STATUS_IS_REGULAR_FILE 4
#define FILE_STATUS_DIR_EMPTY 64 /* if a directory is empty */ #define FILE_STATUS_FILE_OPEN 64 /* only used for file previews */
#define FILE_STATUS_FILE_OPEN 128 /* only used for file previews */
#define COLOR_UNKNOWN 0 #define COLOR_UNKNOWN 0
#define COLOR_DIR 1 #define COLOR_DIR 1
@@ -40,16 +40,16 @@
#define COLOR_ORPHAN 9 #define COLOR_ORPHAN 9
#define COLOR_PATH 10 #define COLOR_PATH 10
#define FILE_TYPE_UNKNOWN COLOR_UNKNOWN #define FILE_TYPE_UNKNOWN 0
#define FILE_TYPE_DIR COLOR_DIR #define FILE_TYPE_EXEC 1
#define FILE_TYPE_EXEC COLOR_EXEC #define FILE_TYPE_REGULAR 2
#define FILE_TYPE_REGULAR COLOR_REGULAR #define FILE_TYPE_BLOCK 3
#define FILE_TYPE_SYMLINK COLOR_SYMLINK #define FILE_TYPE_CHARDEV 4
#define FILE_TYPE_BLOCK COLOR_BLOCK #define FILE_TYPE_SOCK 5
#define FILE_TYPE_CHARDEV COLOR_CHARDEV #define FILE_TYPE_FIFO 6
#define FILE_TYPE_SOCK COLOR_SOCK #define FILE_TYPE_ORPHAN 7
#define FILE_TYPE_FIFO COLOR_FIFO #define FILE_TYPE_DIR 32
#define FILE_TYPE_ORPHAN COLOR_ORPHAN #define FILE_TYPE_SYMLINK 64
#define FILE_TYPE_OPEN_FILE 128 /* this is only used in rgt_content to print a file preview, not the dir */ #define FILE_TYPE_OPEN_FILE 128 /* this is only used in rgt_content to print a file preview, not the dir */
#define YANK_IS_USED 1 #define YANK_IS_USED 1
@@ -91,4 +91,10 @@ typedef struct Yank {
char **list; char **list;
unsigned long count; unsigned long count;
} yank; } yank;
typedef struct Linked_dir {
char *path;
unsigned long selected_file_current;
struct Linked_dir *next;
} linked_dir;
#endif #endif

271
dir.c
View File

@@ -5,19 +5,25 @@
#include <string.h> #include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h>
#include "sorting.h" #include "sorting.h"
#include "defines.h" #include "defines.h"
#include "config.h" #include "config.h"
extern file *mid_content;
extern unsigned long mid_file_count;
extern unsigned int settings; extern unsigned int settings;
extern unsigned int file_modifiers; extern unsigned int file_modifiers;
extern unsigned int color_count; extern unsigned int color_count;
extern unsigned int terminal_height; extern unsigned int terminal_height;
extern volatile unsigned long selected_file_current; extern volatile unsigned long selected_file_current;
extern volatile unsigned long selected_file_last;
extern color *colors; extern color *colors;
int (*order_func)() = sort_natural; int (*order_func)() = sort_natural;
linked_dir *visited_dirs;
linked_dir *current_dir;
unsigned long get_dir_size(char *path); unsigned long get_dir_size(char *path);
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content); void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content);
@@ -28,29 +34,31 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
unsigned long get_dir_size(char *path){ unsigned long get_dir_size(char *path){
DIR *dir = opendir(path); DIR *dir = opendir(path);
unsigned long entry_count = 0; unsigned long entry_count = 0;
if (dir) { struct dirent *entry;
struct dirent *entry; if (dir && file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) {
while ((entry=readdir(dir))) { while ((entry=readdir(dir))) {
if (entry->d_name[0] != '.' || (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) { entry_count++;
}
/* removes files "." and ".." */
entry_count -= 2;
} else if (dir) {
while ((entry=readdir(dir))) {
if (entry->d_name[0] != '.') {
entry_count++; entry_count++;
} }
} }
}
closedir(dir);
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) {
/* removes files "." and ".." */
entry_count -= 2;
} }
closedir(dir);
return entry_count; return entry_count;
} }
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content){ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content){
struct dirent **entry; struct dirent **entry = NULL;
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { /* print hidden files */ if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { /* print hidden files */
scandir(path, &entry, skip_dot, alphasort); scandir(path, &entry, skip_dot, NULL);
} else { } else {
scandir(path, &entry, skip_hidden_files, alphasort); scandir(path, &entry, skip_hidden_files, NULL);
} }
unsigned long i = 0; unsigned long i = 0;
@@ -59,24 +67,20 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten
} else { } else {
dir_content[i].file_name = malloc(strlen(entry[i]->d_name)+1); dir_content[i].file_name = malloc(strlen(entry[i]->d_name)+1);
strcpy(dir_content[i].file_name, entry[i]->d_name); strcpy(dir_content[i].file_name, entry[i]->d_name);
dir_content[i].file_name[strlen(entry[i]->d_name)] = '\0';
struct stat *file; struct stat *file;
file = malloc(sizeof(struct stat)); file = malloc(sizeof(struct stat));
memset(file, ' ', sizeof(struct stat));
/* using the full path allows using the same function for all windows */ /* using the full path allows using the same function for all windows */
unsigned long path_len = strlen(path);
char *full_path = malloc(strlen(path) + strlen(entry[i]->d_name) + 1 + sizeof("/")); char *full_path = malloc(strlen(path) + strlen(entry[i]->d_name) + 1 + sizeof("/"));
memcpy(full_path, path, strlen(path)); memcpy(full_path, path, strlen(path));
memcpy(full_path + path_len, "/", sizeof("/")); memcpy(full_path + strlen(path) + sizeof("/") - 1, entry[i]->d_name, strlen(entry[i]->d_name) + 1);
memcpy(full_path + path_len + sizeof("/") - 1, entry[i]->d_name, strlen(entry[i]->d_name) + 1); full_path[strlen(path)] = '/';
lstat(full_path, file); lstat(full_path, file);
dir_content[i].file_size = file->st_size; dir_content[i].file_size = file->st_size;
dir_content[i].permissions = 1;
dir_content[i].permissions = file->st_mode; dir_content[i].permissions = file->st_mode;
if (S_ISDIR(file->st_mode)) { if (S_ISDIR(file->st_mode)) {
@@ -84,12 +88,30 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten
dir_content[i].color_pair = COLOR_DIR; dir_content[i].color_pair = COLOR_DIR;
dir_content[i].file_size = get_dir_size(full_path); dir_content[i].file_size = get_dir_size(full_path);
} else if (S_ISLNK(file->st_mode)) { } else if (S_ISLNK(file->st_mode)) {
dir_content[i].file_type = FILE_TYPE_SYMLINK; stat(full_path, file);
dir_content[i].color_pair = COLOR_SYMLINK; if (S_ISDIR(file->st_mode)) {
dir_content[i].file_size = get_dir_size(full_path); dir_content[i].file_type = FILE_TYPE_DIR | FILE_TYPE_SYMLINK;
dir_content[i].color_pair = COLOR_SYMLINK;
dir_content[i].file_size = get_dir_size(full_path);
} else {
dir_content[i].file_type = FILE_TYPE_REGULAR | FILE_TYPE_SYMLINK;
dir_content[i].color_pair = COLOR_SYMLINK;
}
} else if (file->st_mode & S_IXUSR) { } else if (file->st_mode & S_IXUSR) {
dir_content[i].file_type = FILE_TYPE_EXEC; dir_content[i].file_type = FILE_TYPE_EXEC;
dir_content[i].color_pair = COLOR_EXEC; dir_content[i].color_pair = COLOR_EXEC;
} else if (S_ISREG(file->st_mode)) {
dir_content[i].file_type = FILE_TYPE_REGULAR;
dir_content[i].color_pair = COLOR_REGULAR;
unsigned long j = 0;
char *extension = strrchr(entry[i]->d_name, '.');
if (extension) {
for (j = 0; j < color_count; j++) {
if (!strncmp(colors[j].file_extension, extension, strlen(colors[j].file_extension))) {
dir_content[i].color_pair = colors[j].color_pair;
}
}
}
} else if (S_ISBLK(file->st_mode)) { } else if (S_ISBLK(file->st_mode)) {
dir_content[i].file_type = FILE_TYPE_BLOCK; dir_content[i].file_type = FILE_TYPE_BLOCK;
dir_content[i].color_pair = COLOR_BLOCK; dir_content[i].color_pair = COLOR_BLOCK;
@@ -101,19 +123,6 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten
} else if (S_ISSOCK(file->st_mode)) { } else if (S_ISSOCK(file->st_mode)) {
dir_content[i].file_type = FILE_TYPE_SOCK; dir_content[i].file_type = FILE_TYPE_SOCK;
dir_content[i].color_pair = COLOR_SOCK; dir_content[i].color_pair = COLOR_SOCK;
} else if (S_ISREG(file->st_mode)) {
dir_content[i].file_type = FILE_TYPE_REGULAR;
dir_content[i].color_pair = COLOR_REGULAR;
unsigned long j = 0;
char *extension = strrchr(entry[i]->d_name, '.');
if (extension) {
for (j = 0; j < color_count; j++) {
if (!strcmp(colors[j].file_extension, extension)) {
dir_content[i].color_pair = colors[j].color_pair;
}
}
} else {
}
} else { } else {
dir_content[i].file_type = COLOR_REGULAR; dir_content[i].file_type = COLOR_REGULAR;
dir_content[i].color_pair = COLOR_REGULAR; dir_content[i].color_pair = COLOR_REGULAR;
@@ -121,7 +130,7 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten
char *extension = strrchr(entry[i]->d_name, '.'); char *extension = strrchr(entry[i]->d_name, '.');
if (extension) { if (extension) {
for (j = 0; j < color_count; j++) { for (j = 0; j < color_count; j++) {
if (!strcmp(colors[j].file_extension, extension)) { if (!strncmp(colors[j].file_extension, extension, strlen(colors[j].file_extension))) {
dir_content[i].color_pair = colors[j].color_pair; dir_content[i].color_pair = colors[j].color_pair;
} }
} }
@@ -138,7 +147,10 @@ void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_conten
for (i = 0; i < *dir_file_count; i++) { for (i = 0; i < *dir_file_count; i++) {
free(entry[i]); free(entry[i]);
} }
free(entry); if (entry != NULL) {
free(entry);
} else {
}
} }
@@ -155,17 +167,16 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
float printed_size = 0; float printed_size = 0;
char size_char = ' '; char size_char = ' ';
char is_selected = 0; char is_selected = 0;
static const char sizes[6] = { 'B', 'K', 'M', 'G', 'T', 'P' };
unsigned long offset_vertical = 0; unsigned long offset_vertical = 0;
unsigned long offset_back = 0; unsigned long offset_back = 0;
#if SETTINGS_LINE_NUMBERS == 0 #if SETTINGS_LINE_NUMBERS == 0
unsigned long offset_front = 0; unsigned long offset_front = 0;
#else #else
unsigned long offset_front = 2; long offset_front = 2;
#endif #endif
unsigned long offset_index = 2; /* only used for the index of the file itself */ long offset_index = 2; /* only used for the index of the file itself */
if (print_info) { if (print_info) {
if (*dir_file_count > 9) { if (*dir_file_count > 9) {
@@ -184,29 +195,33 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
offset_vertical = selected_file_current - (terminal_height/3)*2; offset_vertical = selected_file_current - (terminal_height/3)*2;
} }
} }
} else {
offset_front = 0;
} }
for (i = offset_vertical; i < *dir_file_count && i < (terminal_height + offset_vertical); i++) { for (i = offset_vertical; i < *dir_file_count && i < (terminal_height + offset_vertical); i++) {
if (print_info) { if (print_info) {
file_size = dir_content[i].file_size; file_size = dir_content[i].file_size;
char size_index = 0; char size_index = -1;
while (file_size > 1) {
do {
printed_size=file_size; printed_size=file_size;
file_size /= 1024; file_size /= 1024;
size_index++; size_index++;
if (size_index >= 6) { } while (file_size > 1 && size_index < size_unit_count);
break; size_char = size_unit[(unsigned)size_index];
}
} if (dir_content[i].file_type &= FILE_TYPE_DIR) {
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); offset_back = line_width - (snprintf(NULL,0,"%ld", dir_content[i].file_size) + 1);
} else if (size_char =='B') { } else if (size_char =='B') {
offset_back = line_width - (snprintf(NULL,0,"%0.0lf %c", printed_size, size_char) + 1); offset_back = line_width - (snprintf(NULL,0,"%0.0lf %c", printed_size, size_char) + 1);
} else { } else {
offset_back = line_width - (snprintf(NULL,0,"%0.2lf %c", printed_size, size_char) + 1); offset_back = line_width - (snprintf(NULL,0,"%0.2lf %c", printed_size, size_char) + 1);
} }
} else {
offset_back = line_width;
} }
if (dir_content[i].status & FILE_STATUS_SELECTED) { if (dir_content[i].status & FILE_STATUS_SELECTED) {
@@ -214,7 +229,6 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
} else { } else {
is_selected = 0; is_selected = 0;
} }
if (dir_content[i].status & FILE_STATUS_SELECTED) { if (dir_content[i].status & FILE_STATUS_SELECTED) {
wattron(win, COLOR_PAIR(8)); wattron(win, COLOR_PAIR(8));
} else { } else {
@@ -222,47 +236,46 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
} }
if (dir_content[i].status & FILE_STATUS_HOVER) { if (dir_content[i].status & FILE_STATUS_HOVER) {
wattron(win, A_REVERSE); wattron(win, A_REVERSE);
} else {
wattroff(win, A_REVERSE);
} }
/* shortens the printed file name if it is too long /* shortens the printed file name if it is too long
* example input: aaaaaaaa.txt * example input: aaaaaaaa.txt
* example output: aaa~.txt * example output: aaa~.txt
* if no extension is found, the name will truncate */ * if no extension is found, the name will truncate */
char *file_name; char *file_name = NULL;
unsigned long file_name_width = strlen(dir_content[i].file_name); unsigned long printable_size = (offset_back - is_selected - offset_front);
if ((file_name_width + offset_front + is_selected) > offset_back - 1) { if (strlen(dir_content[i].file_name) > printable_size) {
char *extension = strrchr(dir_content[i].file_name, '.'); char *extension = strrchr(dir_content[i].file_name, '.');
if (extension) { if (extension && extension != dir_content[i].file_name) {
int char_offset = (file_name_width + offset_front + is_selected) - (offset_back - 1) ; file_name = malloc(printable_size);
if ((file_name_width - char_offset - strlen(extension) - 1) > 1) { printable_size -= strlen(extension);
file_name = malloc(file_name_width - char_offset + 1);
memcpy(file_name, dir_content[i].file_name, file_name_width - char_offset);
memcpy(file_name + (file_name_width - char_offset - strlen(extension)), extension, strlen(extension));
file_name[file_name_width - char_offset - strlen(extension) - 1] = '~';
file_name[file_name_width - char_offset] = '\0';
} else {
file_name = malloc(strlen(extension)+1);
file_name[0] = '~';
memcpy(file_name+1, extension, strlen(extension));
file_name[strlen(extension)] = '\0';
}
} else {
file_name = malloc(file_name_width+1);
memcpy(file_name, dir_content[i].file_name, file_name_width);
file_name[file_name_width] = '\0';
}
memcpy(file_name, dir_content[i].file_name, printable_size);
memcpy(file_name + printable_size-1, extension, strlen(extension));
file_name[printable_size + strlen(extension)-1] = '\0';
file_name[printable_size - 2] = '~';
} else {
file_name = malloc(printable_size-1);
memcpy(file_name, dir_content[i].file_name, printable_size);
file_name[printable_size-2] = '~';
file_name[printable_size-1] = '\0';
}
} else { } else {
file_name = malloc(file_name_width+1); file_name = malloc(strlen(dir_content[i].file_name)+1);
memcpy(file_name, dir_content[i].file_name, file_name_width); memcpy(file_name, dir_content[i].file_name, strlen(dir_content[i].file_name)+1);
file_name[file_name_width] = '\0';
} }
mvwaddstr(win, i-offset_vertical, 0, bg); mvwaddstr(win, i-offset_vertical, 0, bg);
if(print_info) { if(print_info) {
#if SETTINGS_LINE_NUMBERS == 2 #if SETTINGS_LINE_NUMBERS == 2
long i_ = (selected_file_current) - i; long i_ = (selected_file_current) - i;
offset_index = !((selected_file_current) - i); offset_index = 0;
while(i_) { while(i_) {
offset_index++; offset_index++;
i_ /= 10; i_ /= 10;
@@ -270,6 +283,13 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
long relative_index = selected_file_current - i; long relative_index = selected_file_current - i;
if (relative_index < 0) { if (relative_index < 0) {
relative_index = relative_index * -1; relative_index = relative_index * -1;
} else if (relative_index == 0) {
i_ = (selected_file_current != 0) ? (selected_file_current) : 1;
while(i_) {
offset_index++;
i_ /= 10;
}
relative_index = i;
} }
mvwprintw(win, i-offset_vertical, offset_front-offset_index-1, "%ld", relative_index); mvwprintw(win, i-offset_vertical, offset_front-offset_index-1, "%ld", relative_index);
@@ -284,9 +304,8 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
#endif #endif
mvwaddnstr(win, i-offset_vertical, offset_front+is_selected, file_name, line_width-offset_front-is_selected-2); mvwaddnstr(win, i-offset_vertical, offset_front+is_selected, file_name, line_width-offset_front-is_selected-2);
free(file_name);
if (dir_content[i].file_type == FILE_TYPE_DIR || dir_content[i].file_type == FILE_TYPE_SYMLINK) { if (dir_content[i].file_type &= FILE_TYPE_DIR) {
mvwprintw(win, i-offset_vertical, offset_back, "%ld", dir_content[i].file_size); mvwprintw(win, i-offset_vertical, offset_back, "%ld", dir_content[i].file_size);
}else if (size_char =='B') { }else if (size_char =='B') {
mvwprintw(win, i-offset_vertical, offset_back, "%0.0lf %c", printed_size, size_char); mvwprintw(win, i-offset_vertical, offset_back, "%0.0lf %c", printed_size, size_char);
@@ -295,11 +314,14 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
} }
} else { } else {
mvwaddnstr(win, i-offset_vertical, 0, file_name, line_width); mvwaddnstr(win, i-offset_vertical, 0, file_name, line_width);
free(file_name);
} }
if (file_name != NULL) {
if (dir_content[i].status & FILE_STATUS_HOVER) { /* sometimes NULL remains, need to do deeper analysis soon */
wattroff(win, A_REVERSE); free(file_name);
} else {
printf("file_name remains NULL on %s, if this happens consistent on the same file, please inform me", dir_content[i].file_name);
volatile static int debug_print_dir;
debug_print_dir++;
} }
if (dir_content[i].status & FILE_STATUS_SELECTED) { if (dir_content[i].status & FILE_STATUS_SELECTED) {
wattroff(win, COLOR_PAIR(8)); wattroff(win, COLOR_PAIR(8));
@@ -311,3 +333,90 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
free(bg); free(bg);
} }
char update_selected_file(){
char ret = -1; /* -1 on empty or inaccessible file, 0 on unchanged file, 1 on changed file */
if (mid_content->file_name[0] == '\0') { /* only happens if the current path is either empty or inaccessible */
return ret;
}
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;
ret = 1;
} else {
ret = 0;
}
mid_content[selected_file_current].status |= FILE_STATUS_HOVER;
selected_file_last = selected_file_current;
return ret;
}
void dir_set_selected_file_current(unsigned long selected_file_current){
if (mid_content->file_name[0] != '\0') {
current_dir->selected_file_current = selected_file_current;
}
}
unsigned long dir_get_selected_file_current(){
current_dir = visited_dirs;
if (mid_content->file_name[0] == '\0') {
return 0;
}
char hit = 0;
char *path = getcwd(NULL, 0);
while(current_dir->next != NULL) {
if (strcmp(path, current_dir->path) == 0) {
hit = 1;
break;
}
current_dir = current_dir->next;
}
if (hit == 0) {
current_dir->next = malloc(sizeof(linked_dir));
current_dir->next->next = NULL;
current_dir->next->path = path;
return 0;
} else {
free(path);
return current_dir->selected_file_current;
}
}
void dir_init(){
visited_dirs = malloc(sizeof(linked_dir));
visited_dirs->path = getcwd(NULL, 0);
visited_dirs->selected_file_current = 0;
visited_dirs->next = NULL;
current_dir = visited_dirs;
}
void recursive_delete(file current_file){
if (current_file.file_type & FILE_TYPE_DIR) {
unsigned int file_modifiers_tmp = file_modifiers;
file_modifiers |= FILE_MODIFIERS_HIDDEN_FILES;
unsigned long current_file_count = get_dir_size(current_file.file_name);
if (current_file_count != 0) {
file *current_dir = malloc(current_file_count * sizeof(file));
memset(current_dir, '\0', current_file_count * sizeof(file));
get_dir_content(current_file.file_name, &current_file_count, current_dir);
if (chdir(current_file.file_name) != 0) {
return;
}
unsigned long i;
for (i = 0; i < current_file_count; i++) {
recursive_delete(current_dir[i]);
free(current_dir[i].file_name);
}
free(current_dir);
if (chdir("..") != 0) {
return;
}
}
remove(current_file.file_name);
file_modifiers = file_modifiers_tmp;
} else {
remove(current_file.file_name);
}
}

11
dir.h
View File

@@ -3,6 +3,11 @@
#include "dir.c" #include "dir.c"
#endif #endif
extern unsigned long get_dir_size(char *path); unsigned long get_dir_size(char *path);
extern void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content); 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); void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file *dir_content);
char update_selected_file();
void dir_set_selected_file_current(unsigned long selected_file_current);
unsigned long dir_get_selected_file_current();
void dir_init();
void recursive_delete(file current_file);

View File

@@ -1,9 +1,14 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "backend.h"
#include "backend.h"
#include "defines.h"
#include "config.h"
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
static FILE *ueberzug = NULL; static FILE *ueberzug = NULL;
#endif
extern unsigned int terminal_height; extern unsigned int terminal_height;
extern unsigned int terminal_width; extern unsigned int terminal_width;
char previewd; char previewd;
@@ -44,17 +49,20 @@ char* preview_file(char *file_name, unsigned long file_size){
char *mime = get_mimetype(file_name); char *mime = get_mimetype(file_name);
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
images_clear();
#endif
if (strstr(mime, "text")) { if (strstr(mime, "text")) {
file_buffer = text(file_name, &file_size); file_buffer = text(file_name, &file_size);
} else { #if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
} else if (strstr(mime, "image")) {
file_buffer = generic(file_name); file_buffer = generic(file_name);
}
if (strstr(mime, "image")) {
images_print(file_name); images_print(file_name);
previewd = 1; previewd = 1;
} else { #endif
images_clear(); } else {
file_buffer = generic(file_name);
} }
free(mime); free(mime);
@@ -63,37 +71,19 @@ char* preview_file(char *file_name, unsigned long file_size){
} }
char* text(char *path, unsigned long *file_size){ char* text(char *path, unsigned long *file_size){
char *file_buffer = malloc(*file_size + 1); unsigned long size = (terminal_width/2) * terminal_height;
if (size > *file_size) {
size = *file_size;
}
char *file_buffer = malloc(size + 1);
FILE *fp = fopen(path, "r"); FILE *fp = fopen(path, "r");
if (fread(file_buffer, *file_size, 1, fp) != 0) { if (fread(file_buffer, size, 1, fp) != 0) {
file_buffer[*file_size] = '\0'; file_buffer[size] = '\0';
return file_buffer; return file_buffer;
} else { } else {
return "failed reading file"; return "failed reading file";
} }
} }
void images_clear() {
if (previewd == 1) {
fprintf(ueberzug, "{\"action\": \"remove\", \
\"identifier\": \"preview\"}\n");
fflush(ueberzug);
previewd = 0;
}
}
void images_print(char *file_name) {
char *path=getcwd(NULL, 0);
fprintf(ueberzug, "{\"action\": \"remove\", \
\"identifier\": \"preview\"}\n");
fprintf(ueberzug, "{\"action\":\"add\", \
\"identifier\":\"preview\", \
\"max_height\":%d, \
\"max_width\":%d, \
\"y\":0, \
\"x\":%d, \
\"path\":\"%s/%s\"}\n", terminal_height, terminal_width/2, terminal_width/2, path, file_name);
fflush(ueberzug);
free(path);
}
char* generic(char *path){ char* generic(char *path){
char *cmd = concat("file ./\"", path); char *cmd = concat("file ./\"", path);
cmd = concat(cmd, "\""); cmd = concat(cmd, "\"");
@@ -109,6 +99,38 @@ char* generic(char *path){
return "failed executing shell command \"file\""; return "failed executing shell command \"file\"";
} }
} }
void ueberzug_init(){ #if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
ueberzug = popen("ueberzug layer -s ", "w"); void images_clear(){
if (previewd == 1) {
fprintf(ueberzug, "{\"action\": \"remove\", \
\"identifier\": \"preview\"}\n");
fflush(ueberzug);
previewd = 0;
}
} }
void images_print(char *file_name){
char *path=getcwd(NULL, 0);
fprintf(ueberzug, "{\"action\":\"add\", \
\"identifier\":\"preview\", \
\"max_height\":%d, \
\"max_width\":%d, \
\"y\":0, \
\"x\":%d, \
\"path\":\"%s/%s\"}\n", terminal_height, terminal_width/2, terminal_width/2, path, file_name);
fflush(ueberzug);
free(path);
}
void ueberzug_init(){
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW == 2
ueberzug = popen("ueberzug layer -s ", "w");
#elif SETTINGS_UEBERZUG_IMAGE_PREVIEW == 1
ueberzug = popen("ueberzug layer -s --no-cache ", "w");
#endif
}
void ueberzug_close(){
images_clear();
pclose(ueberzug);
}
#endif

View File

@@ -1,9 +1,13 @@
#ifndef PREVIEW_GUARD #ifndef PREVIEW_GUARD
#define PREVIEW_GUARD #define PREVIEW_GUARD
#include "file_previews.c" #include "file_previews.c"
#include "config.h"
#endif #endif
char* preview_file(char *file_name, unsigned long file_size); char* preview_file(char *file_name, unsigned long file_size);
char* get_mimetype(char *path); char* get_mimetype(char *path);
void images_clear(); void images_clear();
void ueberzug_init(); void ueberzug_init();
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
void ueberzug_close();
#endif

View File

@@ -1,11 +1,15 @@
#include <curses.h> #include <curses.h>
#include <pthread.h> #include <pthread.h>
#include <dirent.h> #include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "file_previews.h" #include "file_previews.h"
#include "backend.h" #include "backend.h"
#include "defines.h" #include "defines.h"
#include "config.h" #include "config.h"
#include "dir.h"
extern volatile unsigned long selected_file_current; extern volatile unsigned long selected_file_current;
@@ -15,10 +19,11 @@ extern unsigned int file_modifiers;
extern pthread_mutex_t mutex_selection; extern pthread_mutex_t mutex_selection;
extern pthread_mutex_t mutex_rgt; extern pthread_mutex_t mutex_rgt;
extern pthread_mutex_t mutex_mid; extern pthread_mutex_t mutex_mid;
extern pthread_mutex_t mutex_btm;
extern pthread_cond_t cond_rgt;
extern file *mid_content; extern file *mid_content;
extern file *lft_content; extern file *lft_content;
extern file *rgt_content; extern file *rgt_content;
extern volatile file *file_current;
extern unsigned int terminal_height; extern unsigned int terminal_height;
extern unsigned int terminal_width; extern unsigned int terminal_width;
@@ -33,13 +38,15 @@ extern unsigned int status;
extern char *start_path; extern char *start_path;
extern char *input; extern char *input;
extern time_t *seed;
char search_buffer[255];
unsigned int timeout_time = 0; unsigned int timeout_time = 0;
unsigned int input_pass; unsigned int input_pass;
int parsed_input_number; unsigned long parsed_input_number;
yank yank_files = { 0 }; yank yank_files = { 0 };
int read_string(WINDOW *win, int y, int x, char *str); 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 void render_pass();
extern int (*order_func)(); extern int (*order_func)();
@@ -63,7 +70,7 @@ void user_interactions() {
ch = getch(); ch = getch();
if(ch != ERR) { if(ch != ERR) {
timeout(1); /* blocking timeout of getch() */ timeout(10); /* blocking timeout of getch() */
input[input_pass] = ch; input[input_pass] = ch;
mvaddstr(terminal_height-1, (terminal_width/3)*2, input); mvaddstr(terminal_height-1, (terminal_width/3)*2, input);
input_pass++; input_pass++;
@@ -76,6 +83,8 @@ void user_interactions() {
} }
binding_pass = 0; binding_pass = 0;
status |= STATUS_UPDATE_SCREEN_0; status |= STATUS_UPDATE_SCREEN_0;
} else {
timeout(100);
} }
@@ -112,7 +121,7 @@ void user_interactions() {
} }
if (status & STATUS_INPUT_MATCH) { if (status & STATUS_INPUT_MATCH) {
attron(A_UNDERLINE); attron(A_UNDERLINE);
mvaddstr(terminal_height-binding_matches-2, 0, "input\tcommand\t\t"); mvwprintw(stdscr, terminal_height-binding_matches-2, 0, "input\tcommand\t\t");
attroff(A_UNDERLINE); attroff(A_UNDERLINE);
status &= ~STATUS_INPUT_MATCH; status &= ~STATUS_INPUT_MATCH;
} else if (number_length != strlen(input)) { } else if (number_length != strlen(input)) {
@@ -140,14 +149,18 @@ int read_string(WINDOW *win, int y, int x, char *str){
if (ch == '\n') { if (ch == '\n') {
err = 0; err = 0;
break; break;
} else if (ch == 27) { /* esc key */ } else if (ch == '\t') { /* tab */
err = 1; memcpy(str + pass, mid_content[selected_file_current].file_name, strlen(mid_content[selected_file_current].file_name));
break; 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 */ } else if (ch == 127) { /* backspace */
if (pass > 0) { if (pass > 0) {
pass--; pass--;
mvwdelch(win, y, pass); mvwdelch(win, y, pass);
} }
} else if (ch == 27) { /* esc key */
err = 1;
break;
} else { } else {
mvwaddch(win, y, x +pass, ch); mvwaddch(win, y, x +pass, ch);
str[pass] = ch; str[pass] = ch;
@@ -156,97 +169,100 @@ int read_string(WINDOW *win, int y, int x, char *str){
} }
str[pass] = '\0'; str[pass] = '\0';
timeout(10); timeout(100);
curs_set(0); curs_set(0);
return err; 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;
} }
void toggle_selection(){ void select_all(){
pthread_mutex_lock(&mutex_selection); pthread_mutex_lock(&mutex_selection);
pthread_mutex_lock(&mutex_mid); pthread_mutex_lock(&mutex_mid);
mid_content[selected_file_current].status ^= FILE_STATUS_SELECTED; unsigned long i;
file_current->status ^= FILE_STATUS_SELECTED; for(i = 0; i < mid_file_count; i++) {
status |= (STATUS_UPDATE_SCREEN_MASK); mid_content[i].status ^= FILE_STATUS_SELECTED;
}
pthread_mutex_unlock(&mutex_mid); pthread_mutex_unlock(&mutex_mid);
pthread_mutex_unlock(&mutex_selection); pthread_mutex_unlock(&mutex_selection);
} }
void move_down(int passes){ void move_down(unsigned long passes){
pthread_mutex_lock(&mutex_selection); pthread_mutex_lock(&mutex_selection);
if (passes == 0) { if (passes == 0) {
passes++; passes++;
} }
/*capping the maximum file is done inside thread_mid */
selected_file_current += passes; selected_file_current += passes;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
update_selected_file();
dir_set_selected_file_current(selected_file_current);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0);
pthread_mutex_unlock(&mutex_selection); pthread_mutex_unlock(&mutex_selection);
} }
void move_up(int passes){ void move_up(unsigned long passes){
pthread_mutex_lock(&mutex_selection); pthread_mutex_lock(&mutex_selection);
if (passes == 0) { if (passes == 0) {
passes++; passes++;
} }
int i; unsigned long tmp = selected_file_current;
for (i = 0; i < passes; i++) { selected_file_current -= passes;
if (selected_file_current != 0) { if (tmp < selected_file_current) {
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); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
pthread_mutex_unlock(&mutex_selection); pthread_mutex_unlock(&mutex_selection);
} }
void move_left(int passes){ void move_left(unsigned long passes){
if (passes == 0) { if (passes == 0) {
passes++; passes++;
} }
int i; unsigned long i;
for (i = 0; i < passes; i++) { for (i = 0; i < passes; i++) {
if (chdir("..") != 0) { if (chdir("..") != 0) {
/* TODO(2025-07-09T00:30:05) fix */ /* TODO(2025-07-09T00:30:05) fix */
FAIL("move_left", "unhandled error of chdir"); 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); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
} }
void move_right(){ void move_right(){
if (file_current->file_type == FILE_TYPE_DIR || file_current->file_type == FILE_TYPE_SYMLINK) { if (mid_content->file_name[0] == '\0') {
if (chdir(file_current->file_name) != 0) { return;
}
if (mid_content[selected_file_current].file_type &= FILE_TYPE_DIR) {
if (chdir(mid_content[selected_file_current].file_name) != 0) {
FAIL("move_right", "unhandled error of chdir"); FAIL("move_right", "unhandled error of chdir");
} else {
selected_file_current = dir_get_selected_file_current();
} }
} else { } else {
unsigned long i = 0; unsigned long i = 0;
char match = 0; char match = 0;
char *mime = get_mimetype(file_current->file_name); char *mime = get_mimetype(mid_content[selected_file_current].file_name);
char *extension = strrchr(file_current->file_name, '.'); char *extension = strrchr(mid_content[selected_file_current].file_name, '.');
for (i = 0; i < file_extension_default_count; i++) { if (extension != NULL) {
if (strstr(extension, file_extension_default_cmd[i].file_extension)) { for (i = 0; i < file_extension_default_count; i++) {
char *cmd = concat(file_extension_default_cmd[i].command, " ./\""); if (strstr(extension, file_extension_default_cmd[i].file_extension)) {
cmd = concat(cmd, file_current->file_name); char *cmd = concat(file_extension_default_cmd[i].command, " ./\"");
cmd = concat(cmd, "\""); cmd = concat(cmd, mid_content[selected_file_current].file_name);
cmd = concat(cmd, "\"");
if (system(cmd) == -1) { if (system(cmd) == -1) {
/*do nothing*/ /*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;
} }
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) { if (match == 0) {
@@ -254,7 +270,7 @@ void move_right(){
if (strstr(mime, mimetype_default_cmd[i].mimetype)) { if (strstr(mime, mimetype_default_cmd[i].mimetype)) {
char *cmd = concat(mimetype_default_cmd[i].command, " ./\""); char *cmd = concat(mimetype_default_cmd[i].command, " ./\"");
cmd = concat(cmd, file_current->file_name); cmd = concat(cmd, mid_content[selected_file_current].file_name);
cmd = concat(cmd, "\""); cmd = concat(cmd, "\"");
btm_buffer = malloc(strlen(cmd)); btm_buffer = malloc(strlen(cmd));
@@ -273,27 +289,43 @@ void move_right(){
} }
free(mime); free(mime);
} }
update_selected_file();
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
} }
void toggle_hidden_files(){ void toggle_hidden_files(){
file_modifiers ^= FILE_MODIFIERS_HIDDEN_FILES; file_modifiers ^= FILE_MODIFIERS_HIDDEN_FILES;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
} }
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);
move_down(1);
}
void jump_bottom(){ void jump_bottom(){
pthread_mutex_lock(&mutex_selection); pthread_mutex_lock(&mutex_selection);
selected_file_current = 0 - 1; selected_file_current = 0 - 1;
update_selected_file();
dir_set_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 jump_top(){ void jump_top(){
pthread_mutex_lock(&mutex_selection); pthread_mutex_lock(&mutex_selection);
selected_file_current = 0; selected_file_current = 0;
update_selected_file();
dir_set_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 open_with(){ void open_with(){
btm_buffer = concat("open \"", file_current->file_name); pthread_mutex_lock(&mutex_btm);
btm_buffer = concat("open \"", mid_content[selected_file_current].file_name);
btm_buffer = concat(btm_buffer, "\" with:"); btm_buffer = concat(btm_buffer, "\" with:");
status |= STATUS_UPDATE_SCREEN_0; status |= STATUS_UPDATE_SCREEN_0;
@@ -314,9 +346,13 @@ void open_with(){
if (!err) { if (!err) {
char *cmd = concat(str, " ./\""); char *cmd = concat(str, " ./\"");
cmd = concat(cmd, file_current->file_name); cmd = concat(cmd, mid_content[selected_file_current].file_name);
cmd = concat(cmd, "\""); cmd = concat(cmd, "\"");
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
images_clear();
#endif
if (system(cmd) == -1) { if (system(cmd) == -1) {
FAIL("open_with", "creating subcommand failed unhandled"); FAIL("open_with", "creating subcommand failed unhandled");
} }
@@ -332,8 +368,9 @@ void open_with(){
} }
void rename_hovered(){ void rename_hovered(){
pthread_mutex_lock(&mutex_btm);
btm_buffer = concat("rename \"", file_current->file_name); btm_buffer = concat("rename \"", mid_content[selected_file_current].file_name);
btm_buffer = concat(btm_buffer, "\" to:"); btm_buffer = concat(btm_buffer, "\" to:");
status |= STATUS_UPDATE_SCREEN_0; status |= STATUS_UPDATE_SCREEN_0;
@@ -341,7 +378,9 @@ void rename_hovered(){
mvwin(win_b, terminal_height-6, 0); mvwin(win_b, terminal_height-6, 0);
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/ wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
pthread_mutex_unlock(&mutex_btm);
render_pass(); render_pass();
pthread_mutex_lock(&mutex_btm);
unsigned long local_height; unsigned long local_height;
local_height = getmaxy(win_b); local_height = getmaxy(win_b);
@@ -353,7 +392,7 @@ void rename_hovered(){
if (!err) { if (!err) {
char *cmd = concat("mv ./\"", file_current->file_name); char *cmd = concat("mv ./\"", mid_content[selected_file_current].file_name);
cmd = concat(cmd, "\" ./\""); cmd = concat(cmd, "\" ./\"");
cmd = concat(cmd, str); cmd = concat(cmd, str);
cmd = concat(cmd, "\""); cmd = concat(cmd, "\"");
@@ -363,6 +402,7 @@ void rename_hovered(){
}; };
btm_buffer = cmd; btm_buffer = cmd;
} }
pthread_mutex_unlock(&mutex_btm);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
@@ -371,7 +411,8 @@ void rename_hovered(){
} }
void delete(){ void delete(){
pthread_mutex_lock(&mutex_btm);
unsigned int i = 0; unsigned int i = 0;
unsigned int hits = 0; unsigned int hits = 0;
char *file_str = " "; char *file_str = " ";
@@ -387,7 +428,7 @@ void delete(){
if (hits) { if (hits) {
btm_buffer = concat("delete:", file_str); btm_buffer = concat("delete:", file_str);
} else { } else {
btm_buffer = concat("delete: \"", file_current->file_name); btm_buffer = concat("delete: \"", mid_content[selected_file_current].file_name);
btm_buffer = concat(btm_buffer, "\""); btm_buffer = concat(btm_buffer, "\"");
} }
@@ -400,8 +441,9 @@ void delete(){
mvwin(win_b, terminal_height-6, 0); mvwin(win_b, terminal_height-6, 0);
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/ wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
pthread_mutex_unlock(&mutex_btm);
render_pass(); render_pass();
pthread_mutex_lock(&mutex_btm);
timeout(-1); /* negative numbers block until enter is pressed */ timeout(-1); /* negative numbers block until enter is pressed */
/* TODO(2025-06-22T01:24:30) fix fixed buffer size */ /* TODO(2025-06-22T01:24:30) fix fixed buffer size */
@@ -409,32 +451,22 @@ void delete(){
if (ch == 'y' || ch == 'Y') { 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 */ /* TODO(2025-06-30T02:27:06) IMPORTANT: this really fucks up when the file has a quotation mark in its name */
int error;
if (hits) { if (hits) {
int j = 2;
for (i = 0; i < mid_file_count; i++) { for (i = 0; i < mid_file_count; i++) {
if (mid_content[i].status & FILE_STATUS_SELECTED) { if (mid_content[i].status & FILE_STATUS_SELECTED) {
error = remove(mid_content[i].file_name); recursive_delete(mid_content[i]);
if (error != 0) {
mvaddstr(terminal_height-j, 0, "could not delete: " );
mvaddstr(terminal_height-j, strlen("could not delete: "), mid_content[i].file_name);
j++;
}
} }
} }
free(btm_buffer); free(btm_buffer);
btm_buffer = concat("deleted: ", file_str); btm_buffer = concat("deleted: ", file_str);
} else { } else {
free(btm_buffer); free(btm_buffer);
error = remove(file_current->file_name); if (mid_content[selected_file_current].file_type & FILE_TYPE_DIR) {
if (error != 0) { recursive_delete(mid_content[selected_file_current]);
mvaddstr(terminal_height-2, 0, "could not delete: " ); }
mvaddstr(terminal_height-2, strlen("could not delete: "), mid_content[i].file_name); remove(mid_content[selected_file_current].file_name);
btm_buffer = " "; btm_buffer = concat("deleted: \"", mid_content[selected_file_current].file_name);
} else { btm_buffer = concat(btm_buffer, "\"");
btm_buffer = concat("deleted: \"", file_current->file_name);
btm_buffer = concat(btm_buffer, "\"");
}
} }
/*system(cmd);*/ /*system(cmd);*/
@@ -450,15 +482,22 @@ void delete(){
if (hits) { if (hits) {
free(file_str); free(file_str);
} }
pthread_mutex_unlock(&mutex_btm);
} }
void makedir(){ void makedir(){
btm_buffer = "create dir: "; pthread_mutex_lock(&mutex_btm);
status |= STATUS_UPDATE_SCREEN_0;
werase(win_b); werase(win_b);
mvwin(win_b, terminal_height-6, 0); mvwin(win_b, terminal_height-6, 0);
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/ wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
memset(btm_buffer, ' ', terminal_width);
memcpy(btm_buffer, "create dir: ", strlen("create dir: "));
status |= STATUS_UPDATE_SCREEN_0;
pthread_mutex_unlock(&mutex_btm);
render_pass(); render_pass();
pthread_mutex_lock(&mutex_btm);
unsigned long local_height; unsigned long local_height;
local_height = getmaxy(win_b); local_height = getmaxy(win_b);
@@ -475,14 +514,20 @@ void makedir(){
} }
free(str); free(str);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
pthread_mutex_unlock(&mutex_btm);
} }
void makefile(){ void makefile(){
btm_buffer = "create file: "; pthread_mutex_lock(&mutex_btm);
memcpy(btm_buffer, "create file: ", strlen("create file: "));
status |= STATUS_UPDATE_SCREEN_0; status |= STATUS_UPDATE_SCREEN_0;
werase(win_b); werase(win_b);
mvwin(win_b, terminal_height-6, 0); mvwin(win_b, terminal_height-6, 0);
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/ wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
pthread_mutex_unlock(&mutex_btm);
render_pass(); render_pass();
pthread_mutex_lock(&mutex_btm);
unsigned long local_height; unsigned long local_height;
local_height = getmaxy(win_b); local_height = getmaxy(win_b);
@@ -498,23 +543,36 @@ void makefile(){
fclose(fp); fclose(fp);
} }
free(str); free(str);
pthread_mutex_unlock(&mutex_btm);
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
} }
void update(){ void update(){
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
} }
void enter_shell(int passes, int index){ void enter_shell(unsigned long passes, int index){
(void)passes;
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
images_clear();
#endif
endwin();
if (system(key_binding[index].black_magic) != 0) { if (system(key_binding[index].black_magic) != 0) {
/*do nothing*/ /*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 not_implemented(unsigned long passes, int index){
mvaddstr(terminal_height-1, 0, key_binding[index].comment); (void)passes;
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"); 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 jump_to_dir(unsigned long passes, int index){
(void)passes;
char *ch = (char*)key_binding[index].black_magic; char *ch = (char*)key_binding[index].black_magic;
char slash = 0; char slash = 0;
unsigned int env_len = 0; unsigned int env_len = 0;
@@ -556,6 +614,8 @@ void jump_to_dir(int passes, int index){
} }
if (chdir(path) != 0) { if (chdir(path) != 0) {
FAIL("jump_to_dir", "jumping to black_magic in config.h failed"); 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*/ /*env_parsed shall not be modified (read: free'd) - the man page*/
@@ -567,12 +627,21 @@ void jump_to_dir(int passes, int index){
} }
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
} }
void order_by(int passes, int index){ void order_by(unsigned long passes, int index){
(void)passes;
free(seed);
seed = NULL;
seed = malloc(sizeof(time_t));
*seed = time(NULL);
order_func = key_binding[index].black_magic; order_func = key_binding[index].black_magic;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
} }
void cmd_on_selected(int passes, int index){ void cmd_on_selected(unsigned long passes, int index){
(void)passes;
pthread_mutex_lock(&mutex_btm);
unsigned int i = 0; unsigned int i = 0;
unsigned int hits = 0; unsigned int hits = 0;
char *file_str = " "; char *file_str = " ";
@@ -589,7 +658,7 @@ void cmd_on_selected(int passes, int index){
btm_buffer = concat(key_binding[index].black_magic, file_str); btm_buffer = concat(key_binding[index].black_magic, file_str);
} else { } else {
btm_buffer = concat(key_binding[index].black_magic, "\""); btm_buffer = concat(key_binding[index].black_magic, "\"");
btm_buffer = concat(btm_buffer, file_current->file_name); btm_buffer = concat(btm_buffer, mid_content[selected_file_current].file_name);
btm_buffer = concat(btm_buffer, "\""); btm_buffer = concat(btm_buffer, "\"");
} }
@@ -602,7 +671,9 @@ void cmd_on_selected(int passes, int index){
mvwin(win_b, terminal_height-6, 0); mvwin(win_b, terminal_height-6, 0);
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/ wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
pthread_mutex_unlock(&mutex_btm);
render_pass(); render_pass();
pthread_mutex_lock(&mutex_btm);
timeout(-1); /* negative numbers block until enter is pressed */ timeout(-1); /* negative numbers block until enter is pressed */
@@ -626,12 +697,12 @@ void cmd_on_selected(int passes, int index){
} }
} }
free(btm_buffer); free(btm_buffer);
btm_buffer = concat("completed: ", key_binding[index].black_magic); memcpy(btm_buffer, "completed: ", strlen("completed: "));
} else { } else {
free(btm_buffer); free(btm_buffer);
free(cmd); free(cmd);
cmd = concat((char*)key_binding[index].black_magic, " \""); cmd = concat((char*)key_binding[index].black_magic, " \"");
cmd = concat(cmd, file_current->file_name); cmd = concat(cmd, mid_content[selected_file_current].file_name);
cmd = concat(cmd, "\""); cmd = concat(cmd, "\"");
if (system(cmd) != 0) { if (system(cmd) != 0) {
/*do nothing*/ /*do nothing*/
@@ -644,7 +715,7 @@ void cmd_on_selected(int passes, int index){
} else { } else {
free(btm_buffer); free(btm_buffer);
btm_buffer = "cancled deletion"; memcpy(btm_buffer, "cancled deletion", strlen("cancled deletion"));
} }
timeout(10); timeout(10);
@@ -653,16 +724,41 @@ void cmd_on_selected(int passes, int index){
if (hits) { if (hits) {
free(file_str); free(file_str);
} }
pthread_mutex_unlock(&mutex_btm);
} }
void yank_file(int passes, int index){ void yank_text(unsigned long passes, int index){
(void)passes;
char *cmd;
if (strncmp((char*)key_binding[index].black_magic, "path", 4) == 0) {
char *path=getcwd(NULL, 0);
cmd = concat("echo \"", path);
cmd = concat(cmd, "/");
cmd = concat(cmd, mid_content[selected_file_current].file_name);
cmd = concat(cmd, "\" | ");
cmd = concat(cmd, clipboard_cmd);
free(path);
} else {
cmd = concat("echo \"", mid_content[selected_file_current].file_name);
cmd = concat(cmd, "\" | ");
cmd = concat(cmd, clipboard_cmd);
}
if (system(cmd) == -1) {
/*do nothing*/
}
free(cmd);
}
void yank_file(unsigned long passes, int index){
(void)passes;
unsigned long i; unsigned long i;
if (yank_files.status & YANK_IS_USED) { if (yank_files.status & YANK_IS_USED) {
free(yank_files.path);
for (i = 0; i < yank_files.count; i++) { for (i = 0; i < yank_files.count; i++) {
free(yank_files.list[i]); free(yank_files.list[i]);
} }
free(yank_files.list); free(yank_files.list);
free(yank_files.path);
yank_files.count = 0; yank_files.count = 0;
yank_files.status = 0;
} }
yank_files.path=getcwd(NULL, 0); yank_files.path=getcwd(NULL, 0);
yank_files.count = 0; yank_files.count = 0;
@@ -679,7 +775,7 @@ void yank_file(int passes, int index){
strcpy(*yank_files.list, mid_content[selected_file_current].file_name); strcpy(*yank_files.list, mid_content[selected_file_current].file_name);
} else { } else {
yank_files.list = malloc(yank_files.count * sizeof(char*)); yank_files.list = malloc(yank_files.count * sizeof(char*));
for (i = 0; i < mid_file_count && i < yank_files.count; i++) { for (i = 0; i < mid_file_count; i++) {
if (mid_content[i].status & FILE_STATUS_SELECTED) { if (mid_content[i].status & FILE_STATUS_SELECTED) {
*yank_files.list = malloc(strlen(mid_content[i].file_name)+1); *yank_files.list = malloc(strlen(mid_content[i].file_name)+1);
strcpy(*yank_files.list, mid_content[i].file_name); strcpy(*yank_files.list, mid_content[i].file_name);
@@ -703,24 +799,161 @@ void paste(){
/*TODO(2025-08-14T22:10:44) escape path*/ /*TODO(2025-08-14T22:10:44) escape path*/
char *cmd; char *cmd;
if (yank_files.status & YANK_COPY) { if (yank_files.status & YANK_COPY) {
cmd = concat("false | cp -r -i ", yank_files.path); cmd = concat("false | cp -riv ", yank_files.path);
} else { } else {
cmd = concat("mv ", yank_files.path); cmd = concat("mv ", yank_files.path);
} }
cmd = concat(cmd, "/"); cmd = concat(cmd, "/");
cmd = concat(cmd, *yank_files.list); cmd = concat(cmd, *yank_files.list);
cmd = concat(cmd, " ./"); cmd = concat(cmd, " ./");
mvprintw(i, 0, cmd); cmd = concat(cmd, *yank_files.list);
if (system(cmd) != 0) { cmd = concat(cmd, " 2>&1");
cmd = concat(cmd, *yank_files.list); char *line = malloc(255);
while (1) { FILE *cmd_open;
while (1) {
cmd_open = popen(cmd, "r");
if (fgets(line, 255, cmd_open) == 0) {
break;
}
if (strstr(line, "are the same file")) {
cmd[strlen(cmd)-strlen(" 2>&1")] = '\0';
cmd = concat(cmd, "_"); cmd = concat(cmd, "_");
if (system(cmd) == 0) { cmd = concat(cmd, " 2>&1");
break; } else if ((strstr(line, "overwrite"))) {
} } cmd[strlen(cmd)-strlen(" 2>&1")] = '\0';
cmd = concat(cmd, "_");
cmd = concat(cmd, " 2>&1");
} else if ((strstr(line, "No such file or directory"))) {
pclose(cmd_open);
break;
} else if (pclose(cmd_open) == 0) {
break;
}
pclose(cmd_open);
} }
free(cmd);
yank_files.list++; yank_files.list++;
} }
yank_files.list -= yank_files.count; yank_files.list -= yank_files.count;
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL); status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
} }
void search(){
pthread_mutex_lock(&mutex_btm);
unsigned long local_height;
local_height = getmaxy(win_b);
memset(search_buffer, '\0', 255);
pthread_mutex_unlock(&mutex_btm);
render_pass();
pthread_mutex_lock(&mutex_btm);
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);
pthread_mutex_unlock(&mutex_btm);
}
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;
}
}
}
void jmp_file_index(){
char *index = malloc(255);
memset(index, ' ', 255);
index[254] = '\0';
unsigned long local_height;
local_height = getmaxy(win_b);
read_string(win_b, local_height - 1, 0, index);
unsigned long new_index = 0;
while((*index >= '0') && (*index <= '9')) {
new_index = (new_index * 10) + (*index - '0');
index++;
}
if (new_index > mid_file_count) {
selected_file_current = mid_file_count;
} else {
selected_file_current = new_index;
}
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0);
update_selected_file();
}

View File

@@ -7,10 +7,11 @@
void user_interactions(); void user_interactions();
void quit_program(); void quit_program();
void toggle_selection(); void toggle_selection();
void select_all();
void move_right(); void move_right();
void move_up(int passes); void move_up(unsigned long passes);
void move_down(int passes); void move_down(unsigned long passes);
void move_left(int passes); void move_left(unsigned long passes);
void jump_bottom(); void jump_bottom();
void jump_top(); void jump_top();
void toggle_hidden_files(); void toggle_hidden_files();
@@ -20,10 +21,15 @@ void delete();
void makedir(); void makedir();
void makefile(); void makefile();
void update(); void update();
void enter_shell(int passes, int index); void enter_shell(unsigned long passes, int index);
void not_implemented(int passes, int index); void not_implemented(unsigned long passes, int index);
void jump_to_dir(int passes, int index); void jump_to_dir(unsigned long passes, int index);
void order_by(int passes, int index); void order_by(unsigned long passes, int index);
void cmd_on_selected(int passes, int index); void cmd_on_selected(unsigned long passes, int index);
void yank_file(int passes, int index); void yank_text(unsigned long passes, int index);
void yank_file(unsigned long passes, int index);
void paste(); void paste();
void search();
void search_next();
void search_previous();
void jmp_file_index();

59
main.c
View File

@@ -4,6 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <locale.h> #include <locale.h>
#include <time.h>
#include "defines.h" #include "defines.h"
#include "threading.h" #include "threading.h"
@@ -20,6 +21,7 @@ unsigned int settings;
unsigned int file_modifiers; unsigned int file_modifiers;
unsigned int status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY); unsigned int status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
char *start_path; char *start_path;
time_t *seed;
WINDOW *win_t; WINDOW *win_t;
WINDOW *win_b; WINDOW *win_b;
@@ -58,7 +60,17 @@ int main(){
char threading = 0; char threading = 0;
terminal_width_empty_line = malloc(terminal_width); terminal_width_empty_line = malloc(terminal_width);
#if SETTINGS_RELOAD_DIR_DELTA != 0
time_t t;
time_t dt;
time(&t);
#endif
pthread_create(&thread_t, NULL, thread_top, &status); /*top bar*/
pthread_create(&thread_l, NULL, thread_lft, &status); /*parent_content slash win_l*/
pthread_create(&thread_m, NULL, thread_mid, &status); /*current_content slash win_m*/
pthread_create(&thread_r, NULL, thread_rgt, &status); /*child_content slash win_r*/
pthread_create(&thread_b, NULL, thread_btm, &status); /*bottom bar*/
while(!(status & STATUS_QUIT_PROGRAM)){ while(!(status & STATUS_QUIT_PROGRAM)){
getmaxyx(stdscr, terminal_height, terminal_width); getmaxyx(stdscr, terminal_height, terminal_width);
@@ -68,28 +80,14 @@ int main(){
temp_width = terminal_width; temp_width = terminal_width;
temp_heigth = terminal_height; temp_heigth = terminal_height;
} }
if (status & STATUS_RUN_BACKEND && threading) {
pthread_cancel(thread_b);
pthread_cancel(thread_r);
pthread_cancel(thread_m);
pthread_cancel(thread_l);
pthread_cancel(thread_t);
}
if (threading) { if (threading) {
status &= ~(STATUS_RELOAD_DIRECTORY); status &= ~(STATUS_RELOAD_DIRECTORY | STATUS_DELTA_TIME);
pthread_join(thread_t, NULL);
pthread_join(thread_l, NULL);
pthread_join(thread_m, NULL);
pthread_join(thread_b, NULL);
pthread_join(thread_r, NULL);
threading = 0; threading = 0;
} }
if (status & STATUS_RUN_BACKEND) { if (status & STATUS_RUN_BACKEND) {
pthread_create(&thread_t, NULL, thread_top, &status); /*top bar*/ pthread_cond_signal(&cond_top);
pthread_create(&thread_l, NULL, thread_lft, &status); /*parent_content slash win_l*/ pthread_cond_signal(&cond_mid);
pthread_create(&thread_m, NULL, thread_mid, &status); /*current_content slash win_m*/ pthread_cond_signal(&cond_lft);
pthread_create(&thread_b, NULL, thread_btm, &status); /*bottom bar*/
pthread_create(&thread_r, NULL, thread_rgt, &status); /*child_content slash win_r*/
status &= ~(STATUS_RUN_BACKEND); status &= ~(STATUS_RUN_BACKEND);
status |= STATUS_UPDATE_SCREEN_0; status |= STATUS_UPDATE_SCREEN_0;
threading = 1; threading = 1;
@@ -99,17 +97,30 @@ int main(){
render_pass(); render_pass();
#if SETTINGS_RELOAD_DIR_DELTA != 0
time(&dt);
if (dt - t >= SETTINGS_RELOAD_DIR_DELTA) {
time(&t);
status |= (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY | STATUS_DELTA_TIME);
}
#endif
} }
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
ueberzug_close();
#endif
threading_free(); threading_free();
free(start_path); free(start_path);
if (threading) { /*
if (threading) {
pthread_join(thread_l, NULL); pthread_join(thread_l, NULL);
pthread_join(thread_r, NULL); pthread_join(thread_r, NULL);
pthread_join(thread_m, NULL); pthread_join(thread_m, NULL);
pthread_join(thread_t, NULL); pthread_join(thread_t, NULL);
pthread_join(thread_b, NULL); pthread_join(thread_b, NULL);
} }
*/
delwin(win_l); delwin(win_l);
delwin(win_m); delwin(win_m);
@@ -117,9 +128,9 @@ int main(){
delwin(win_t); delwin(win_t);
delwin(win_b); delwin(win_b);
noraw(); noraw();
endwin();
curs_set(1);
echo(); echo();
curs_set(1);
endwin();
return 0; return 0;
} }
@@ -190,11 +201,17 @@ void init() {
threading_init(); /* found in threading.c */ threading_init(); /* found in threading.c */
colors_init(); /* in colors.c */ colors_init(); /* in colors.c */
dir_init(); /*in dir.c */
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
ueberzug_init(); /* in file_previews.c */ ueberzug_init(); /* in file_previews.c */
#endif
ESCDELAY = 10; ESCDELAY = 10;
char *start_path = getcwd(NULL, 0); char *start_path = getcwd(NULL, 0);
setenv("START_PATH", start_path, 0); setenv("START_PATH", start_path, 0);
free(start_path); free(start_path);
seed = malloc(sizeof(time_t));
*seed = time(NULL);
} }

277
sorting.c
View File

@@ -1,19 +1,24 @@
#include <curses.h> #include <curses.h>
#include <dirent.h> #include <dirent.h>
#include <strings.h>
#include <stdlib.h> #include <stdlib.h>
#include <strings.h>
#include <string.h> #include <string.h>
#include <pthread.h>
#include <unistd.h>
extern unsigned int settings; #include "defines.h"
extern unsigned int file_modifiers;
extern time_t *seed;
int skip_hidden_files(const struct dirent *entry){ int skip_hidden_files(const struct dirent *entry){
if (entry->d_name[0] == '.') { if (entry->d_name[0] == '.') {
return 0; return 0;
} }
return 1; return 1;
} }
int skip_dot(const struct dirent *entry){ int skip_dot(const struct dirent *entry){
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
return 0; return 0;
} }
@@ -21,136 +26,178 @@ int skip_dot(const struct dirent *entry){
} }
int sort_natural(const void *file0, const void *file1){ int sort_natural(const void *file0, const void *file1){
unsigned char file_type0 = ((file*)file0)->file_type;
unsigned char file_type1 = ((file*)file1)->file_type;
char weight = 0;
if (file_type0 == FILE_TYPE_DIR || file_type0 == FILE_TYPE_SYMLINK) {
weight |= 1;
}
if (file_type1 == FILE_TYPE_DIR || file_type1 == FILE_TYPE_SYMLINK) {
weight |= 2;
}
if (weight == 0 || weight == 3) {
char *file_name0 = ((file*)file0)->file_name;
char *file_name1 = ((file*)file1)->file_name;
return strcasecmp(file_name0, file_name1);
} else {
if (file_type0 > file_type1) {
return 1;
} else if (file_type0 < file_type1) {
return -1;
} else {
char *file_name0 = ((file*)file0)->file_name;
char *file_name1 = ((file*)file1)->file_name;
return strcasecmp(file_name0, file_name1);
}
}
}
int sort_alpha(const void *file0, const void *file1){
char *file_name0 = ((file*)file0)->file_name;
char *file_name1 = ((file*)file1)->file_name;
return strcmp(file_name0, file_name1);
}
int sort_random(const void *file0, const void *file1){
unsigned char file_type0 = ((file*)file0)->file_type;
unsigned char file_type1 = ((file*)file1)->file_type;
static int seed = 0;
static int random = 0;
if (seed == 0) { if ((((file*)file0)->file_type & FILE_TYPE_DIR) && !(((file*)file1)->file_type & FILE_TYPE_DIR)) {
seed = rand();
}
if (random == 0) {
random = seed;
}
char weight = 0;
if (file_type0 == FILE_TYPE_DIR || file_type0 == FILE_TYPE_SYMLINK) {
weight |= 1;
}
if (file_type1 == FILE_TYPE_DIR || file_type1 == FILE_TYPE_SYMLINK) {
weight |= 2;
}
if (weight == 0 || weight == 3) {
random = random > 1;
if ((random & 2) == 2) {
return -1; return -1;
} else { }
if (random & 1){ if (!(((file*)file0)->file_type & FILE_TYPE_DIR) && (((file*)file1)->file_type & FILE_TYPE_DIR)) {
return 1;
} else {
return 0;
}
}
} else {
if (file_type0 > file_type1) {
return 1; return 1;
} else if (file_type0 < file_type1) { }
return -1; const unsigned char *a = (unsigned char*)((file*)file0)->file_name;
} else { const unsigned char *b = (unsigned char*)((file*)file1)->file_name;
random = random > 1;
if ((random & 2) == 2) { long parsed_number0 = 0;
return -1; long parsed_number1 = 0;
} else { char is_num = 0;
if (random & 1){ char result = 0;
return 1;
do {
if ((*a <= '9') && (*a >= '0')) {
parsed_number0 = 0;
do {
parsed_number0 = (parsed_number0 * 10) + (*a - '0');
a++;
} while((*a <= '9') && (*a >= '0'));
is_num |= 1;
}
if ((*b <= '9') && (*b >= '0')) {
parsed_number1 = 0;
do {
parsed_number1 = (parsed_number1 * 10) + (*b - '0');
b++;
} while((*b <= '9') && (*b >= '0'));
is_num |= 2;
}
if (is_num) {
if (is_num == 1) {
if (*b > '9') {
result = -1;
} else { } else {
return 0; result = 1;
}
break;
} else if (is_num == 2) {
if (*a > '9') {
result = 1;
} else {
result = -1;
}
break;
} else {
if (parsed_number0 > parsed_number1) {
result = 1;
break;
} else if (parsed_number0 < parsed_number1) {
result = -1;
break;
} }
} }
/* those breaks are not set here, due to the possibillity that both numbers are equal
* in which case the comparison should continue */
is_num = 0;
}
unsigned char aa = ((*a >= 'A') && (*a <= 'Z')) ? (*a | ' ') : *a;
unsigned char bb = ((*b >= 'A') && (*b <= 'Z')) ? (*b | ' ') : *b;
/*using a simple aa - bb would occasionaly cause underflows with wide chars*/
result = ((aa == bb) ? 0 : ((aa > bb) ? 1 : -1 ));
a++;
b++;
} while (result == 0);
return result;
}
int sort_alpha(const void *file0, const void *file1){
if ((((file*)file0)->file_type & FILE_TYPE_DIR) && !(((file*)file1)->file_type & FILE_TYPE_DIR)) {
return -1;
}
if (!(((file*)file0)->file_type & FILE_TYPE_DIR) && (((file*)file1)->file_type & FILE_TYPE_DIR)) {
return 1;
}
return strcmp(((file*)file0)->file_name, ((file*)file1)->file_name);
}
int sort_random(const void *file0, const void *file1){
if ((((file*)file0)->file_type & FILE_TYPE_DIR) && !(((file*)file1)->file_type & FILE_TYPE_DIR)) {
return -1;
}
if (!(((file*)file0)->file_type & FILE_TYPE_DIR) && (((file*)file1)->file_type & FILE_TYPE_DIR)) {
return 1;
}
time_t num = (time_t)&seed;
time_t i;
for (i = num%2; i < 6+(((time_t)&seed)%2); i++){
num ^= *seed;
if (num%2) {
num ^= (time_t)&num;
num ^= num << (i + (((time_t)&seed)%5));
} else {
num ^= (time_t)&seed;
num ^= num >> (i + (((time_t)&num)%5));
} }
} }
return 0; num ^= getpid();
return ((num%3) - 1);
} }
int sort_type(const void *file0, const void *file1){ int sort_type(const void *file0, const void *file1){
unsigned char file_type0 = ((file*)file0)->file_type;
unsigned char file_type1 = ((file*)file1)->file_type; if ((((file*)file0)->file_type & FILE_TYPE_DIR) && !(((file*)file1)->file_type & FILE_TYPE_DIR)) {
char *file_name0 = ((file*)file0)->file_name; return -1;
char *file_name1 = ((file*)file1)->file_name; }
if (file_type0 == file_type1) { if (!(((file*)file0)->file_type & FILE_TYPE_DIR) && (((file*)file1)->file_type & FILE_TYPE_DIR)) {
return strcasecmp(file_name0, file_name1); return 1;
} else if (file_type0 == FILE_TYPE_DIR || file_type0 == FILE_TYPE_SYMLINK) { }
if (((file*)file0)->file_type > ((file*)file1)->file_type) {
return -1; return -1;
} else if (file_type1 == FILE_TYPE_DIR || file_type1 == FILE_TYPE_SYMLINK) { } else if (((file*)file0)->file_type < ((file*)file1)->file_type) {
return 1; return 1;
} else { } else {
if (file_type0 > file_type1) { return sort_natural(file0, file1);
return -1;
} else if (file_type0 < file_type1) {
return 1;
} else {
return strcasecmp(file_name0, file_name1);
}
} }
} }
int sort_size(const void *file0, const void *file1){ int sort_size(const void *file0, const void *file1){
unsigned char file_type0 = ((file*)file0)->file_type;
unsigned char file_type1 = ((file*)file1)->file_type; if ((((file*)file0)->file_type & FILE_TYPE_DIR) && !(((file*)file1)->file_type & FILE_TYPE_DIR)) {
unsigned long file_size0 = ((file*)file0)->file_size;
unsigned long file_size1 = ((file*)file1)->file_size;
char *file_name0 = ((file*)file0)->file_name;
char *file_name1 = ((file*)file1)->file_name;
if (file_type0 == file_type1) {
if (file_size0 > file_size1) {
return -1; return -1;
} else if (file_size0 < file_size1) { }
if (!(((file*)file0)->file_type & FILE_TYPE_DIR) && (((file*)file1)->file_type & FILE_TYPE_DIR)) {
return 1; return 1;
} else { }
return strcasecmp(file_name0, file_name1);
} if (((file*)file0)->file_size > ((file*)file1)->file_size) {
return -1;
} else if (((file*)file0)->file_size < ((file*)file1)->file_size) {
return 1;
} else { } else {
if (file_type0 == FILE_TYPE_DIR || file_type0 == FILE_TYPE_SYMLINK) { return sort_natural(file0, file1);
return -1; }
} else if (file_type1 == FILE_TYPE_DIR || file_type1 == FILE_TYPE_SYMLINK) { }
return 1; int sort_extension(const void *file0, const void *file1){
} else {
if (file_size0 > file_size1) { if ((((file*)file0)->file_type & FILE_TYPE_DIR) && !(((file*)file1)->file_type & FILE_TYPE_DIR)) {
return -1; return -1;
} else if (file_size0 < file_size1) { }
return 1; if (!(((file*)file0)->file_type & FILE_TYPE_DIR) && (((file*)file1)->file_type & FILE_TYPE_DIR)) {
} else { return 1;
return strcasecmp(file_name0, file_name1); }
}
} char *extension0 = strrchr(((file*)file0)->file_name, '.');
char *extension1 = strrchr(((file*)file1)->file_name, '.');
if (extension0 && extension1) {
if ((strcmp(extension0, extension1)) == 0) {
return sort_natural(file0, file1);
} else {
file f0;
file f1;
memcpy(&f0, file0, sizeof(file));
memcpy(&f1, file1, sizeof(file));
f0.file_name = extension0;
f1.file_name = extension1;
return sort_natural(&f0, &f1);
}
} else if (extension0 != NULL && extension1 == NULL) {
return 1;
} else if (extension0 == NULL && extension1 != NULL) {
return -1;
} else {
return sort_natural(file0, file1);
} }
} }

View File

@@ -6,7 +6,9 @@
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/statvfs.h>
#include "config.h"
#include "dir.h" #include "dir.h"
#include "file_previews.h" #include "file_previews.h"
@@ -18,9 +20,12 @@ pthread_mutex_t mutex_lft;
pthread_mutex_t mutex_mid; pthread_mutex_t mutex_mid;
pthread_mutex_t mutex_rgt; pthread_mutex_t mutex_rgt;
pthread_mutex_t mutex_selection; pthread_mutex_t mutex_selection;
pthread_mutex_t mutex_wait; pthread_cond_t cond_mid;
pthread_cond_t cond_wait; pthread_cond_t cond_rgt;
volatile char wait_count; /* this is used to determine how many threads are waiting for cont_wait */ pthread_cond_t cond_lft;
pthread_cond_t cond_top;
pthread_cond_t cond_btm;
file *rgt_content; file *rgt_content;
file *mid_content; file *mid_content;
@@ -29,7 +34,6 @@ char *rgt_buffer; /* used for file previews, unlike rgt_content, which is used f
char *btm_buffer; char *btm_buffer;
char *top_buffer; /* current path */ char *top_buffer; /* current path */
volatile file *file_current;
unsigned long rgt_file_count = 0; unsigned long rgt_file_count = 0;
@@ -42,232 +46,320 @@ unsigned long top_width;
volatile unsigned long selected_file_current = 0; volatile unsigned long selected_file_current = 0;
volatile unsigned long selected_file_last = 0; volatile unsigned long selected_file_last = 0;
extern unsigned int terminal_width; extern unsigned int terminal_width;
extern unsigned int status;
unsigned int btm_status;
void *thread_mid(void *data){ void *thread_mid(){
unsigned int status = *(unsigned int*)data;
pthread_mutex_lock(&mutex_mid);
while(!(status & STATUS_QUIT_PROGRAM)){
pthread_mutex_lock(&mutex_mid);
pthread_cond_wait(&cond_mid, &mutex_mid);
unsigned int local_status = status;
char *path; char *path;
if((path=getcwd(NULL, 0)) == NULL) { if((path=getcwd(NULL, 0)) == NULL) {
mid_content = malloc(sizeof(file)); mid_content = malloc(sizeof(file));
mid_content->file_name = "cannot open directory"; mid_content->file_name = "cannot open directory";
mid_file_count = 1; mid_file_count = 1;
} else {
if (status & STATUS_RELOAD_DIRECTORY) {
unsigned long i = 0;
for (i = 0; i < mid_file_count; i++) {
free(mid_content[i].file_name);
}
free(mid_content);
mid_file_count = get_dir_size(path);
if (mid_file_count > 0) {
mid_content = malloc(mid_file_count * sizeof(file));
memset(mid_content, '\0', mid_file_count * sizeof(file));
get_dir_content(path, &mid_file_count, mid_content);
} else {
selected_file_current = 0;
mid_content = malloc(sizeof(file));
mid_content->status = FILE_STATUS_DIR_EMPTY;
mid_content->file_type = 0;
mid_content->file_size = 0;
mid_content->permissions = 0;
mid_content->color_pair = 0;
mid_content->file_name = "empty dir";
file_current->file_name = mid_content->file_name;
file_current->file_size = mid_content->file_size;
file_current->file_type = mid_content->file_type;
file_current->color_pair = mid_content->color_pair;
file_current->permissions = mid_content->permissions;
file_current->status = mid_content->status;
mid_file_count = 0;
while(wait_count < 2){
/*wait for thread_rgt and thread_btm to lock*/
}
pthread_mutex_lock(&mutex_wait);
pthread_cond_broadcast(&cond_wait);
pthread_mutex_unlock(&mutex_wait);
free(path);
pthread_mutex_unlock(&mutex_mid);
pthread_exit(0);
}
}
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*/
}
pthread_mutex_lock(&mutex_wait);
pthread_cond_broadcast(&cond_wait);
pthread_mutex_unlock(&mutex_wait);
}
free(path);
pthread_mutex_unlock(&mutex_mid);
pthread_exit(0);
}
void *thread_lft(void *data){
unsigned int status = *(unsigned int*)data;
pthread_mutex_lock(&mutex_lft);
char *path;
if((path=getcwd(NULL, 0)) == NULL) {
lft_content = malloc(sizeof(file));
lft_content[0].file_name = "cannot open directory";
lft_file_count = 1;
} else {
path[strrchr(path, '/')-path] = '\0';
path[0] = '/';
if (status & STATUS_RELOAD_DIRECTORY) {
lft_file_count = get_dir_size(path);
free(lft_content);
lft_content = malloc(lft_file_count * sizeof(file));
memset(lft_content, '\0', lft_file_count * sizeof(file));
get_dir_content(path, &lft_file_count, lft_content);
}
}
free(path);
pthread_mutex_unlock(&mutex_lft);
pthread_exit(0);
}
void *thread_rgt(void *data){
unsigned int status = *(unsigned int*)data;
pthread_mutex_lock(&mutex_rgt);
pthread_mutex_lock(&mutex_wait);
wait_count++;
pthread_cond_wait(&cond_wait, &mutex_wait);
wait_count--;
pthread_mutex_unlock(&mutex_wait);
pthread_mutex_lock(&mutex_selection);
char *path = malloc(strlen(file_current->file_name) + 1);
strcpy(path, file_current->file_name);
unsigned char file_current_type = file_current->file_type;
unsigned long file_current_size = file_current->file_size;
char file_current_status = file_current->status;
pthread_mutex_unlock(&mutex_selection);
if (file_current_type == FILE_TYPE_DIR || file_current_type == FILE_TYPE_SYMLINK) {
images_clear();
unsigned long i = 0;
for (i = 0; i < rgt_file_count; i++) {
free(rgt_content[i].file_name);
}
free(rgt_content);
rgt_file_count = get_dir_size(path);
rgt_content = malloc(rgt_file_count * sizeof(file));
memset(rgt_content, '\0', rgt_file_count * sizeof(file));
get_dir_content(path, &rgt_file_count, rgt_content);
rgt_content[0].status &= ~FILE_STATUS_FILE_OPEN;
free(rgt_buffer);
rgt_buffer = malloc(sizeof(char));
rgt_buffer[0] = '\0';
} else {
unsigned long i = 0;
for (i = 0; i < rgt_file_count; i++) {
free(rgt_content[i].file_name);
}
free(rgt_content);
rgt_file_count = 0;
rgt_content = malloc(sizeof(file));
free(rgt_buffer);
if (file_current_status & FILE_STATUS_DIR_EMPTY) {
rgt_buffer = "empty dir";
} else { } else {
rgt_content->file_type = FILE_TYPE_OPEN_FILE;
rgt_content->status = FILE_STATUS_HOVER;
rgt_buffer = preview_file(path, file_current_size); if (local_status & STATUS_RELOAD_DIRECTORY) {
unsigned long i = 0;
for (i = 0; i < mid_file_count; i++) {
free(mid_content[i].file_name);
}
free(mid_content);
mid_file_count = get_dir_size(path);
if (mid_file_count != 0) {
mid_content = malloc(mid_file_count * sizeof(file));
memset(mid_content, '\0', mid_file_count * sizeof(file));
get_dir_content(path, &mid_file_count, mid_content);
} else {
selected_file_current = 0;
mid_content = malloc(sizeof(file));
mid_content->file_type = 0;
mid_content->file_size = 0;
mid_content->permissions = 0;
mid_content->color_pair = 0;
mid_content->file_name = malloc(sizeof(char));
mid_content->file_name[0] = '\0';
mid_file_count = 0;
}
pthread_mutex_lock(&mutex_selection);
update_selected_file();
pthread_mutex_unlock(&mutex_selection);
}
btm_status = local_status;
pthread_cond_signal(&cond_rgt);
pthread_cond_signal(&cond_btm);
} }
free(path);
pthread_mutex_unlock(&mutex_mid);
} }
free(path);
pthread_mutex_unlock(&mutex_rgt);
pthread_exit(0); pthread_exit(0);
} }
void *thread_top(void *data){ void *thread_lft(){
unsigned int status = *(unsigned int*)data;
pthread_mutex_lock(&mutex_top);
free(top_buffer);
char *path; while(!(status & STATUS_QUIT_PROGRAM)){
if((path=getcwd(NULL, 0)) == NULL) { pthread_mutex_lock(&mutex_lft);
top_buffer = malloc(sizeof("cannot open directory")); pthread_cond_wait(&cond_lft, &mutex_lft);
top_width = sizeof("cannot open directory"); unsigned int local_status = status;
top_buffer = "cannot open directory";
} else { char *path;
top_buffer = getcwd(NULL, 0); if((path=getcwd(NULL, 0)) == NULL) {
top_width = strlen(top_buffer); lft_content = malloc(sizeof(file));
lft_content[0].file_name = "cannot open directory";
lft_file_count = 1;
} else {
path[strrchr(path, '/')-path] = '\0';
path[0] = '/';
if (local_status & STATUS_RELOAD_DIRECTORY) {
lft_file_count = get_dir_size(path);
free(lft_content);
lft_content = malloc(lft_file_count * sizeof(file));
memset(lft_content, '\0', lft_file_count * sizeof(file));
get_dir_content(path, &lft_file_count, lft_content);
}
}
free(path);
pthread_mutex_unlock(&mutex_lft);
} }
pthread_exit(0);
free(path);
pthread_mutex_unlock(&mutex_top); }
void *thread_rgt(){
file file_current;
while(!(status & STATUS_QUIT_PROGRAM)){
pthread_mutex_lock(&mutex_rgt);
pthread_cond_wait(&cond_rgt, &mutex_rgt);
pthread_mutex_lock(&mutex_mid);
char *path;
if (mid_file_count != 0) {
path = malloc(strlen(mid_content[selected_file_current].file_name) + 1);
strcpy(path, mid_content[selected_file_current].file_name);
} else {
path = malloc(sizeof(char));
path[0] = '\0';
}
file_current.file_type = mid_content[selected_file_current].file_type;
file_current.file_size = mid_content[selected_file_current].file_size;
file_current.status = mid_content[selected_file_current].status;
pthread_mutex_unlock(&mutex_mid);
if (mid_content[selected_file_current].permissions & S_IRUSR) {
if (file_current.file_type &= FILE_TYPE_DIR) {
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
images_clear();
#endif
unsigned long i = 0;
for (i = 0; i < rgt_file_count; i++) {
if (rgt_content[i].file_name) {
free(rgt_content[i].file_name);
}
}
free(rgt_content);
rgt_file_count = get_dir_size(path);
rgt_content = malloc(rgt_file_count * sizeof(file));
memset(rgt_content, '\0', rgt_file_count * sizeof(file));
get_dir_content(path, &rgt_file_count, rgt_content);
rgt_content[0].status &= ~FILE_STATUS_FILE_OPEN;
free(rgt_buffer);
rgt_buffer = malloc(sizeof(char));
rgt_buffer[0] = '\0';
} else if ((status & STATUS_DELTA_TIME) != STATUS_DELTA_TIME) {
unsigned long i = 0;
for (i = 0; i < rgt_file_count; i++) {
if (rgt_content[i].file_name) {
free(rgt_content[i].file_name);
}
}
free(rgt_content);
rgt_file_count = 0;
rgt_content = malloc(sizeof(file));
free(rgt_buffer);
rgt_content->file_type = FILE_TYPE_OPEN_FILE;
rgt_content->status = FILE_STATUS_HOVER;
rgt_buffer = preview_file(path, file_current.file_size);
}
} else {
unsigned long i = 0;
for (i = 0; i < rgt_file_count; i++) {
if (rgt_content[i].file_name) {
free(rgt_content[i].file_name);
}
}
free(rgt_content);
rgt_file_count = 0;
rgt_content = malloc(sizeof(file));
free(rgt_buffer);
rgt_buffer = malloc(sizeof(char));
rgt_buffer[0] = '\0';
}
free(path);
pthread_mutex_unlock(&mutex_rgt);
}
pthread_exit(0); pthread_exit(0);
} }
void *thread_btm(void *data){ void *thread_top(){
unsigned int status = *(unsigned int*)data;
pthread_mutex_lock(&mutex_btm);
pthread_mutex_lock(&mutex_wait);
wait_count++;
pthread_cond_wait(&cond_wait, &mutex_wait);
wait_count--;
pthread_mutex_unlock(&mutex_wait);
free(btm_buffer); while(!(status & STATUS_QUIT_PROGRAM)){
int buffer_width = terminal_width; pthread_mutex_lock(&mutex_top);
btm_buffer = malloc(buffer_width); pthread_cond_wait(&cond_top, &mutex_top);
memset(btm_buffer, 0, buffer_width);
btm_buffer[0] = (S_ISDIR(file_current->permissions)) ? 'd' : '-'; free(top_buffer);
btm_buffer[1] = (file_current->permissions & S_IRUSR) ? 'r' : '-';
btm_buffer[2] = (file_current->permissions & S_IWUSR) ? 'w' : '-'; char *path;
btm_buffer[3] = (file_current->permissions & S_IXUSR) ? 'x' : '-'; if((path=getcwd(NULL, 0)) == NULL) {
btm_buffer[4] = (file_current->permissions & S_IRGRP) ? 'r' : '-'; top_buffer = malloc(sizeof("cannot open directory"));
btm_buffer[5] = (file_current->permissions & S_IWGRP) ? 'w' : '-'; top_width = sizeof("cannot open directory");
btm_buffer[6] = (file_current->permissions & S_IXGRP) ? 'x' : '-'; top_buffer = "cannot open directory";
btm_buffer[7] = (file_current->permissions & S_IROTH) ? 'r' : '-'; } else {
btm_buffer[8] = (file_current->permissions & S_IWOTH) ? 'w' : '-'; top_buffer = malloc(strlen(path)+1);
btm_buffer[9] = (file_current->permissions & S_IXOTH) ? 'x' : '-'; memcpy(top_buffer, path, strlen(path)+1);
top_width = strlen(top_buffer);
}
free(path);
pthread_mutex_unlock(&mutex_top);
}
pthread_exit(0);
}
void *thread_btm(){
char *path = NULL;
char *ui_btm_right_block = malloc(sizeof(char));
unsigned int ui_btm_right_block_size = 0;
unsigned int buffer_width = 0;
while(!(status & STATUS_QUIT_PROGRAM)){
pthread_mutex_lock(&mutex_btm);
pthread_cond_wait(&cond_btm, &mutex_btm);
unsigned int local_status = btm_status;
pthread_mutex_unlock(&mutex_btm); if (local_status & (STATUS_RUN_BACKEND | STATUS_RELOAD_DIRECTORY)) {
/*{{{ parse storage info; the fold of shame*/
pthread_mutex_lock(&mutex_mid);
unsigned long i;
float total_dir_size = 0;
for(i = 0; i < mid_file_count; i++) {
if ((mid_content[i].file_type & (FILE_TYPE_DIR)) != FILE_TYPE_DIR) {
total_dir_size += mid_content[i].file_size;
}
}
pthread_mutex_unlock(&mutex_mid);
if (path != NULL) {
/* sometimes NULL remains, need to do deeper analysis soon */
free(path);
path = NULL;
} else {
volatile static int debug_thread_btm;
debug_thread_btm++;
}
free(ui_btm_right_block);
path = getcwd(NULL, 0);
struct statvfs fs;
statvfs(path, &fs);
float disk_size_free = fs.f_bsize * fs.f_bavail;
float parsed_number[2] = { 0 };
char size_index[2] = { 0 };
if (total_dir_size > 1) {
size_index[0] = -1;
while (total_dir_size > 1 && size_index[0] < size_unit_count) {
parsed_number[0]=total_dir_size;
size_index[0]++;
total_dir_size /= 1024;
}
} else {
size_index[0] = 0;
parsed_number[0] = 0;
}
if (disk_size_free > 1) {
size_index[1] = -1;
while (disk_size_free > 1 && size_index[1] < size_unit_count) {
parsed_number[1]=disk_size_free;
size_index[1]++;
disk_size_free /= 1024;
}
} else {
size_index[1] = 0;
}
if (size_index[0] > 0 && size_index[1] > 0) {
ui_btm_right_block_size = snprintf(NULL, 0, "%0.2lf%c %s %0.2lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left)+1;
ui_btm_right_block = malloc(ui_btm_right_block_size);
sprintf(ui_btm_right_block, "%0.2lf%c %s %0.2lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left);
} else if (size_index[0] <= 0 && size_index[1] > 0) {
ui_btm_right_block_size = snprintf(NULL, 0, "%0.0lf%c %s %0.2lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left)+1;
ui_btm_right_block = malloc(ui_btm_right_block_size);
sprintf(ui_btm_right_block, "%0.0lf%c %s %0.2lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left);
} else if (size_index[0] > 0 && size_index[1] <= 0) {
ui_btm_right_block_size = snprintf(NULL, 0, "%0.2lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left)+1;
ui_btm_right_block = malloc(ui_btm_right_block_size);
sprintf(ui_btm_right_block, "%0.2lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left);
} else {
ui_btm_right_block_size = snprintf(NULL, 0, "%0.0lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left)+1;
ui_btm_right_block = malloc(ui_btm_right_block_size);
sprintf(ui_btm_right_block, "%0.0lf%c %s %0.0lf%c %s", parsed_number[0], size_unit[(unsigned)size_index[0]], ui_btm_current_dir_size, parsed_number[1], size_unit[(unsigned)size_index[1]], ui_btm_text_storage_left);
}
/*}}}*/
}
if (buffer_width != terminal_width) {
buffer_width = terminal_width;
free(btm_buffer);
btm_buffer = malloc(buffer_width);
memset(btm_buffer, ' ', buffer_width/2);
}
memset(btm_buffer + (buffer_width/2), ' ', buffer_width/2);
btm_buffer[buffer_width] = '\0';
memcpy(btm_buffer + buffer_width - ui_btm_right_block_size, ui_btm_right_block, ui_btm_right_block_size);
btm_buffer[0] = (S_ISDIR(mid_content[selected_file_current].permissions)) ? 'd' : '-';
btm_buffer[1] = (mid_content[selected_file_current].permissions & S_IRUSR) ? 'r' : '-';
btm_buffer[2] = (mid_content[selected_file_current].permissions & S_IWUSR) ? 'w' : '-';
btm_buffer[3] = (mid_content[selected_file_current].permissions & S_IXUSR) ? 'x' : '-';
btm_buffer[4] = (mid_content[selected_file_current].permissions & S_IRGRP) ? 'r' : '-';
btm_buffer[5] = (mid_content[selected_file_current].permissions & S_IWGRP) ? 'w' : '-';
btm_buffer[6] = (mid_content[selected_file_current].permissions & S_IXGRP) ? 'x' : '-';
btm_buffer[7] = (mid_content[selected_file_current].permissions & S_IROTH) ? 'r' : '-';
btm_buffer[8] = (mid_content[selected_file_current].permissions & S_IWOTH) ? 'w' : '-';
btm_buffer[9] = (mid_content[selected_file_current].permissions & S_IXOTH) ? 'x' : '-';
pthread_mutex_unlock(&mutex_btm);
}
pthread_exit(0); pthread_exit(0);
} }
@@ -293,11 +385,6 @@ void threading_init(){
rgt_content->file_name = malloc(sizeof(char)); rgt_content->file_name = malloc(sizeof(char));
rgt_content->file_name[0] = '\0'; rgt_content->file_name[0] = '\0';
file_current = malloc(sizeof(file));
file_current->file_type = 0;
file_current->file_size = 0;
file_current->file_name = malloc(sizeof(char));
file_current->file_name[0] = '\0';
volatile char vol; /* needed to make sure higher optimization steps dont move these around */ volatile char vol; /* needed to make sure higher optimization steps dont move these around */
vol = pthread_mutex_init(&mutex_top, NULL); vol = pthread_mutex_init(&mutex_top, NULL);
@@ -306,8 +393,11 @@ void threading_init(){
vol = pthread_mutex_init(&mutex_btm, NULL); vol = pthread_mutex_init(&mutex_btm, NULL);
vol = pthread_mutex_init(&mutex_rgt, NULL); vol = pthread_mutex_init(&mutex_rgt, NULL);
vol = pthread_mutex_init(&mutex_selection, NULL); vol = pthread_mutex_init(&mutex_selection, NULL);
vol = pthread_mutex_init(&mutex_wait, NULL); vol = pthread_cond_init(&cond_rgt, NULL);
vol = pthread_cond_init(&cond_wait, NULL); vol = pthread_cond_init(&cond_lft, NULL);
vol = pthread_cond_init(&cond_mid, NULL);
vol = pthread_cond_init(&cond_top, NULL);
vol = pthread_cond_init(&cond_btm, NULL);
vol; vol;
selected_file_current = 0; selected_file_current = 0;
selected_file_last = 0; selected_file_last = 0;

View File

@@ -1,10 +1,14 @@
#include <curses.h> #include <curses.h>
#include "threading.c"
void *thread_lft(void *data); #ifndef THREADING_GUARD
void *thread_mid(void *data); #define THREADING_GUARD
void *thread_rgt(void *data); #include "threading.c"
void *thread_top(void *data); #endif
void *thread_btm(void *data);
void *thread_lft();
void *thread_mid();
void *thread_rgt();
void *thread_top();
void *thread_btm();
void threading_init(); void threading_init();
void threading_free(); void threading_free();

View File

@@ -5,6 +5,8 @@
#include "defines.h" #include "defines.h"
#include "dir.h" #include "dir.h"
extern unsigned int terminal_height;
extern unsigned int terminal_width;
extern unsigned int status; extern unsigned int status;
extern char *input; extern char *input;
@@ -42,15 +44,13 @@ void window_top(WINDOW *win){
wattroff(win, COLOR_PAIR(COLOR_PATH)); wattroff(win, COLOR_PAIR(COLOR_PATH));
pthread_mutex_unlock(&mutex_top); pthread_mutex_unlock(&mutex_top);
} else { } else {
wprintw(win,"loading"); mvwaddstr(win, 0, terminal_width/2, "LOADING");
status |= STATUS_UPDATE_SCREEN_0; status |= STATUS_UPDATE_SCREEN_0;
} }
} }
void window_btm(WINDOW *win){ void window_btm(WINDOW *win){
werase(win); werase(win);
unsigned long local_width;
unsigned long local_height;
getmaxyx(win, local_height, local_width);
if (pthread_mutex_trylock(&mutex_btm) == 0) { if (pthread_mutex_trylock(&mutex_btm) == 0) {
if (*top_buffer != ' ') { /*printing ' ' (standard initialized value, see threading_init) makes valgrind throw a fuss*/ if (*top_buffer != ' ') { /*printing ' ' (standard initialized value, see threading_init) makes valgrind throw a fuss*/
mvwprintw(win, 0, 0, "%s", btm_buffer); mvwprintw(win, 0, 0, "%s", btm_buffer);
@@ -59,58 +59,48 @@ void window_btm(WINDOW *win){
/*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 */ /*the printing of all possible inputs are done in user_interactions */
} else { } else {
mvwprintw(win, local_height/2, local_width/2, "LOADING"); mvwaddstr(win, 0, terminal_width/2, "LOADING");
status |= STATUS_UPDATE_SCREEN_0; status |= STATUS_UPDATE_SCREEN_0;
} }
} }
void window_lft(WINDOW *win){ void window_lft(WINDOW *win){
werase(win); werase(win);
box(win, 0, 0);
unsigned long local_width;
unsigned long local_height;
getmaxyx(win, local_height, local_width);
if (pthread_mutex_trylock(&mutex_lft) == 0) { if (pthread_mutex_trylock(&mutex_lft) == 0) {
print_dir(win, 0, &lft_file_count, lft_content); print_dir(win, 0, &lft_file_count, lft_content);
pthread_mutex_unlock(&mutex_lft); pthread_mutex_unlock(&mutex_lft);
} else { } else {
mvwprintw(win, local_height/2, local_width/2, "LOADING"); mvwaddstr(win, terminal_height/2, terminal_width/8, "LOADING");
status |= STATUS_UPDATE_SCREEN_0; status |= STATUS_UPDATE_SCREEN_0;
} }
} }
void window_mid(WINDOW *win){ void window_mid(WINDOW *win){
werase(win); werase(win);
box(win, 0, 0);
unsigned long local_width;
unsigned long local_height;
getmaxyx(win, local_height, local_width);
if (pthread_mutex_trylock(&mutex_mid) == 0) { if (pthread_mutex_trylock(&mutex_mid) == 0) {
if (mid_file_count == 0) { if (mid_file_count == 0) {
mvwprintw(win, 0, 0, "empty"); mvwaddstr(win, 0, 0, "empty");
} else { } else {
print_dir(win, 1, &mid_file_count, mid_content); print_dir(win, 1, &mid_file_count, mid_content);
} }
pthread_mutex_unlock(&mutex_mid); pthread_mutex_unlock(&mutex_mid);
} else { } else {
mvwprintw(win, local_height/2, local_width/2, "LOADING"); mvwaddstr(win, terminal_height/2, terminal_width/4, "LOADING");
status |= STATUS_UPDATE_SCREEN_0; status |= STATUS_UPDATE_SCREEN_0;
} }
} }
void window_rgt(WINDOW *win){ void window_rgt(WINDOW *win){
werase(win); werase(win);
box(win, 0, 0);
unsigned long local_width;
unsigned long local_height;
getmaxyx(win, local_height, local_width);
if (pthread_mutex_trylock(&mutex_rgt) == 0) { if (pthread_mutex_trylock(&mutex_rgt) == 0) {
if (rgt_file_count == 0) { if (rgt_file_count == 0) {
if (rgt_content[0].file_type == FILE_TYPE_OPEN_FILE) { if (rgt_content[0].file_type == FILE_TYPE_OPEN_FILE) {
mvwprintw(win, 0, 0, "%s", rgt_buffer); mvwaddnstr(win, 0, 0, rgt_buffer, (terminal_width/2) * terminal_width);
} else if (rgt_content->permissions & S_IRUSR) {
mvwaddstr(win, 0, 0, "not accessible");
} else { } else {
mvwprintw(win, 0, 0, "empty"); mvwaddstr(win, 0, 0, "empty");
} }
@@ -119,7 +109,7 @@ void window_rgt(WINDOW *win){
} }
pthread_mutex_unlock(&mutex_rgt); pthread_mutex_unlock(&mutex_rgt);
} else { } else {
mvwprintw(win, local_height/2, local_width/2, "LOADING"); mvwaddstr(win, terminal_height/2, terminal_width/4, "LOADING");
status |= STATUS_UPDATE_SCREEN_0; status |= STATUS_UPDATE_SCREEN_0;
} }
} }