135 lines
2.7 KiB
C
135 lines
2.7 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.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;
|
|
|
|
char* text(file *f);
|
|
void images_print(char *file_name);
|
|
void images_clear();
|
|
char* generic(file *f);
|
|
|
|
char* get_mimetype(file *f){
|
|
static const char *cmd_str = "file --mime-type -b";
|
|
char *cmd = parse_cmd(cmd_str, f);
|
|
|
|
FILE *cmd_open = popen(cmd, "r");
|
|
char *line = NULL;
|
|
size_t size = 0;
|
|
|
|
free(cmd);
|
|
if (getline(&line, &size, cmd_open) != -1){
|
|
pclose(cmd_open);
|
|
return line;
|
|
} else {
|
|
pclose(cmd_open);
|
|
return "unknown";
|
|
}
|
|
}
|
|
char* preview_file(file *f){
|
|
/* this calls "file" on path */
|
|
|
|
char *file_buffer;
|
|
|
|
|
|
char *mime = get_mimetype(f);
|
|
|
|
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
|
|
images_clear();
|
|
#endif
|
|
|
|
if (strstr(mime, "text")) {
|
|
file_buffer = text(f);
|
|
#if SETTINGS_UEBERZUG_IMAGE_PREVIEW != 0
|
|
} else if (strstr(mime, "image")) {
|
|
file_buffer = generic(f);
|
|
images_print(f->file_name);
|
|
previewd = 1;
|
|
#endif
|
|
} else {
|
|
file_buffer = generic(f);
|
|
}
|
|
|
|
free(mime);
|
|
return file_buffer;
|
|
|
|
}
|
|
char* text(file *f){
|
|
|
|
unsigned long size = (terminal_width/2) * terminal_height;
|
|
if (size > f->file_size) {
|
|
size = f->file_size;
|
|
}
|
|
char *file_buffer = malloc(size + 1);
|
|
FILE *fp = fopen(f->file_name, "r");
|
|
if (fread(file_buffer, size, 1, fp) != 0) {
|
|
fclose(fp);
|
|
file_buffer[size] = '\0';
|
|
return file_buffer;
|
|
} else {
|
|
fclose(fp);
|
|
return "failed reading file";
|
|
}
|
|
}
|
|
char* generic(file *f){
|
|
static const char *cmd_str = "file ";
|
|
char *cmd = parse_cmd(cmd_str, f);
|
|
|
|
FILE *cmd_open = popen(cmd, "r");
|
|
char *line = NULL;
|
|
size_t size = 0;
|
|
|
|
free(cmd);
|
|
if (getline(&line, &size, cmd_open) != -1) {
|
|
pclose(cmd_open);
|
|
return line;
|
|
} else {
|
|
pclose(cmd_open);
|
|
return "failed executing shell command \"file\"";
|
|
}
|
|
}
|
|
#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
|
|
}
|
|
void ueberzug_close(){
|
|
images_clear();
|
|
pclose(ueberzug);
|
|
}
|
|
#endif
|