mirror of
https://gittea.dev/nova/th.git
synced 2025-10-22 10:50:15 -04:00
Compare commits
57 Commits
9ea82511e6
...
main
Author | SHA1 | Date | |
---|---|---|---|
![]() |
df726e2f32 | ||
![]() |
140457e4b7 | ||
![]() |
23654741bc | ||
![]() |
fa544204ba | ||
![]() |
61023ce42e | ||
![]() |
3fa16fd8b2 | ||
![]() |
ac6d0e8408 | ||
![]() |
731bfe722a | ||
![]() |
3082fed378 | ||
![]() |
23ff0b07ec | ||
![]() |
c2d88f4909 | ||
![]() |
0b98e8eb68 | ||
![]() |
87ae2a5e8f | ||
![]() |
f0ad6295f7 | ||
![]() |
e979209de3 | ||
![]() |
8fade0c4a8 | ||
![]() |
333d4d1df6 | ||
![]() |
c6763233bf | ||
![]() |
7ee16c8f4a | ||
![]() |
b32848ad3d | ||
![]() |
4d9dc46691 | ||
![]() |
8dcf88baea | ||
![]() |
8839f737c5 | ||
![]() |
36012b1a71 | ||
![]() |
4784ec3f64 | ||
![]() |
d3e46846cd | ||
![]() |
21a11d8be1 | ||
![]() |
b46d2da308 | ||
![]() |
fe781c2d3c | ||
![]() |
fb1af6d2d2 | ||
![]() |
14948ba573 | ||
![]() |
e0a5871bc2 | ||
![]() |
395c5fcb09 | ||
![]() |
525f205d67 | ||
![]() |
c923497f01 | ||
![]() |
69f9a609e5 | ||
![]() |
5afabfb412 | ||
![]() |
9f3042ccac | ||
![]() |
a05c3ecd96 | ||
![]() |
4a9c38d034 | ||
![]() |
ba1e95bca3 | ||
![]() |
922881cd45 | ||
![]() |
59964cf828 | ||
![]() |
e1c298ae50 | ||
![]() |
a57869a7a1 | ||
![]() |
520d4eca13 | ||
![]() |
963a018419 | ||
![]() |
a0a102e5f6 | ||
![]() |
0d2310cd7c | ||
![]() |
3f90802663 | ||
![]() |
f25768d522 | ||
![]() |
55cd4077b1 | ||
![]() |
3a2c226ed9 | ||
![]() |
1d54169c2d | ||
![]() |
f0a2f14966 | ||
![]() |
017bf407d9 | ||
![]() |
2af21a0875 |
4
Makefile
4
Makefile
@@ -3,6 +3,7 @@ CFLAGS := -Wall -Wextra -O2 -flto=auto
|
||||
CURSES := -lncursesw -ltinfow #utf8 support
|
||||
#CURSES := -lncurses -tinfo #no utf8
|
||||
CFLAGS_DEBUG := $(CFLAGS) -g
|
||||
CFLAGS_PROFILE := $(CFLAGS) -pg
|
||||
GDB := gdb --tui ./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
|
||||
@@ -15,6 +16,9 @@ d:
|
||||
$(CC) ./main.c -o th -std=c89 $(CFLAGS_DEBUG) $(CURSES)
|
||||
$(GDB)
|
||||
|
||||
p:
|
||||
$(CC) ./main.c -o th -std=c89 $(CFLAGS_PROFILE) $(CURSES)
|
||||
|
||||
v:
|
||||
$(CC) ./main.c -o th -std=c89 $(CFLAGS_DEBUG) $(CURSES)
|
||||
$(VALGRIND)
|
||||
|
289
backend.c
289
backend.c
@@ -1,22 +1,5 @@
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <curses.h>
|
||||
#include <dirent.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <pthread.h>
|
||||
#include "defines.h"
|
||||
#include "sorting.h"
|
||||
|
||||
extern unsigned int settings;
|
||||
extern unsigned int file_modifiers;
|
||||
extern unsigned int color_count;
|
||||
extern unsigned int terminal_height;
|
||||
extern volatile unsigned long selected_file_current;
|
||||
extern color *colors;
|
||||
int (*order_func)() = sort_natural;
|
||||
|
||||
|
||||
char* concat(const char *s1, const char *s2){
|
||||
const size_t len1 = strlen(s1);
|
||||
@@ -26,254 +9,46 @@ char* concat(const char *s1, const char *s2){
|
||||
memcpy(result + len1, s2, len2 + 1);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
unsigned long get_dir_size(char *path){
|
||||
DIR *dir = opendir(path);
|
||||
unsigned long entry_count = 0;
|
||||
if (dir) {
|
||||
struct dirent *entry;
|
||||
while ((entry=readdir(dir))) {
|
||||
if (entry->d_name[0] != '.' || (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
||||
entry_count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) {
|
||||
/* removes files "." and ".." */
|
||||
entry_count -= 2;
|
||||
}
|
||||
return entry_count;
|
||||
|
||||
}
|
||||
|
||||
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content){
|
||||
struct dirent **entry;
|
||||
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { /* print hidden files */
|
||||
scandir(path, &entry, skip_dot, alphasort);
|
||||
} else {
|
||||
scandir(path, &entry, skip_hidden_files, alphasort);
|
||||
}
|
||||
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i < *dir_file_count; i++ ) {
|
||||
if (entry[i]->d_name[0] == '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
||||
} else {
|
||||
dir_content[i].file_name = malloc(strlen(entry[i]->d_name)+1);
|
||||
strcpy(dir_content[i].file_name, entry[i]->d_name);
|
||||
dir_content[i].file_name[strlen(entry[i]->d_name)] = '\0';
|
||||
|
||||
|
||||
struct stat *file;
|
||||
file = malloc(sizeof(struct stat));
|
||||
memset(file, ' ', sizeof(struct stat));
|
||||
|
||||
/* 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("/"));
|
||||
memcpy(full_path, path, strlen(path));
|
||||
memcpy(full_path + path_len, "/", sizeof("/"));
|
||||
memcpy(full_path + path_len + sizeof("/") - 1, entry[i]->d_name, strlen(entry[i]->d_name) + 1);
|
||||
|
||||
lstat(full_path, file);
|
||||
|
||||
dir_content[i].file_size = file->st_size;
|
||||
dir_content[i].permissions = 1;
|
||||
dir_content[i].permissions = file->st_mode;
|
||||
|
||||
if (S_ISDIR(file->st_mode)) {
|
||||
dir_content[i].file_type = FILE_TYPE_DIR;
|
||||
dir_content[i].color_pair = COLOR_DIR;
|
||||
dir_content[i].file_size = get_dir_size(full_path);
|
||||
} else if (S_ISLNK(file->st_mode)) {
|
||||
dir_content[i].file_type = FILE_TYPE_SYMLINK;
|
||||
dir_content[i].color_pair = COLOR_SYMLINK;
|
||||
dir_content[i].file_size = get_dir_size(full_path);
|
||||
} else if (file->st_mode & S_IXUSR) {
|
||||
dir_content[i].file_type = FILE_TYPE_EXEC;
|
||||
dir_content[i].color_pair = COLOR_EXEC;
|
||||
} else if (S_ISBLK(file->st_mode)) {
|
||||
dir_content[i].file_type = FILE_TYPE_BLOCK;
|
||||
dir_content[i].color_pair = COLOR_BLOCK;
|
||||
} else if (S_ISCHR(file->st_mode)) {
|
||||
dir_content[i].file_type = COLOR_CHARDEV;
|
||||
} else if (S_ISFIFO(file->st_mode)) {
|
||||
dir_content[i].file_type = FILE_TYPE_FIFO;
|
||||
dir_content[i].color_pair = COLOR_FIFO;
|
||||
} else if (S_ISSOCK(file->st_mode)) {
|
||||
dir_content[i].file_type = FILE_TYPE_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 {
|
||||
dir_content[i].file_type = COLOR_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 {
|
||||
}
|
||||
}
|
||||
free(full_path);
|
||||
free(file);
|
||||
}
|
||||
}
|
||||
|
||||
qsort(dir_content, *dir_file_count, sizeof(file), order_func);
|
||||
|
||||
for (i = 0; i < *dir_file_count; i++) {
|
||||
free(entry[i]);
|
||||
}
|
||||
free(entry);
|
||||
|
||||
}
|
||||
|
||||
void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file *dir_content){
|
||||
/* i am not proud of this function */
|
||||
unsigned long line_width = getmaxx(win);
|
||||
|
||||
char *bg = malloc(line_width+1);
|
||||
memset(bg, ' ', line_width);
|
||||
bg[line_width] = '\0';
|
||||
|
||||
unsigned long i = 0;
|
||||
float file_size;
|
||||
float printed_size = 0;
|
||||
char size_char = ' ';
|
||||
char is_selected = 0;
|
||||
static const char sizes[6] = { 'B', 'K', 'M', 'G', 'T', 'P' };
|
||||
|
||||
unsigned long offset_vertical = 0;
|
||||
unsigned long offset_back = 0;
|
||||
unsigned long offset_front = 2;
|
||||
if (print_info) {
|
||||
if (*dir_file_count > 9) {
|
||||
offset_front = (snprintf(NULL, 0, "%ld", *dir_file_count)) + 1;
|
||||
}
|
||||
if (selected_file_current > (terminal_height/3)*2 && *dir_file_count > terminal_height - 2) {
|
||||
if (selected_file_current + (terminal_height/3) >= *dir_file_count) {
|
||||
offset_vertical = *dir_file_count - terminal_height+2;
|
||||
} else {
|
||||
offset_vertical = selected_file_current - (terminal_height/3)*2;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (i = offset_vertical; i < *dir_file_count && i < (terminal_height + offset_vertical); i++) {
|
||||
if (print_info) {
|
||||
file_size = dir_content[i].file_size;
|
||||
char size_index = 0;
|
||||
while (file_size > 1) {
|
||||
printed_size=file_size;
|
||||
file_size /= 1024;
|
||||
size_index++;
|
||||
if (size_index >= 6) {
|
||||
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++;
|
||||
}
|
||||
size_char = sizes[size_index-1];
|
||||
if (dir_content[i].file_type == FILE_TYPE_DIR || dir_content[i].file_type == FILE_TYPE_SYMLINK) {
|
||||
offset_back = line_width - (snprintf(NULL,0,"%ld", dir_content[i].file_size) + 1);
|
||||
} else if (size_char =='B') {
|
||||
offset_back = line_width - (snprintf(NULL,0,"%0.0lf %c", printed_size, size_char) + 1);
|
||||
} else {
|
||||
offset_back = line_width - (snprintf(NULL,0,"%0.2lf %c", printed_size, size_char) + 1);
|
||||
}
|
||||
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;
|
||||
|
||||
if (dir_content[i].status & FILE_STATUS_SELECTED) {
|
||||
is_selected = 1;
|
||||
} else {
|
||||
is_selected = 0;
|
||||
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;
|
||||
|
||||
if (dir_content[i].status & FILE_STATUS_SELECTED) {
|
||||
wattron(win, COLOR_PAIR(8));
|
||||
ret = strstr(haystack_case, needle_case);
|
||||
free(needle_case);
|
||||
free(haystack_case);
|
||||
} else {
|
||||
wattron(win, COLOR_PAIR(dir_content[i].color_pair));
|
||||
ret = strstr(haystack, needle);
|
||||
}
|
||||
if (dir_content[i].status & FILE_STATUS_HOVER) {
|
||||
wattron(win, A_REVERSE);
|
||||
}
|
||||
|
||||
/* shortens the printed file name if it is too long
|
||||
* example input: aaaaaaaa.txt
|
||||
* example output: aaa~.txt
|
||||
* if no extension is found, the name will truncate */
|
||||
char *file_name;
|
||||
unsigned long file_name_width = strlen(dir_content[i].file_name);
|
||||
if ((file_name_width + offset_front + is_selected) > offset_back - 1) {
|
||||
char *extension = strrchr(dir_content[i].file_name, '.');
|
||||
if (extension) {
|
||||
int char_offset = (file_name_width + offset_front + is_selected) - (offset_back - 1) ;
|
||||
if ((file_name_width - char_offset - strlen(extension) - 1) > 1) {
|
||||
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';
|
||||
}
|
||||
|
||||
} 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';
|
||||
}
|
||||
|
||||
mvwaddstr(win, i-offset_vertical, 0, bg);
|
||||
if(print_info) {
|
||||
mvwprintw(win, i-offset_vertical, 0, "%ld", i);
|
||||
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) {
|
||||
mvwprintw(win, i-offset_vertical, offset_back, "%ld", dir_content[i].file_size);
|
||||
}else if (size_char =='B') {
|
||||
mvwprintw(win, i-offset_vertical, offset_back, "%0.0lf %c", printed_size, size_char);
|
||||
} else {
|
||||
mvwprintw(win, i-offset_vertical, offset_back, "%0.2lf %c", printed_size, size_char);
|
||||
}
|
||||
} else {
|
||||
mvwaddnstr(win, i-offset_vertical, 0, file_name, line_width);
|
||||
free(file_name);
|
||||
}
|
||||
|
||||
if (dir_content[i].status & FILE_STATUS_HOVER) {
|
||||
wattroff(win, A_REVERSE);
|
||||
}
|
||||
if (dir_content[i].status & FILE_STATUS_SELECTED) {
|
||||
wattroff(win, COLOR_PAIR(8));
|
||||
} else {
|
||||
wattroff(win, COLOR_PAIR(dir_content[i].color_pair));
|
||||
}
|
||||
|
||||
}
|
||||
free(bg);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@@ -1,7 +1,9 @@
|
||||
#include <curses.h>
|
||||
#ifndef BACKEND_GUARD
|
||||
#define BACKEND_GUARD
|
||||
#include "backend.c"
|
||||
#endif
|
||||
|
||||
|
||||
unsigned long get_dir_size(char *path);
|
||||
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content);
|
||||
void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file *dir_content);
|
||||
char* concat(const char *s1, const char *s2);
|
||||
char* smartstrcasestr(char *haystack, const char *needle);
|
||||
|
54
config.h
54
config.h
@@ -1,7 +1,14 @@
|
||||
#include "defines.h"
|
||||
#include "interactions.h"
|
||||
#include "sorting.h"
|
||||
#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
|
||||
#define CONFIG_GUARD
|
||||
#include "defines.h"
|
||||
#include "sorting.h"
|
||||
#include "interactions.h"
|
||||
/* }}} */
|
||||
|
||||
static const mimetype mimetype_default_cmd[] = {
|
||||
/* mimetype shell command
|
||||
@@ -37,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 */
|
||||
{ "q", quit_program, NULL, "quit" },
|
||||
{ " ", 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 */
|
||||
{ "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 */
|
||||
{ "t", move_down, NULL, "move down" },
|
||||
@@ -50,6 +61,8 @@ static const binding key_binding[] = {
|
||||
{ "r", rename_hovered, NULL, "rename hovered file" }, /* renames currently hovered 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 */
|
||||
{ "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" },
|
||||
{ "dd", yank_file, "cut", "cut file/directory" },
|
||||
{ "pp", paste, NULL, "paste" },
|
||||
@@ -57,10 +70,14 @@ static const binding key_binding[] = {
|
||||
{ "G", jump_bottom, NULL, "jump to last file in dir" },
|
||||
{ "gg", jump_top, NULL, "jump to first file in dir" },
|
||||
{ "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" },
|
||||
{ "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" },
|
||||
{ "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" },
|
||||
{ "go", jump_to_dir, "/opt", "jump to /opt" },
|
||||
{ "gt", jump_to_dir, "/tmp", "jump to /tmp" },
|
||||
@@ -74,11 +91,10 @@ static const binding key_binding[] = {
|
||||
{ "uz", cmd_on_selected, "unzip ", "unzip zip" },
|
||||
|
||||
{ "on", order_by, sort_natural, "order natural" },
|
||||
{ "or", not_implemented, "", "order reverse" },
|
||||
{ "oe", not_implemented, "", "order extension" },
|
||||
{ "oe", order_by, sort_extension, "order extension" },
|
||||
{ "os", order_by, sort_size, "order size" },
|
||||
{ "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" },
|
||||
|
||||
{ "mk", makedir, NULL, "create directory" },
|
||||
@@ -87,7 +103,27 @@ static const binding key_binding[] = {
|
||||
{ "a", toggle_hidden_files, NULL, "toggle hidden files" },
|
||||
{ "\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 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 char size_unit_count = (sizeof(size_unit) / sizeof(size_unit[0])) - 1;
|
||||
#else
|
||||
static const char clipboard_cmd[];
|
||||
static const mimetype mimetype_default_cmd[];
|
||||
static const extension file_extension_default_cmd[];
|
||||
static const binding key_binding[];
|
||||
static const unsigned long binding_count;
|
||||
static const unsigned long mimetype_default_count;
|
||||
static const unsigned long file_extension_default_count;
|
||||
static const char size_unit[];
|
||||
static const char size_unit_count;
|
||||
#endif
|
||||
/* }}} */
|
||||
|
30
defines.h
30
defines.h
@@ -9,6 +9,7 @@
|
||||
#define STATUS_UPDATE_SCREEN_RELOAD_FULL 32
|
||||
#define STATUS_USER_ROOT 64
|
||||
#define STATUS_INPUT_MATCH 128
|
||||
#define STATUS_DELTA_TIME 256
|
||||
|
||||
#define SETTINGS_HAS_COLOR 1
|
||||
|
||||
@@ -25,8 +26,7 @@
|
||||
#define FILE_STATUS_HOVER 1
|
||||
#define FILE_STATUS_SELECTED 2
|
||||
#define FILE_STATUS_IS_REGULAR_FILE 4
|
||||
#define FILE_STATUS_DIR_EMPTY 64 /* if a directory is empty */
|
||||
#define FILE_STATUS_FILE_OPEN 128 /* only used for file previews */
|
||||
#define FILE_STATUS_FILE_OPEN 64 /* only used for file previews */
|
||||
|
||||
#define COLOR_UNKNOWN 0
|
||||
#define COLOR_DIR 1
|
||||
@@ -40,16 +40,16 @@
|
||||
#define COLOR_ORPHAN 9
|
||||
#define COLOR_PATH 10
|
||||
|
||||
#define FILE_TYPE_UNKNOWN COLOR_UNKNOWN
|
||||
#define FILE_TYPE_DIR COLOR_DIR
|
||||
#define FILE_TYPE_EXEC COLOR_EXEC
|
||||
#define FILE_TYPE_REGULAR COLOR_REGULAR
|
||||
#define FILE_TYPE_SYMLINK COLOR_SYMLINK
|
||||
#define FILE_TYPE_BLOCK COLOR_BLOCK
|
||||
#define FILE_TYPE_CHARDEV COLOR_CHARDEV
|
||||
#define FILE_TYPE_SOCK COLOR_SOCK
|
||||
#define FILE_TYPE_FIFO COLOR_FIFO
|
||||
#define FILE_TYPE_ORPHAN COLOR_ORPHAN
|
||||
#define FILE_TYPE_UNKNOWN 0
|
||||
#define FILE_TYPE_EXEC 1
|
||||
#define FILE_TYPE_REGULAR 2
|
||||
#define FILE_TYPE_BLOCK 3
|
||||
#define FILE_TYPE_CHARDEV 4
|
||||
#define FILE_TYPE_SOCK 5
|
||||
#define FILE_TYPE_FIFO 6
|
||||
#define FILE_TYPE_ORPHAN 7
|
||||
#define FILE_TYPE_DIR 32
|
||||
#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 YANK_IS_USED 1
|
||||
@@ -91,6 +91,10 @@ typedef struct Yank {
|
||||
char **list;
|
||||
unsigned long count;
|
||||
} yank;
|
||||
|
||||
typedef struct Linked_dir {
|
||||
char *path;
|
||||
unsigned long selected_file_current;
|
||||
struct Linked_dir *next;
|
||||
} linked_dir;
|
||||
|
||||
#endif
|
||||
|
422
dir.c
Normal file
422
dir.c
Normal file
@@ -0,0 +1,422 @@
|
||||
#include <curses.h>
|
||||
#include <pthread.h>
|
||||
#include <dirent.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "sorting.h"
|
||||
#include "defines.h"
|
||||
#include "config.h"
|
||||
|
||||
|
||||
extern file *mid_content;
|
||||
extern unsigned long mid_file_count;
|
||||
extern unsigned int settings;
|
||||
extern unsigned int file_modifiers;
|
||||
extern unsigned int color_count;
|
||||
extern unsigned int terminal_height;
|
||||
extern volatile unsigned long selected_file_current;
|
||||
extern volatile unsigned long selected_file_last;
|
||||
extern color *colors;
|
||||
int (*order_func)() = sort_natural;
|
||||
linked_dir *visited_dirs;
|
||||
linked_dir *current_dir;
|
||||
|
||||
unsigned long get_dir_size(char *path);
|
||||
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content);
|
||||
void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file *dir_content);
|
||||
|
||||
|
||||
|
||||
unsigned long get_dir_size(char *path){
|
||||
DIR *dir = opendir(path);
|
||||
unsigned long entry_count = 0;
|
||||
struct dirent *entry;
|
||||
if (dir && file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) {
|
||||
while ((entry=readdir(dir))) {
|
||||
entry_count++;
|
||||
}
|
||||
/* removes files "." and ".." */
|
||||
entry_count -= 2;
|
||||
} else if (dir) {
|
||||
while ((entry=readdir(dir))) {
|
||||
if (entry->d_name[0] != '.') {
|
||||
entry_count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
return entry_count;
|
||||
|
||||
}
|
||||
|
||||
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content){
|
||||
struct dirent **entry = NULL;
|
||||
if (file_modifiers & FILE_MODIFIERS_HIDDEN_FILES) { /* print hidden files */
|
||||
scandir(path, &entry, skip_dot, NULL);
|
||||
} else {
|
||||
scandir(path, &entry, skip_hidden_files, NULL);
|
||||
}
|
||||
|
||||
unsigned long i = 0;
|
||||
for (i = 0; i < *dir_file_count; i++ ) {
|
||||
if (entry[i]->d_name[0] == '.' && !(file_modifiers & FILE_MODIFIERS_HIDDEN_FILES)) {
|
||||
} else {
|
||||
dir_content[i].file_name = malloc(strlen(entry[i]->d_name)+1);
|
||||
strcpy(dir_content[i].file_name, entry[i]->d_name);
|
||||
|
||||
|
||||
struct stat *file;
|
||||
file = malloc(sizeof(struct stat));
|
||||
|
||||
/* using the full path allows using the same function for all windows */
|
||||
char *full_path = malloc(strlen(path) + strlen(entry[i]->d_name) + 1 + sizeof("/"));
|
||||
memcpy(full_path, path, strlen(path));
|
||||
memcpy(full_path + strlen(path) + sizeof("/") - 1, entry[i]->d_name, strlen(entry[i]->d_name) + 1);
|
||||
full_path[strlen(path)] = '/';
|
||||
|
||||
lstat(full_path, file);
|
||||
|
||||
dir_content[i].file_size = file->st_size;
|
||||
dir_content[i].permissions = file->st_mode;
|
||||
|
||||
if (S_ISDIR(file->st_mode)) {
|
||||
dir_content[i].file_type = FILE_TYPE_DIR;
|
||||
dir_content[i].color_pair = COLOR_DIR;
|
||||
dir_content[i].file_size = get_dir_size(full_path);
|
||||
} else if (S_ISLNK(file->st_mode)) {
|
||||
stat(full_path, file);
|
||||
if (S_ISDIR(file->st_mode)) {
|
||||
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) {
|
||||
dir_content[i].file_type = FILE_TYPE_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)) {
|
||||
dir_content[i].file_type = FILE_TYPE_BLOCK;
|
||||
dir_content[i].color_pair = COLOR_BLOCK;
|
||||
} else if (S_ISCHR(file->st_mode)) {
|
||||
dir_content[i].file_type = COLOR_CHARDEV;
|
||||
} else if (S_ISFIFO(file->st_mode)) {
|
||||
dir_content[i].file_type = FILE_TYPE_FIFO;
|
||||
dir_content[i].color_pair = COLOR_FIFO;
|
||||
} else if (S_ISSOCK(file->st_mode)) {
|
||||
dir_content[i].file_type = FILE_TYPE_SOCK;
|
||||
dir_content[i].color_pair = COLOR_SOCK;
|
||||
} else {
|
||||
dir_content[i].file_type = COLOR_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 {
|
||||
}
|
||||
}
|
||||
free(full_path);
|
||||
free(file);
|
||||
}
|
||||
}
|
||||
|
||||
qsort(dir_content, *dir_file_count, sizeof(file), order_func);
|
||||
|
||||
for (i = 0; i < *dir_file_count; i++) {
|
||||
free(entry[i]);
|
||||
}
|
||||
if (entry != NULL) {
|
||||
free(entry);
|
||||
} else {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file *dir_content){
|
||||
/* i am not proud of this function */
|
||||
unsigned long line_width = getmaxx(win);
|
||||
|
||||
char *bg = malloc(line_width+1);
|
||||
memset(bg, ' ', line_width);
|
||||
bg[line_width] = '\0';
|
||||
|
||||
unsigned long i = 0;
|
||||
float file_size;
|
||||
float printed_size = 0;
|
||||
char size_char = ' ';
|
||||
char is_selected = 0;
|
||||
|
||||
unsigned long offset_vertical = 0;
|
||||
unsigned long offset_back = 0;
|
||||
#if SETTINGS_LINE_NUMBERS == 0
|
||||
unsigned long offset_front = 0;
|
||||
#else
|
||||
long offset_front = 2;
|
||||
#endif
|
||||
|
||||
long offset_index = 2; /* only used for the index of the file itself */
|
||||
if (print_info) {
|
||||
if (*dir_file_count > 9) {
|
||||
|
||||
#if SETTINGS_LINE_NUMBERS != 0
|
||||
unsigned long dir_file_count_ = *dir_file_count;
|
||||
while(dir_file_count_ > 9) {
|
||||
offset_front++;
|
||||
dir_file_count_ /= 10;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
if (selected_file_current > (terminal_height/3)*2 && *dir_file_count > terminal_height - 2) {
|
||||
if (selected_file_current + (terminal_height/3) >= *dir_file_count) {
|
||||
offset_vertical = *dir_file_count - terminal_height+2;
|
||||
} else {
|
||||
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++) {
|
||||
|
||||
|
||||
|
||||
if (print_info) {
|
||||
file_size = dir_content[i].file_size;
|
||||
char size_index = -1;
|
||||
|
||||
do {
|
||||
printed_size=file_size;
|
||||
file_size /= 1024;
|
||||
size_index++;
|
||||
} while (file_size > 1 && size_index < size_unit_count);
|
||||
size_char = size_unit[(unsigned)size_index];
|
||||
|
||||
if (dir_content[i].file_type &= FILE_TYPE_DIR) {
|
||||
offset_back = line_width - (snprintf(NULL,0,"%ld", dir_content[i].file_size) + 1);
|
||||
} else if (size_char =='B') {
|
||||
offset_back = line_width - (snprintf(NULL,0,"%0.0lf %c", printed_size, size_char) + 1);
|
||||
} else {
|
||||
offset_back = line_width - (snprintf(NULL,0,"%0.2lf %c", printed_size, size_char) + 1);
|
||||
}
|
||||
} else {
|
||||
offset_back = line_width;
|
||||
}
|
||||
|
||||
if (dir_content[i].status & FILE_STATUS_SELECTED) {
|
||||
is_selected = 1;
|
||||
} else {
|
||||
is_selected = 0;
|
||||
}
|
||||
if (dir_content[i].status & FILE_STATUS_SELECTED) {
|
||||
wattron(win, COLOR_PAIR(8));
|
||||
} else {
|
||||
wattron(win, COLOR_PAIR(dir_content[i].color_pair));
|
||||
}
|
||||
if (dir_content[i].status & FILE_STATUS_HOVER) {
|
||||
wattron(win, A_REVERSE);
|
||||
} else {
|
||||
wattroff(win, A_REVERSE);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* shortens the printed file name if it is too long
|
||||
* example input: aaaaaaaa.txt
|
||||
* example output: aaa~.txt
|
||||
* if no extension is found, the name will truncate */
|
||||
char *file_name = NULL;
|
||||
unsigned long printable_size = (offset_back - is_selected - offset_front);
|
||||
if (strlen(dir_content[i].file_name) > printable_size) {
|
||||
char *extension = strrchr(dir_content[i].file_name, '.');
|
||||
if (extension && extension != dir_content[i].file_name) {
|
||||
file_name = malloc(printable_size);
|
||||
printable_size -= strlen(extension);
|
||||
|
||||
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 {
|
||||
file_name = malloc(strlen(dir_content[i].file_name)+1);
|
||||
memcpy(file_name, dir_content[i].file_name, strlen(dir_content[i].file_name)+1);
|
||||
}
|
||||
|
||||
|
||||
|
||||
mvwaddstr(win, i-offset_vertical, 0, bg);
|
||||
if(print_info) {
|
||||
#if SETTINGS_LINE_NUMBERS == 2
|
||||
long i_ = (selected_file_current) - i;
|
||||
offset_index = 0;
|
||||
while(i_) {
|
||||
offset_index++;
|
||||
i_ /= 10;
|
||||
}
|
||||
long relative_index = selected_file_current - i;
|
||||
if (relative_index < 0) {
|
||||
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);
|
||||
|
||||
#elif SETTINGS_LINE_NUMBERS == 1
|
||||
unsigned long i_ = i;
|
||||
offset_index = 2;
|
||||
while(i_ > 9) {
|
||||
offset_index++;
|
||||
i_ /= 10;
|
||||
}
|
||||
mvwprintw(win, i-offset_vertical, offset_front-offset_index, "%ld", i);
|
||||
#endif
|
||||
|
||||
mvwaddnstr(win, i-offset_vertical, offset_front+is_selected, file_name, line_width-offset_front-is_selected-2);
|
||||
|
||||
if (dir_content[i].file_type &= FILE_TYPE_DIR) {
|
||||
mvwprintw(win, i-offset_vertical, offset_back, "%ld", dir_content[i].file_size);
|
||||
}else if (size_char =='B') {
|
||||
mvwprintw(win, i-offset_vertical, offset_back, "%0.0lf %c", printed_size, size_char);
|
||||
} else {
|
||||
mvwprintw(win, i-offset_vertical, offset_back, "%0.2lf %c", printed_size, size_char);
|
||||
}
|
||||
} else {
|
||||
mvwaddnstr(win, i-offset_vertical, 0, file_name, line_width);
|
||||
}
|
||||
if (file_name != NULL) {
|
||||
/* sometimes NULL remains, need to do deeper analysis soon */
|
||||
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) {
|
||||
wattroff(win, COLOR_PAIR(8));
|
||||
} else {
|
||||
wattroff(win, COLOR_PAIR(dir_content[i].color_pair));
|
||||
}
|
||||
|
||||
}
|
||||
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, ¤t_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);
|
||||
}
|
||||
}
|
13
dir.h
Normal file
13
dir.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#ifndef DIR_GUARD
|
||||
#define DIR_GUARD
|
||||
#include "dir.c"
|
||||
#endif
|
||||
|
||||
unsigned long get_dir_size(char *path);
|
||||
void get_dir_content(char *path, unsigned long *dir_file_count, file *dir_content);
|
||||
void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file *dir_content);
|
||||
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);
|
@@ -1,9 +1,14 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "defines.h"
|
||||
|
||||
#include "backend.h"
|
||||
#include "defines.h"
|
||||
#include "config.h"
|
||||
|
||||
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
|
||||
static FILE *ueberzug = NULL;
|
||||
#endif
|
||||
extern unsigned int terminal_height;
|
||||
extern unsigned int terminal_width;
|
||||
char previewd;
|
||||
@@ -44,17 +49,20 @@ char* preview_file(char *file_name, unsigned long file_size){
|
||||
|
||||
char *mime = get_mimetype(file_name);
|
||||
|
||||
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
|
||||
images_clear();
|
||||
#endif
|
||||
|
||||
if (strstr(mime, "text")) {
|
||||
file_buffer = text(file_name, &file_size);
|
||||
} else {
|
||||
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
|
||||
} else if (strstr(mime, "image")) {
|
||||
file_buffer = generic(file_name);
|
||||
}
|
||||
|
||||
if (strstr(mime, "image")) {
|
||||
images_print(file_name);
|
||||
previewd = 1;
|
||||
#endif
|
||||
} else {
|
||||
images_clear();
|
||||
file_buffer = generic(file_name);
|
||||
}
|
||||
|
||||
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 *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");
|
||||
if (fread(file_buffer, *file_size, 1, fp) != 0) {
|
||||
file_buffer[*file_size] = '\0';
|
||||
if (fread(file_buffer, size, 1, fp) != 0) {
|
||||
file_buffer[size] = '\0';
|
||||
return file_buffer;
|
||||
} else {
|
||||
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 *cmd = concat("file ./\"", path);
|
||||
cmd = concat(cmd, "\"");
|
||||
@@ -109,6 +99,38 @@ char* generic(char *path){
|
||||
return "failed executing shell command \"file\"";
|
||||
}
|
||||
}
|
||||
void ueberzug_init(){
|
||||
ueberzug = popen("ueberzug layer -s ", "w");
|
||||
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
|
||||
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
|
||||
|
@@ -1,10 +1,13 @@
|
||||
#include "defines.h"
|
||||
#ifndef PREVIEW_GUARD
|
||||
#define PREVIEW_GUARD
|
||||
#include "file_previews.c"
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
char* preview_file(char *file_name, unsigned long file_size);
|
||||
char* get_mimetype(char *path);
|
||||
void images_clear();
|
||||
void ueberzug_init();
|
||||
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
|
||||
void ueberzug_close();
|
||||
#endif
|
||||
|
412
interactions.c
412
interactions.c
@@ -2,14 +2,14 @@
|
||||
#include <pthread.h>
|
||||
#include <dirent.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <strings.h>
|
||||
#include <signal.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "file_previews.h"
|
||||
#include "backend.h"
|
||||
#include "defines.h"
|
||||
#include "config.h"
|
||||
#include "dir.h"
|
||||
|
||||
|
||||
extern volatile unsigned long selected_file_current;
|
||||
@@ -19,10 +19,11 @@ extern unsigned int file_modifiers;
|
||||
extern pthread_mutex_t mutex_selection;
|
||||
extern pthread_mutex_t mutex_rgt;
|
||||
extern pthread_mutex_t mutex_mid;
|
||||
extern pthread_mutex_t mutex_btm;
|
||||
extern pthread_cond_t cond_rgt;
|
||||
extern file *mid_content;
|
||||
extern file *lft_content;
|
||||
extern file *rgt_content;
|
||||
extern volatile file *file_current;
|
||||
|
||||
extern unsigned int terminal_height;
|
||||
extern unsigned int terminal_width;
|
||||
@@ -37,13 +38,15 @@ extern unsigned int status;
|
||||
extern char *start_path;
|
||||
extern char *input;
|
||||
|
||||
extern time_t *seed;
|
||||
|
||||
char search_buffer[255];
|
||||
unsigned int timeout_time = 0;
|
||||
unsigned int input_pass;
|
||||
int parsed_input_number;
|
||||
unsigned long parsed_input_number;
|
||||
yank yank_files = { 0 };
|
||||
|
||||
int read_string(WINDOW *win, int y, int x, char *str);
|
||||
int strcmp_offset(char *in0, char *in1, char offset);
|
||||
extern void render_pass();
|
||||
extern int (*order_func)();
|
||||
|
||||
@@ -55,7 +58,6 @@ void FAIL(char *function, char *str){
|
||||
curs_set(1);
|
||||
echo();
|
||||
printf("ERROR in function %s: %s", function, str);
|
||||
kill(getpid(),9);
|
||||
}
|
||||
void user_interactions() {
|
||||
|
||||
@@ -68,7 +70,7 @@ void user_interactions() {
|
||||
|
||||
ch = getch();
|
||||
if(ch != ERR) {
|
||||
timeout(1); /* blocking timeout of getch() */
|
||||
timeout(10); /* blocking timeout of getch() */
|
||||
input[input_pass] = ch;
|
||||
mvaddstr(terminal_height-1, (terminal_width/3)*2, input);
|
||||
input_pass++;
|
||||
@@ -81,6 +83,8 @@ void user_interactions() {
|
||||
}
|
||||
binding_pass = 0;
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
} else {
|
||||
timeout(100);
|
||||
}
|
||||
|
||||
|
||||
@@ -117,7 +121,7 @@ void user_interactions() {
|
||||
}
|
||||
if (status & STATUS_INPUT_MATCH) {
|
||||
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);
|
||||
status &= ~STATUS_INPUT_MATCH;
|
||||
} else if (number_length != strlen(input)) {
|
||||
@@ -145,14 +149,18 @@ int read_string(WINDOW *win, int y, int x, char *str){
|
||||
if (ch == '\n') {
|
||||
err = 0;
|
||||
break;
|
||||
} else if (ch == 27) { /* esc key */
|
||||
err = 1;
|
||||
break;
|
||||
} else if (ch == '\t') { /* tab */
|
||||
memcpy(str + pass, mid_content[selected_file_current].file_name, strlen(mid_content[selected_file_current].file_name));
|
||||
mvwaddstr(win, y, x +pass, mid_content[selected_file_current].file_name);
|
||||
pass += strlen(mid_content[selected_file_current].file_name);
|
||||
} else if (ch == 127) { /* backspace */
|
||||
if (pass > 0) {
|
||||
pass--;
|
||||
mvwdelch(win, y, pass);
|
||||
}
|
||||
} else if (ch == 27) { /* esc key */
|
||||
err = 1;
|
||||
break;
|
||||
} else {
|
||||
mvwaddch(win, y, x +pass, ch);
|
||||
str[pass] = ch;
|
||||
@@ -161,87 +169,89 @@ int read_string(WINDOW *win, int y, int x, char *str){
|
||||
}
|
||||
str[pass] = '\0';
|
||||
|
||||
timeout(10);
|
||||
timeout(100);
|
||||
curs_set(0);
|
||||
|
||||
return err;
|
||||
}
|
||||
int strcmp_offset(char *in0, char *in1, char offset){
|
||||
|
||||
int i = 0;
|
||||
while (in0[i] != '\0' && in1[i] != '\0') {
|
||||
if (in0[i+offset] != in1[i]) {
|
||||
return 1;
|
||||
}
|
||||
i++;
|
||||
in1++;
|
||||
}
|
||||
return 0;
|
||||
|
||||
}
|
||||
void quit_program(){
|
||||
status = STATUS_QUIT_PROGRAM;
|
||||
}
|
||||
void toggle_selection(){
|
||||
void select_all(){
|
||||
pthread_mutex_lock(&mutex_selection);
|
||||
pthread_mutex_lock(&mutex_mid);
|
||||
mid_content[selected_file_current].status ^= FILE_STATUS_SELECTED;
|
||||
file_current->status ^= FILE_STATUS_SELECTED;
|
||||
status |= (STATUS_UPDATE_SCREEN_MASK);
|
||||
unsigned long i;
|
||||
for(i = 0; i < mid_file_count; i++) {
|
||||
mid_content[i].status ^= FILE_STATUS_SELECTED;
|
||||
}
|
||||
pthread_mutex_unlock(&mutex_mid);
|
||||
pthread_mutex_unlock(&mutex_selection);
|
||||
}
|
||||
void move_down(int passes){
|
||||
void move_down(unsigned long passes){
|
||||
pthread_mutex_lock(&mutex_selection);
|
||||
if (passes == 0) {
|
||||
passes++;
|
||||
}
|
||||
/*capping the maximum file is done inside thread_mid */
|
||||
selected_file_current += passes;
|
||||
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
|
||||
|
||||
update_selected_file();
|
||||
dir_set_selected_file_current(selected_file_current);
|
||||
|
||||
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_0);
|
||||
pthread_mutex_unlock(&mutex_selection);
|
||||
}
|
||||
void move_up(int passes){
|
||||
void move_up(unsigned long passes){
|
||||
pthread_mutex_lock(&mutex_selection);
|
||||
if (passes == 0) {
|
||||
passes++;
|
||||
}
|
||||
int i;
|
||||
for (i = 0; i < passes; i++) {
|
||||
if (selected_file_current != 0) {
|
||||
selected_file_current--;
|
||||
}
|
||||
unsigned long tmp = selected_file_current;
|
||||
selected_file_current -= passes;
|
||||
if (tmp < selected_file_current) {
|
||||
selected_file_current = 0;
|
||||
}
|
||||
|
||||
update_selected_file();
|
||||
dir_set_selected_file_current(selected_file_current);
|
||||
|
||||
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
|
||||
pthread_mutex_unlock(&mutex_selection);
|
||||
}
|
||||
void move_left(int passes){
|
||||
void move_left(unsigned long passes){
|
||||
if (passes == 0) {
|
||||
passes++;
|
||||
}
|
||||
int i;
|
||||
unsigned long i;
|
||||
for (i = 0; i < passes; i++) {
|
||||
if (chdir("..") != 0) {
|
||||
/* TODO(2025-07-09T00:30:05) fix */
|
||||
FAIL("move_left", "unhandled error of chdir");
|
||||
} else {
|
||||
selected_file_current = dir_get_selected_file_current();
|
||||
}
|
||||
}
|
||||
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
|
||||
}
|
||||
void move_right(){
|
||||
if (file_current->file_type == FILE_TYPE_DIR || file_current->file_type == FILE_TYPE_SYMLINK) {
|
||||
if (chdir(file_current->file_name) != 0) {
|
||||
if (mid_content->file_name[0] == '\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");
|
||||
} else {
|
||||
selected_file_current = dir_get_selected_file_current();
|
||||
}
|
||||
} else {
|
||||
unsigned long i = 0;
|
||||
char match = 0;
|
||||
char *mime = get_mimetype(file_current->file_name);
|
||||
char *extension = strrchr(file_current->file_name, '.');
|
||||
char *mime = get_mimetype(mid_content[selected_file_current].file_name);
|
||||
char *extension = strrchr(mid_content[selected_file_current].file_name, '.');
|
||||
if (extension != NULL) {
|
||||
for (i = 0; i < file_extension_default_count; i++) {
|
||||
if (strstr(extension, file_extension_default_cmd[i].file_extension)) {
|
||||
char *cmd = concat(file_extension_default_cmd[i].command, " ./\"");
|
||||
cmd = concat(cmd, file_current->file_name);
|
||||
cmd = concat(cmd, mid_content[selected_file_current].file_name);
|
||||
cmd = concat(cmd, "\"");
|
||||
|
||||
|
||||
@@ -254,12 +264,13 @@ void move_right(){
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (match == 0) {
|
||||
for (i = 0; i < mimetype_default_count; i++) {
|
||||
if (strstr(mime, mimetype_default_cmd[i].mimetype)) {
|
||||
|
||||
char *cmd = concat(mimetype_default_cmd[i].command, " ./\"");
|
||||
cmd = concat(cmd, file_current->file_name);
|
||||
cmd = concat(cmd, mid_content[selected_file_current].file_name);
|
||||
cmd = concat(cmd, "\"");
|
||||
btm_buffer = malloc(strlen(cmd));
|
||||
|
||||
@@ -278,27 +289,43 @@ void move_right(){
|
||||
}
|
||||
free(mime);
|
||||
}
|
||||
update_selected_file();
|
||||
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
|
||||
}
|
||||
void toggle_hidden_files(){
|
||||
file_modifiers ^= FILE_MODIFIERS_HIDDEN_FILES;
|
||||
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
|
||||
}
|
||||
void 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(){
|
||||
pthread_mutex_lock(&mutex_selection);
|
||||
selected_file_current = 0 - 1;
|
||||
update_selected_file();
|
||||
dir_set_selected_file_current(selected_file_current);
|
||||
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
|
||||
pthread_mutex_unlock(&mutex_selection);
|
||||
}
|
||||
void jump_top(){
|
||||
pthread_mutex_lock(&mutex_selection);
|
||||
selected_file_current = 0;
|
||||
update_selected_file();
|
||||
dir_set_selected_file_current(selected_file_current);
|
||||
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK);
|
||||
pthread_mutex_unlock(&mutex_selection);
|
||||
}
|
||||
|
||||
void open_with(){
|
||||
btm_buffer = concat("open \"", 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:");
|
||||
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
@@ -319,9 +346,13 @@ void open_with(){
|
||||
|
||||
if (!err) {
|
||||
char *cmd = concat(str, " ./\"");
|
||||
cmd = concat(cmd, file_current->file_name);
|
||||
cmd = concat(cmd, mid_content[selected_file_current].file_name);
|
||||
cmd = concat(cmd, "\"");
|
||||
|
||||
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
|
||||
images_clear();
|
||||
#endif
|
||||
|
||||
if (system(cmd) == -1) {
|
||||
FAIL("open_with", "creating subcommand failed unhandled");
|
||||
}
|
||||
@@ -337,8 +368,9 @@ void open_with(){
|
||||
}
|
||||
|
||||
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:");
|
||||
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
@@ -346,7 +378,9 @@ void rename_hovered(){
|
||||
mvwin(win_b, terminal_height-6, 0);
|
||||
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
|
||||
|
||||
pthread_mutex_unlock(&mutex_btm);
|
||||
render_pass();
|
||||
pthread_mutex_lock(&mutex_btm);
|
||||
|
||||
unsigned long local_height;
|
||||
local_height = getmaxy(win_b);
|
||||
@@ -358,7 +392,7 @@ void rename_hovered(){
|
||||
|
||||
|
||||
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, str);
|
||||
cmd = concat(cmd, "\"");
|
||||
@@ -368,6 +402,7 @@ void rename_hovered(){
|
||||
};
|
||||
btm_buffer = cmd;
|
||||
}
|
||||
pthread_mutex_unlock(&mutex_btm);
|
||||
|
||||
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
|
||||
|
||||
@@ -376,6 +411,7 @@ void rename_hovered(){
|
||||
}
|
||||
|
||||
void delete(){
|
||||
pthread_mutex_lock(&mutex_btm);
|
||||
|
||||
unsigned int i = 0;
|
||||
unsigned int hits = 0;
|
||||
@@ -392,7 +428,7 @@ void delete(){
|
||||
if (hits) {
|
||||
btm_buffer = concat("delete:", file_str);
|
||||
} 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, "\"");
|
||||
}
|
||||
|
||||
@@ -405,8 +441,9 @@ void delete(){
|
||||
mvwin(win_b, terminal_height-6, 0);
|
||||
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
|
||||
|
||||
pthread_mutex_unlock(&mutex_btm);
|
||||
render_pass();
|
||||
|
||||
pthread_mutex_lock(&mutex_btm);
|
||||
|
||||
timeout(-1); /* negative numbers block until enter is pressed */
|
||||
/* TODO(2025-06-22T01:24:30) fix fixed buffer size */
|
||||
@@ -414,32 +451,22 @@ void delete(){
|
||||
|
||||
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 */
|
||||
int error;
|
||||
if (hits) {
|
||||
int j = 2;
|
||||
for (i = 0; i < mid_file_count; i++) {
|
||||
if (mid_content[i].status & FILE_STATUS_SELECTED) {
|
||||
error = remove(mid_content[i].file_name);
|
||||
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++;
|
||||
}
|
||||
recursive_delete(mid_content[i]);
|
||||
}
|
||||
}
|
||||
free(btm_buffer);
|
||||
btm_buffer = concat("deleted: ", file_str);
|
||||
} else {
|
||||
free(btm_buffer);
|
||||
error = remove(file_current->file_name);
|
||||
if (error != 0) {
|
||||
mvaddstr(terminal_height-2, 0, "could not delete: " );
|
||||
mvaddstr(terminal_height-2, strlen("could not delete: "), mid_content[i].file_name);
|
||||
btm_buffer = " ";
|
||||
} else {
|
||||
btm_buffer = concat("deleted: \"", file_current->file_name);
|
||||
btm_buffer = concat(btm_buffer, "\"");
|
||||
if (mid_content[selected_file_current].file_type & FILE_TYPE_DIR) {
|
||||
recursive_delete(mid_content[selected_file_current]);
|
||||
}
|
||||
remove(mid_content[selected_file_current].file_name);
|
||||
btm_buffer = concat("deleted: \"", mid_content[selected_file_current].file_name);
|
||||
btm_buffer = concat(btm_buffer, "\"");
|
||||
|
||||
}
|
||||
/*system(cmd);*/
|
||||
@@ -455,15 +482,22 @@ void delete(){
|
||||
if (hits) {
|
||||
free(file_str);
|
||||
}
|
||||
pthread_mutex_unlock(&mutex_btm);
|
||||
}
|
||||
|
||||
void makedir(){
|
||||
btm_buffer = "create dir: ";
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
pthread_mutex_lock(&mutex_btm);
|
||||
|
||||
werase(win_b);
|
||||
mvwin(win_b, terminal_height-6, 0);
|
||||
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();
|
||||
pthread_mutex_lock(&mutex_btm);
|
||||
|
||||
unsigned long local_height;
|
||||
local_height = getmaxy(win_b);
|
||||
@@ -480,14 +514,20 @@ void makedir(){
|
||||
}
|
||||
free(str);
|
||||
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
|
||||
pthread_mutex_unlock(&mutex_btm);
|
||||
}
|
||||
void makefile(){
|
||||
btm_buffer = "create file: ";
|
||||
pthread_mutex_lock(&mutex_btm);
|
||||
|
||||
memcpy(btm_buffer, "create file: ", strlen("create file: "));
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
werase(win_b);
|
||||
mvwin(win_b, terminal_height-6, 0);
|
||||
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
|
||||
|
||||
pthread_mutex_unlock(&mutex_btm);
|
||||
render_pass();
|
||||
pthread_mutex_lock(&mutex_btm);
|
||||
|
||||
unsigned long local_height;
|
||||
local_height = getmaxy(win_b);
|
||||
@@ -503,23 +543,36 @@ void makefile(){
|
||||
fclose(fp);
|
||||
}
|
||||
free(str);
|
||||
pthread_mutex_unlock(&mutex_btm);
|
||||
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
|
||||
}
|
||||
|
||||
void update(){
|
||||
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY | STATUS_UPDATE_SCREEN_RELOAD_FULL);
|
||||
}
|
||||
void enter_shell(int passes, int index){
|
||||
void 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) {
|
||||
/*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){
|
||||
(void)passes;
|
||||
|
||||
mvaddstr(terminal_height-1, 0, key_binding[index].comment);
|
||||
mvaddstr(terminal_height-1, strlen(key_binding[index].comment), "\t");
|
||||
mvaddstr(terminal_height-1, strlen(key_binding[index].comment) + strlen("\t"), "is not yet implemented");
|
||||
}
|
||||
void jump_to_dir(int passes, int index){
|
||||
void jump_to_dir(unsigned long passes, int index){
|
||||
(void)passes;
|
||||
|
||||
char *ch = (char*)key_binding[index].black_magic;
|
||||
char slash = 0;
|
||||
unsigned int env_len = 0;
|
||||
@@ -561,6 +614,8 @@ void jump_to_dir(int passes, int index){
|
||||
}
|
||||
if (chdir(path) != 0) {
|
||||
FAIL("jump_to_dir", "jumping to black_magic in config.h failed");
|
||||
} else {
|
||||
selected_file_current = dir_get_selected_file_current();
|
||||
}
|
||||
|
||||
/*env_parsed shall not be modified (read: free'd) - the man page*/
|
||||
@@ -572,12 +627,21 @@ void jump_to_dir(int passes, int index){
|
||||
}
|
||||
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;
|
||||
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 hits = 0;
|
||||
char *file_str = " ";
|
||||
@@ -594,7 +658,7 @@ void cmd_on_selected(int passes, int index){
|
||||
btm_buffer = concat(key_binding[index].black_magic, file_str);
|
||||
} else {
|
||||
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, "\"");
|
||||
}
|
||||
|
||||
@@ -607,7 +671,9 @@ void cmd_on_selected(int passes, int index){
|
||||
mvwin(win_b, terminal_height-6, 0);
|
||||
wresize(win_b, 5, terminal_width/3); /*the div3 just looks cool*/
|
||||
|
||||
pthread_mutex_unlock(&mutex_btm);
|
||||
render_pass();
|
||||
pthread_mutex_lock(&mutex_btm);
|
||||
|
||||
|
||||
timeout(-1); /* negative numbers block until enter is pressed */
|
||||
@@ -631,12 +697,12 @@ void cmd_on_selected(int passes, int index){
|
||||
}
|
||||
}
|
||||
free(btm_buffer);
|
||||
btm_buffer = concat("completed: ", key_binding[index].black_magic);
|
||||
memcpy(btm_buffer, "completed: ", strlen("completed: "));
|
||||
} else {
|
||||
free(btm_buffer);
|
||||
free(cmd);
|
||||
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, "\"");
|
||||
if (system(cmd) != 0) {
|
||||
/*do nothing*/
|
||||
@@ -649,7 +715,7 @@ void cmd_on_selected(int passes, int index){
|
||||
|
||||
} else {
|
||||
free(btm_buffer);
|
||||
btm_buffer = "cancled deletion";
|
||||
memcpy(btm_buffer, "cancled deletion", strlen("cancled deletion"));
|
||||
}
|
||||
|
||||
timeout(10);
|
||||
@@ -658,16 +724,41 @@ void cmd_on_selected(int passes, int index){
|
||||
if (hits) {
|
||||
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;
|
||||
if (yank_files.status & YANK_IS_USED) {
|
||||
free(yank_files.path);
|
||||
for (i = 0; i < yank_files.count; i++) {
|
||||
free(yank_files.list[i]);
|
||||
}
|
||||
free(yank_files.list);
|
||||
free(yank_files.path);
|
||||
yank_files.count = 0;
|
||||
yank_files.status = 0;
|
||||
}
|
||||
yank_files.path=getcwd(NULL, 0);
|
||||
yank_files.count = 0;
|
||||
@@ -684,7 +775,7 @@ void yank_file(int passes, int index){
|
||||
strcpy(*yank_files.list, mid_content[selected_file_current].file_name);
|
||||
} else {
|
||||
yank_files.list = malloc(yank_files.count * sizeof(char*));
|
||||
for (i = 0; i < mid_file_count && i < yank_files.count; i++) {
|
||||
for (i = 0; i < mid_file_count; i++) {
|
||||
if (mid_content[i].status & FILE_STATUS_SELECTED) {
|
||||
*yank_files.list = malloc(strlen(mid_content[i].file_name)+1);
|
||||
strcpy(*yank_files.list, mid_content[i].file_name);
|
||||
@@ -708,24 +799,161 @@ void paste(){
|
||||
/*TODO(2025-08-14T22:10:44) escape path*/
|
||||
char *cmd;
|
||||
if (yank_files.status & YANK_COPY) {
|
||||
cmd = concat("false | cp -r -i ", yank_files.path);
|
||||
cmd = concat("false | cp -riv ", yank_files.path);
|
||||
} else {
|
||||
cmd = concat("mv ", yank_files.path);
|
||||
}
|
||||
cmd = concat(cmd, "/");
|
||||
cmd = concat(cmd, *yank_files.list);
|
||||
cmd = concat(cmd, " ./");
|
||||
mvprintw(i, 0, cmd);
|
||||
if (system(cmd) != 0) {
|
||||
cmd = concat(cmd, *yank_files.list);
|
||||
cmd = concat(cmd, " 2>&1");
|
||||
char *line = malloc(255);
|
||||
FILE *cmd_open;
|
||||
while (1) {
|
||||
cmd = concat(cmd, "_");
|
||||
if (system(cmd) == 0) {
|
||||
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, " 2>&1");
|
||||
} 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.count;
|
||||
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();
|
||||
}
|
||||
|
@@ -1,5 +1,3 @@
|
||||
#include <curses.h>
|
||||
#include <pthread.h>
|
||||
#ifndef INTERACTIONS_GUARD
|
||||
#define INTERACTIONS_GUARD
|
||||
#include "interactions.c"
|
||||
@@ -9,10 +7,11 @@
|
||||
void user_interactions();
|
||||
void quit_program();
|
||||
void toggle_selection();
|
||||
void select_all();
|
||||
void move_right();
|
||||
void move_up(int passes);
|
||||
void move_down(int passes);
|
||||
void move_left(int passes);
|
||||
void move_up(unsigned long passes);
|
||||
void move_down(unsigned long passes);
|
||||
void move_left(unsigned long passes);
|
||||
void jump_bottom();
|
||||
void jump_top();
|
||||
void toggle_hidden_files();
|
||||
@@ -22,10 +21,15 @@ void delete();
|
||||
void makedir();
|
||||
void makefile();
|
||||
void update();
|
||||
void enter_shell(int passes, int index);
|
||||
void not_implemented(int passes, int index);
|
||||
void jump_to_dir(int passes, int index);
|
||||
void order_by(int passes, int index);
|
||||
void cmd_on_selected(int passes, int index);
|
||||
void yank_file();
|
||||
void enter_shell(unsigned long passes, int index);
|
||||
void not_implemented(unsigned long passes, int index);
|
||||
void jump_to_dir(unsigned long passes, int index);
|
||||
void order_by(unsigned long passes, int index);
|
||||
void cmd_on_selected(unsigned long passes, int index);
|
||||
void yank_text(unsigned long passes, int index);
|
||||
void yank_file(unsigned long passes, int index);
|
||||
void paste();
|
||||
void search();
|
||||
void search_next();
|
||||
void search_previous();
|
||||
void jmp_file_index();
|
||||
|
67
main.c
67
main.c
@@ -2,15 +2,16 @@
|
||||
#include <curses.h>
|
||||
#include <pthread.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <sys/sysinfo.h>
|
||||
#include <string.h>
|
||||
#include <locale.h>
|
||||
#include "threading.h"
|
||||
#include "window.h"
|
||||
#include <time.h>
|
||||
|
||||
#include "defines.h"
|
||||
#include "threading.h"
|
||||
#include "window.c"
|
||||
#include "colors.h"
|
||||
#include "interactions.h"
|
||||
#include "file_previews.h"
|
||||
|
||||
|
||||
unsigned int terminal_height;
|
||||
unsigned int terminal_width;
|
||||
@@ -20,6 +21,7 @@ unsigned int settings;
|
||||
unsigned int file_modifiers;
|
||||
unsigned int status = (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_MASK | STATUS_RELOAD_DIRECTORY);
|
||||
char *start_path;
|
||||
time_t *seed;
|
||||
|
||||
WINDOW *win_t;
|
||||
WINDOW *win_b;
|
||||
@@ -58,7 +60,17 @@ int main(){
|
||||
|
||||
char threading = 0;
|
||||
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)){
|
||||
getmaxyx(stdscr, terminal_height, terminal_width);
|
||||
@@ -67,29 +79,15 @@ int main(){
|
||||
status |= (STATUS_RUN_BACKEND | STATUS_UPDATE_SCREEN_RESIZE);
|
||||
temp_width = terminal_width;
|
||||
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) {
|
||||
status &= ~(STATUS_RELOAD_DIRECTORY);
|
||||
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);
|
||||
status &= ~(STATUS_RELOAD_DIRECTORY | STATUS_DELTA_TIME);
|
||||
threading = 0;
|
||||
}
|
||||
if (status & STATUS_RUN_BACKEND) {
|
||||
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_b, NULL, thread_btm, &status); /*bottom bar*/
|
||||
pthread_create(&thread_r, NULL, thread_rgt, &status); /*child_content slash win_r*/
|
||||
pthread_cond_signal(&cond_top);
|
||||
pthread_cond_signal(&cond_mid);
|
||||
pthread_cond_signal(&cond_lft);
|
||||
status &= ~(STATUS_RUN_BACKEND);
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
threading = 1;
|
||||
@@ -99,10 +97,22 @@ int main(){
|
||||
|
||||
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();
|
||||
free(start_path);
|
||||
|
||||
/*
|
||||
if (threading) {
|
||||
pthread_join(thread_l, NULL);
|
||||
pthread_join(thread_r, NULL);
|
||||
@@ -110,6 +120,7 @@ int main(){
|
||||
pthread_join(thread_t, NULL);
|
||||
pthread_join(thread_b, NULL);
|
||||
}
|
||||
*/
|
||||
|
||||
delwin(win_l);
|
||||
delwin(win_m);
|
||||
@@ -117,9 +128,9 @@ int main(){
|
||||
delwin(win_t);
|
||||
delwin(win_b);
|
||||
noraw();
|
||||
endwin();
|
||||
curs_set(1);
|
||||
echo();
|
||||
curs_set(1);
|
||||
endwin();
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -190,11 +201,17 @@ void init() {
|
||||
|
||||
threading_init(); /* found in threading.c */
|
||||
colors_init(); /* in colors.c */
|
||||
dir_init(); /*in dir.c */
|
||||
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
|
||||
ueberzug_init(); /* in file_previews.c */
|
||||
#endif
|
||||
|
||||
ESCDELAY = 10;
|
||||
char *start_path = getcwd(NULL, 0);
|
||||
setenv("START_PATH", start_path, 0);
|
||||
free(start_path);
|
||||
|
||||
seed = malloc(sizeof(time_t));
|
||||
*seed = time(NULL);
|
||||
|
||||
}
|
||||
|
264
sorting.c
264
sorting.c
@@ -1,20 +1,24 @@
|
||||
#include <curses.h>
|
||||
#include <dirent.h>
|
||||
#include <strings.h>
|
||||
#include <stdlib.h>
|
||||
#include <strings.h>
|
||||
#include <string.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "defines.h"
|
||||
|
||||
extern unsigned int settings;
|
||||
extern unsigned int file_modifiers;
|
||||
extern time_t *seed;
|
||||
|
||||
int skip_hidden_files(const struct dirent *entry){
|
||||
|
||||
if (entry->d_name[0] == '.') {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int skip_dot(const struct dirent *entry){
|
||||
|
||||
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
|
||||
return 0;
|
||||
}
|
||||
@@ -22,136 +26,178 @@ int skip_dot(const struct dirent *entry){
|
||||
}
|
||||
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
const unsigned char *a = (unsigned char*)((file*)file0)->file_name;
|
||||
const unsigned char *b = (unsigned char*)((file*)file1)->file_name;
|
||||
|
||||
long parsed_number0 = 0;
|
||||
long parsed_number1 = 0;
|
||||
char is_num = 0;
|
||||
char result = 0;
|
||||
|
||||
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 {
|
||||
char *file_name0 = ((file*)file0)->file_name;
|
||||
char *file_name1 = ((file*)file1)->file_name;
|
||||
return strcasecmp(file_name0, file_name1);
|
||||
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){
|
||||
char *file_name0 = ((file*)file0)->file_name;
|
||||
char *file_name1 = ((file*)file1)->file_name;
|
||||
return strcmp(file_name0, file_name1);
|
||||
|
||||
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){
|
||||
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) {
|
||||
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) {
|
||||
if ((((file*)file0)->file_type & FILE_TYPE_DIR) && !(((file*)file1)->file_type & FILE_TYPE_DIR)) {
|
||||
return -1;
|
||||
} else {
|
||||
if (random & 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 << (i + (((time_t)&seed)%5));
|
||||
} else {
|
||||
return 0;
|
||||
num ^= (time_t)&seed;
|
||||
num ^= num >> (i + (((time_t)&num)%5));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (file_type0 > file_type1) {
|
||||
return 1;
|
||||
} else if (file_type0 < file_type1) {
|
||||
return -1;
|
||||
} else {
|
||||
random = random > 1;
|
||||
if ((random & 2) == 2) {
|
||||
return -1;
|
||||
} else {
|
||||
if (random & 1){
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
num ^= getpid();
|
||||
|
||||
return ((num%3) - 1);
|
||||
|
||||
}
|
||||
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;
|
||||
char *file_name0 = ((file*)file0)->file_name;
|
||||
char *file_name1 = ((file*)file1)->file_name;
|
||||
if (file_type0 == file_type1) {
|
||||
return strcasecmp(file_name0, file_name1);
|
||||
} else if (file_type0 == FILE_TYPE_DIR || file_type0 == FILE_TYPE_SYMLINK) {
|
||||
|
||||
if ((((file*)file0)->file_type & FILE_TYPE_DIR) && !(((file*)file1)->file_type & FILE_TYPE_DIR)) {
|
||||
return -1;
|
||||
} else if (file_type1 == FILE_TYPE_DIR || file_type1 == FILE_TYPE_SYMLINK) {
|
||||
return 1;
|
||||
} else {
|
||||
if (file_type0 > file_type1) {
|
||||
return -1;
|
||||
} else if (file_type0 < file_type1) {
|
||||
return 1;
|
||||
} else {
|
||||
return strcasecmp(file_name0, file_name1);
|
||||
}
|
||||
if (!(((file*)file0)->file_type & FILE_TYPE_DIR) && (((file*)file1)->file_type & FILE_TYPE_DIR)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (((file*)file0)->file_type > ((file*)file1)->file_type) {
|
||||
return -1;
|
||||
} else if (((file*)file0)->file_type < ((file*)file1)->file_type) {
|
||||
return 1;
|
||||
} else {
|
||||
return sort_natural(file0, 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;
|
||||
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) {
|
||||
|
||||
if ((((file*)file0)->file_type & FILE_TYPE_DIR) && !(((file*)file1)->file_type & FILE_TYPE_DIR)) {
|
||||
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;
|
||||
}
|
||||
|
||||
if (((file*)file0)->file_size > ((file*)file1)->file_size) {
|
||||
return -1;
|
||||
} else if (((file*)file0)->file_size < ((file*)file1)->file_size) {
|
||||
return 1;
|
||||
} else {
|
||||
return strcasecmp(file_name0, file_name1);
|
||||
}
|
||||
} else {
|
||||
if (file_type0 == FILE_TYPE_DIR || file_type0 == FILE_TYPE_SYMLINK) {
|
||||
return -1;
|
||||
} else if (file_type1 == FILE_TYPE_DIR || file_type1 == FILE_TYPE_SYMLINK) {
|
||||
return 1;
|
||||
} else {
|
||||
if (file_size0 > file_size1) {
|
||||
return -1;
|
||||
} else if (file_size0 < file_size1) {
|
||||
return 1;
|
||||
} else {
|
||||
return strcasecmp(file_name0, file_name1);
|
||||
}
|
||||
}
|
||||
return sort_natural(file0, file1);
|
||||
}
|
||||
}
|
||||
int sort_extension(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;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
312
threading.c
312
threading.c
@@ -5,20 +5,27 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <time.h>
|
||||
#include "defines.h"
|
||||
#include "backend.h"
|
||||
#include <sys/stat.h>
|
||||
#include <sys/statvfs.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "dir.h"
|
||||
#include "file_previews.h"
|
||||
|
||||
|
||||
|
||||
pthread_mutex_t mutex_top;
|
||||
pthread_mutex_t mutex_btm;
|
||||
pthread_mutex_t mutex_lft;
|
||||
pthread_mutex_t mutex_mid;
|
||||
pthread_mutex_t mutex_rgt;
|
||||
pthread_mutex_t mutex_selection;
|
||||
pthread_mutex_t mutex_wait;
|
||||
pthread_cond_t cond_wait;
|
||||
volatile char wait_count; /* this is used to determine how many threads are waiting for cont_wait */
|
||||
pthread_cond_t cond_mid;
|
||||
pthread_cond_t cond_rgt;
|
||||
pthread_cond_t cond_lft;
|
||||
pthread_cond_t cond_top;
|
||||
pthread_cond_t cond_btm;
|
||||
|
||||
|
||||
file *rgt_content;
|
||||
file *mid_content;
|
||||
@@ -27,7 +34,6 @@ char *rgt_buffer; /* used for file previews, unlike rgt_content, which is used f
|
||||
char *btm_buffer;
|
||||
char *top_buffer; /* current path */
|
||||
|
||||
volatile file *file_current;
|
||||
|
||||
|
||||
unsigned long rgt_file_count = 0;
|
||||
@@ -40,12 +46,17 @@ unsigned long top_width;
|
||||
volatile unsigned long selected_file_current = 0;
|
||||
volatile unsigned long selected_file_last = 0;
|
||||
extern unsigned int terminal_width;
|
||||
extern unsigned int status;
|
||||
|
||||
unsigned int btm_status;
|
||||
|
||||
|
||||
void *thread_mid(void *data){
|
||||
unsigned int status = *(unsigned int*)data;
|
||||
void *thread_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;
|
||||
if((path=getcwd(NULL, 0)) == NULL) {
|
||||
@@ -55,86 +66,51 @@ void *thread_mid(void *data){
|
||||
} else {
|
||||
|
||||
|
||||
if (status & STATUS_RELOAD_DIRECTORY) {
|
||||
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) {
|
||||
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";
|
||||
mid_content->file_name = malloc(sizeof(char));
|
||||
mid_content->file_name[0] = '\0';
|
||||
|
||||
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;
|
||||
update_selected_file();
|
||||
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);
|
||||
|
||||
btm_status = local_status;
|
||||
pthread_cond_signal(&cond_rgt);
|
||||
pthread_cond_signal(&cond_btm);
|
||||
|
||||
}
|
||||
free(path);
|
||||
pthread_mutex_unlock(&mutex_mid);
|
||||
}
|
||||
pthread_exit(0);
|
||||
}
|
||||
void *thread_lft(void *data){
|
||||
unsigned int status = *(unsigned int*)data;
|
||||
void *thread_lft(){
|
||||
|
||||
while(!(status & STATUS_QUIT_PROGRAM)){
|
||||
pthread_mutex_lock(&mutex_lft);
|
||||
pthread_cond_wait(&cond_lft, &mutex_lft);
|
||||
unsigned int local_status = status;
|
||||
|
||||
char *path;
|
||||
if((path=getcwd(NULL, 0)) == NULL) {
|
||||
@@ -146,7 +122,7 @@ void *thread_lft(void *data){
|
||||
path[strrchr(path, '/')-path] = '\0';
|
||||
path[0] = '/';
|
||||
|
||||
if (status & STATUS_RELOAD_DIRECTORY) {
|
||||
if (local_status & STATUS_RELOAD_DIRECTORY) {
|
||||
lft_file_count = get_dir_size(path);
|
||||
free(lft_content);
|
||||
lft_content = malloc(lft_file_count * sizeof(file));
|
||||
@@ -157,35 +133,45 @@ void *thread_lft(void *data){
|
||||
}
|
||||
free(path);
|
||||
pthread_mutex_unlock(&mutex_lft);
|
||||
}
|
||||
pthread_exit(0);
|
||||
|
||||
|
||||
}
|
||||
void *thread_rgt(void *data){
|
||||
unsigned int status = *(unsigned int*)data;
|
||||
void *thread_rgt(){
|
||||
|
||||
file file_current;
|
||||
while(!(status & STATUS_QUIT_PROGRAM)){
|
||||
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_cond_wait(&cond_rgt, &mutex_rgt);
|
||||
|
||||
|
||||
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);
|
||||
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 (file_current_type == FILE_TYPE_DIR || file_current_type == FILE_TYPE_SYMLINK) {
|
||||
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);
|
||||
@@ -197,33 +183,51 @@ void *thread_rgt(void *data){
|
||||
free(rgt_buffer);
|
||||
rgt_buffer = malloc(sizeof(char));
|
||||
rgt_buffer[0] = '\0';
|
||||
} else {
|
||||
} 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);
|
||||
if (file_current_status & FILE_STATUS_DIR_EMPTY) {
|
||||
rgt_buffer = "empty dir";
|
||||
} else {
|
||||
rgt_content->file_type = FILE_TYPE_OPEN_FILE;
|
||||
rgt_content->status = FILE_STATUS_HOVER;
|
||||
rgt_buffer = preview_file(path, file_current_size);
|
||||
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);
|
||||
}
|
||||
void *thread_top(void *data){
|
||||
unsigned int status = *(unsigned int*)data;
|
||||
void *thread_top(){
|
||||
|
||||
while(!(status & STATUS_QUIT_PROGRAM)){
|
||||
pthread_mutex_lock(&mutex_top);
|
||||
pthread_cond_wait(&cond_top, &mutex_top);
|
||||
|
||||
free(top_buffer);
|
||||
|
||||
char *path;
|
||||
@@ -232,40 +236,130 @@ void *thread_top(void *data){
|
||||
top_width = sizeof("cannot open directory");
|
||||
top_buffer = "cannot open directory";
|
||||
} else {
|
||||
top_buffer = getcwd(NULL, 0);
|
||||
top_buffer = malloc(strlen(path)+1);
|
||||
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(void *data){
|
||||
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);
|
||||
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;
|
||||
|
||||
|
||||
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);
|
||||
int buffer_width = terminal_width;
|
||||
btm_buffer = malloc(buffer_width);
|
||||
memset(btm_buffer, 0, buffer_width);
|
||||
btm_buffer[0] = (S_ISDIR(file_current->permissions)) ? 'd' : '-';
|
||||
btm_buffer[1] = (file_current->permissions & S_IRUSR) ? 'r' : '-';
|
||||
btm_buffer[2] = (file_current->permissions & S_IWUSR) ? 'w' : '-';
|
||||
btm_buffer[3] = (file_current->permissions & S_IXUSR) ? 'x' : '-';
|
||||
btm_buffer[4] = (file_current->permissions & S_IRGRP) ? 'r' : '-';
|
||||
btm_buffer[5] = (file_current->permissions & S_IWGRP) ? 'w' : '-';
|
||||
btm_buffer[6] = (file_current->permissions & S_IXGRP) ? 'x' : '-';
|
||||
btm_buffer[7] = (file_current->permissions & S_IROTH) ? 'r' : '-';
|
||||
btm_buffer[8] = (file_current->permissions & S_IWOTH) ? 'w' : '-';
|
||||
btm_buffer[9] = (file_current->permissions & S_IXOTH) ? 'x' : '-';
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -291,11 +385,6 @@ void threading_init(){
|
||||
rgt_content->file_name = malloc(sizeof(char));
|
||||
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 */
|
||||
vol = pthread_mutex_init(&mutex_top, NULL);
|
||||
@@ -304,8 +393,11 @@ void threading_init(){
|
||||
vol = pthread_mutex_init(&mutex_btm, NULL);
|
||||
vol = pthread_mutex_init(&mutex_rgt, NULL);
|
||||
vol = pthread_mutex_init(&mutex_selection, NULL);
|
||||
vol = pthread_mutex_init(&mutex_wait, NULL);
|
||||
vol = pthread_cond_init(&cond_wait, NULL);
|
||||
vol = pthread_cond_init(&cond_rgt, 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;
|
||||
selected_file_current = 0;
|
||||
selected_file_last = 0;
|
||||
|
16
threading.h
16
threading.h
@@ -1,10 +1,14 @@
|
||||
#include <curses.h>
|
||||
#include "threading.c"
|
||||
|
||||
void *thread_lft(void *data);
|
||||
void *thread_mid(void *data);
|
||||
void *thread_rgt(void *data);
|
||||
void *thread_top(void *data);
|
||||
void *thread_btm(void *data);
|
||||
#ifndef THREADING_GUARD
|
||||
#define THREADING_GUARD
|
||||
#include "threading.c"
|
||||
#endif
|
||||
|
||||
void *thread_lft();
|
||||
void *thread_mid();
|
||||
void *thread_rgt();
|
||||
void *thread_top();
|
||||
void *thread_btm();
|
||||
void threading_init();
|
||||
void threading_free();
|
||||
|
40
window.c
40
window.c
@@ -1,8 +1,12 @@
|
||||
#include <curses.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
#include "defines.h"
|
||||
|
||||
#include "defines.h"
|
||||
#include "dir.h"
|
||||
|
||||
extern unsigned int terminal_height;
|
||||
extern unsigned int terminal_width;
|
||||
extern unsigned int status;
|
||||
extern char *input;
|
||||
|
||||
@@ -40,15 +44,13 @@ void window_top(WINDOW *win){
|
||||
wattroff(win, COLOR_PAIR(COLOR_PATH));
|
||||
pthread_mutex_unlock(&mutex_top);
|
||||
} else {
|
||||
wprintw(win,"loading");
|
||||
mvwaddstr(win, 0, terminal_width/2, "LOADING");
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
}
|
||||
}
|
||||
void window_btm(WINDOW *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 (*top_buffer != ' ') { /*printing ' ' (standard initialized value, see threading_init) makes valgrind throw a fuss*/
|
||||
mvwprintw(win, 0, 0, "%s", btm_buffer);
|
||||
@@ -57,58 +59,48 @@ void window_btm(WINDOW *win){
|
||||
/*the printing of the input char is done in user_interactions*/
|
||||
/*the printing of all possible inputs are done in user_interactions */
|
||||
} else {
|
||||
mvwprintw(win, local_height/2, local_width/2, "LOADING");
|
||||
mvwaddstr(win, 0, terminal_width/2, "LOADING");
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
}
|
||||
}
|
||||
void window_lft(WINDOW *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) {
|
||||
print_dir(win, 0, &lft_file_count, lft_content);
|
||||
pthread_mutex_unlock(&mutex_lft);
|
||||
|
||||
} else {
|
||||
mvwprintw(win, local_height/2, local_width/2, "LOADING");
|
||||
mvwaddstr(win, terminal_height/2, terminal_width/8, "LOADING");
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
}
|
||||
}
|
||||
void window_mid(WINDOW *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 (mid_file_count == 0) {
|
||||
mvwprintw(win, 0, 0, "empty");
|
||||
mvwaddstr(win, 0, 0, "empty");
|
||||
} else {
|
||||
print_dir(win, 1, &mid_file_count, mid_content);
|
||||
}
|
||||
pthread_mutex_unlock(&mutex_mid);
|
||||
} else {
|
||||
mvwprintw(win, local_height/2, local_width/2, "LOADING");
|
||||
mvwaddstr(win, terminal_height/2, terminal_width/4, "LOADING");
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
}
|
||||
}
|
||||
void window_rgt(WINDOW *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 (rgt_file_count == 0) {
|
||||
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 {
|
||||
mvwprintw(win, 0, 0, "empty");
|
||||
mvwaddstr(win, 0, 0, "empty");
|
||||
}
|
||||
|
||||
|
||||
@@ -117,7 +109,7 @@ void window_rgt(WINDOW *win){
|
||||
}
|
||||
pthread_mutex_unlock(&mutex_rgt);
|
||||
} else {
|
||||
mvwprintw(win, local_height/2, local_width/2, "LOADING");
|
||||
mvwaddstr(win, terminal_height/2, terminal_width/4, "LOADING");
|
||||
status |= STATUS_UPDATE_SCREEN_0;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user