added image previews using ueberzug
This commit is contained in:
@@ -3,8 +3,14 @@
|
||||
#include <string.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);
|
||||
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");
|
||||
}
|
||||
|
Reference in New Issue
Block a user