added image previews using ueberzug

This commit is contained in:
nova
2025-08-06 22:25:39 +02:00
parent ec4d2dec8b
commit 7ab04a5a87
4 changed files with 50 additions and 2 deletions

View File

@@ -3,8 +3,14 @@
#include <string.h> #include <string.h>
#include "defines.h" #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); char* text(char *path, unsigned long *file_size);
void images_print(char *file_name);
void images_clear();
char* generic(char *path); char* generic(char *path);
char* get_mimetype(char *path){ char* get_mimetype(char *path){
@@ -43,6 +49,14 @@ char* preview_file(char *file_name, unsigned long file_size){
} else { } else {
file_buffer = generic(file_name); file_buffer = generic(file_name);
} }
if (strstr(mime, "image")) {
images_print(file_name);
previewd = 1;
} else {
images_clear();
}
free(mime); free(mime);
return file_buffer; return file_buffer;
@@ -58,6 +72,28 @@ char* text(char *path, unsigned long *file_size){
return "failed reading file"; return "failed reading file";
} }
} }
void images_clear() {
if (previewd == 1) {
fprintf(ueberzug, "{\"action\": \"remove\", \
\"identifier\": \"preview\"}\n");
fflush(ueberzug);
previewd = 0;
}
}
void images_print(char *file_name) {
char *path=getcwd(NULL, 0);
fprintf(ueberzug, "{\"action\": \"remove\", \
\"identifier\": \"preview\"}\n");
fprintf(ueberzug, "{\"action\":\"add\", \
\"identifier\":\"preview\", \
\"max_height\":%d, \
\"max_width\":%d, \
\"y\":0, \
\"x\":%d, \
\"path\":\"%s/%s\"}\n", terminal_height, terminal_width/2, terminal_width/2, path, file_name);
fflush(ueberzug);
free(path);
}
char* generic(char *path){ char* generic(char *path){
char *cmd = concat("file ./\"", path); char *cmd = concat("file ./\"", path);
cmd = concat(cmd, "\""); cmd = concat(cmd, "\"");
@@ -73,3 +109,6 @@ char* generic(char *path){
return "failed executing shell command \"file\""; return "failed executing shell command \"file\"";
} }
} }
void ueberzug_init(){
ueberzug = popen("ueberzug layer -s ", "w");
}

View File

@@ -1,5 +1,10 @@
#include "file_previews.c"
#include "defines.h" #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* preview_file(char *file_name, unsigned long file_size);
char* get_mimetype(char *path); char* get_mimetype(char *path);
void images_clear();
void ueberzug_init();

5
main.c
View File

@@ -10,6 +10,7 @@
#include "defines.h" #include "defines.h"
#include "colors.h" #include "colors.h"
#include "interactions.h" #include "interactions.h"
#include "file_previews.h"
unsigned int terminal_height; unsigned int terminal_height;
unsigned int terminal_width; unsigned int terminal_width;
@@ -188,7 +189,9 @@ void init() {
} }
threading_init(); /* found in threading.c */ threading_init(); /* found in threading.c */
colors_init(); colors_init(); /* in colors.c */
ueberzug_init(); /* in file_previews.c */
ESCDELAY = 10; ESCDELAY = 10;
char *start_path = getcwd(NULL, 0); char *start_path = getcwd(NULL, 0);
setenv("START_PATH", start_path, 0); setenv("START_PATH", start_path, 0);

View File

@@ -180,6 +180,7 @@ void *thread_rgt(void *data){
pthread_mutex_unlock(&mutex_selection); pthread_mutex_unlock(&mutex_selection);
if (file_current_type == FILE_TYPE_DIR || file_current_type == FILE_TYPE_SYMLINK) { if (file_current_type == FILE_TYPE_DIR || file_current_type == FILE_TYPE_SYMLINK) {
images_clear();
unsigned long i = 0; unsigned long i = 0;
for (i = 0; i < rgt_file_count; i++) { for (i = 0; i < rgt_file_count; i++) {