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

Compare commits

...

4 Commits

Author SHA1 Message Date
nova
21a11d8be1 fixed images not being flushed when going ontop of a directory 2025-09-24 23:14:20 +02:00
nova
b46d2da308 delta time to reload directory 2025-09-24 23:07:29 +02:00
nova
fe781c2d3c image previews can now be disabled 2025-09-24 22:44:13 +02:00
nova
fb1af6d2d2 now printing left storage & related refactoring 2025-09-24 22:20:07 +02:00
5 changed files with 114 additions and 38 deletions

View File

@@ -1,10 +1,14 @@
#define SETTINGS_LINE_NUMBERS 2 /* 0 is disabled, 1 is enabled, 2 is relative */
#define SETTINGS_UEBERZUG_IMAGE_PREVIEW 1 /* 0 is disabled, 1 is enabled, 2 is with caching */
#define SETTINGS_RELOAD_DIR_DELTA 10 /* 0 is disabled, time in seconds of how often the directory should be reload */
/* {{{ */
#ifndef CONFIG_GUARD
#define CONFIG_GUARD
#include "defines.h"
#include "sorting.h"
#include "interactions.h"
#define SETTINGS_LINE_NUMBERS 2 /* 0 is disabled, 1 is enabled, 2 is relative */
/* }}} */
static const mimetype mimetype_default_cmd[] = {
/* mimetype shell command
@@ -93,9 +97,17 @@ 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 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 mimetype mimetype_default_cmd[];
static const extension file_extension_default_cmd[];
@@ -103,5 +115,7 @@ 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
/* }}} */

16
dir.c
View File

@@ -12,6 +12,8 @@
#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;
@@ -160,7 +162,6 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
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;
@@ -229,16 +230,15 @@ void print_dir(WINDOW *win, char print_info, unsigned long *dir_file_count, file
if (print_info) {
file_size = dir_content[i].file_size;
char size_index = 0;
while (file_size > 1) {
char size_index = -1;
do {
printed_size=file_size;
file_size /= 1024;
size_index++;
if (size_index >= 6) {
break;
}
}
size_char = sizes[size_index-1];
} 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 || 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') {

View File

@@ -1,9 +1,14 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "backend.h"
#include "backend.h"
#include "defines.h"
#include "config.h"
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
static FILE *ueberzug = NULL;
#endif
extern unsigned int terminal_height;
extern unsigned int terminal_width;
char previewd;
@@ -43,14 +48,19 @@ 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);
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
} else if (strstr(mime, "image")) {
file_buffer = generic(file_name);
images_print(file_name);
previewd = 1;
#endif
} else {
file_buffer = generic(file_name);
}
@@ -74,28 +84,6 @@ char* text(char *path, unsigned long *file_size){
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, "\"");
@@ -111,6 +99,34 @@ 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
}
#endif

17
main.c
View File

@@ -60,6 +60,11 @@ 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*/
@@ -92,6 +97,14 @@ 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);
}
#endif
}
threading_free();
free(start_path);
@@ -183,8 +196,10 @@ void init() {
threading_init(); /* found in threading.c */
colors_init(); /* in colors.c */
ueberzug_init(); /* in file_previews.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);

View File

@@ -6,7 +6,9 @@
#include <unistd.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include "config.h"
#include "dir.h"
#include "file_previews.h"
@@ -155,7 +157,9 @@ void *thread_rgt(){
if (mid_content[selected_file_current].permissions & S_IRUSR) {
if (file_current.file_type == FILE_TYPE_DIR || file_current.file_type == FILE_TYPE_SYMLINK) {
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
images_clear();
#endif
unsigned long i = 0;
for (i = 0; i < rgt_file_count; i++) {
@@ -246,7 +250,8 @@ void *thread_btm(){
free(btm_buffer);
int buffer_width = terminal_width;
btm_buffer = malloc(buffer_width);
memset(btm_buffer, 0, buffer_width);
memset(btm_buffer, ' ', buffer_width);
btm_buffer[buffer_width] = '\0';
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' : '-';
@@ -259,6 +264,32 @@ void *thread_btm(){
btm_buffer[9] = (mid_content[selected_file_current].permissions & S_IXOTH) ? 'x' : '-';
char *path;
path=getcwd(NULL, 0);
struct statvfs fs;
statvfs(path, &fs);
float disk_size_free = fs.f_bsize * fs.f_bavail;
float parsed_number = 0;
int offset_back = buffer_width - strlen(ui_btm_text_storage_left);
strcpy(btm_buffer + offset_back, ui_btm_text_storage_left);
char *float_str = malloc(buffer_width / 4);
char size_index = -1;
do {
parsed_number=disk_size_free;
disk_size_free /= 1024;
size_index++;
} while (disk_size_free > 1 && size_index < size_unit_count);
snprintf(float_str, 10, "%0.2lf", parsed_number);
memcpy(btm_buffer + (offset_back - strlen(float_str) - 1), float_str, strlen(float_str));
btm_buffer[offset_back - 2] = size_unit[(unsigned)size_index];
pthread_mutex_unlock(&mutex_btm);
}
pthread_exit(0);