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

image previews can now be disabled

This commit is contained in:
nova
2025-09-24 22:44:13 +02:00
parent fb1af6d2d2
commit fe781c2d3c
4 changed files with 45 additions and 27 deletions

View File

@@ -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

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

4
main.c
View File

@@ -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);

View File

@@ -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++) {