From fe781c2d3cafa9f5350dc25635837f14fd0ed251 Mon Sep 17 00:00:00 2001 From: nova Date: Wed, 24 Sep 2025 22:44:13 +0200 Subject: [PATCH] image previews can now be disabled --- config.h | 1 + file_previews.c | 66 ++++++++++++++++++++++++++++++------------------- main.c | 4 ++- threading.c | 1 - 4 files changed, 45 insertions(+), 27 deletions(-) diff --git a/config.h b/config.h index 6110ad6..c0ee4b2 100644 --- a/config.h +++ b/config.h @@ -1,4 +1,5 @@ #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 */ /* {{{ */ #ifndef CONFIG_GUARD diff --git a/file_previews.c b/file_previews.c index 20c4da9..32c4dce 100644 --- a/file_previews.c +++ b/file_previews.c @@ -1,9 +1,14 @@ #include #include #include -#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 diff --git a/main.c b/main.c index ef2f0a0..083a8fc 100644 --- a/main.c +++ b/main.c @@ -183,8 +183,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); diff --git a/threading.c b/threading.c index f295186..3952dd4 100644 --- a/threading.c +++ b/threading.c @@ -157,7 +157,6 @@ 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) { - images_clear(); unsigned long i = 0; for (i = 0; i < rgt_file_count; i++) {