From 7ab04a5a87410ace183ea432a606c4f2921d72ea Mon Sep 17 00:00:00 2001 From: nova Date: Wed, 6 Aug 2025 22:25:39 +0200 Subject: [PATCH] added image previews using ueberzug --- file_previews.c | 39 +++++++++++++++++++++++++++++++++++++++ file_previews.h | 7 ++++++- main.c | 5 ++++- threading.c | 1 + 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/file_previews.c b/file_previews.c index c1ee09e..5207277 100644 --- a/file_previews.c +++ b/file_previews.c @@ -3,8 +3,14 @@ #include #include "defines.h" +static FILE *ueberzug = NULL; +extern unsigned int terminal_height; +extern unsigned int terminal_width; +char previewd; char* text(char *path, unsigned long *file_size); +void images_print(char *file_name); +void images_clear(); char* generic(char *path); char* get_mimetype(char *path){ @@ -43,6 +49,14 @@ char* preview_file(char *file_name, unsigned long file_size){ } else { file_buffer = generic(file_name); } + + if (strstr(mime, "image")) { + images_print(file_name); + previewd = 1; + } else { + images_clear(); + } + free(mime); return file_buffer; @@ -58,6 +72,28 @@ 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, "\""); @@ -73,3 +109,6 @@ char* generic(char *path){ return "failed executing shell command \"file\""; } } +void ueberzug_init(){ + ueberzug = popen("ueberzug layer -s ", "w"); +} diff --git a/file_previews.h b/file_previews.h index 3db07e9..5107621 100644 --- a/file_previews.h +++ b/file_previews.h @@ -1,5 +1,10 @@ -#include "file_previews.c" #include "defines.h" +#ifndef PREVIEW_GUARD +#define PREVIEW_GUARD +#include "file_previews.c" +#endif char* preview_file(char *file_name, unsigned long file_size); char* get_mimetype(char *path); +void images_clear(); +void ueberzug_init(); diff --git a/main.c b/main.c index a2fbf85..a74eb3c 100644 --- a/main.c +++ b/main.c @@ -10,6 +10,7 @@ #include "defines.h" #include "colors.h" #include "interactions.h" +#include "file_previews.h" unsigned int terminal_height; unsigned int terminal_width; @@ -188,7 +189,9 @@ void init() { } threading_init(); /* found in threading.c */ - colors_init(); + colors_init(); /* in colors.c */ + ueberzug_init(); /* in file_previews.c */ + ESCDELAY = 10; char *start_path = getcwd(NULL, 0); setenv("START_PATH", start_path, 0); diff --git a/threading.c b/threading.c index ac59059..9a9d9a7 100644 --- a/threading.c +++ b/threading.c @@ -180,6 +180,7 @@ void *thread_rgt(void *data){ pthread_mutex_unlock(&mutex_selection); if (file_current_type == FILE_TYPE_DIR || file_current_type == FILE_TYPE_SYMLINK) { + images_clear(); unsigned long i = 0; for (i = 0; i < rgt_file_count; i++) {