Add image preview support with libsixel
This commit is contained in:
parent
ec67b06594
commit
0caf9527a5
1 changed files with 76 additions and 59 deletions
17
ccc.c
17
ccc.c
|
@ -13,6 +13,8 @@
|
|||
#include <sys/stat.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
#include <sixel.h>
|
||||
|
||||
#include "config.h"
|
||||
#include "file.h"
|
||||
#include "icons.h"
|
||||
|
@ -870,6 +872,7 @@ void show_file_content(void)
|
|||
return;
|
||||
}
|
||||
|
||||
if (strstr(current_file.name, ".jpg") == NULL && strstr(current_file.name, ".png") == NULL) {
|
||||
int c;
|
||||
/* Check if its binary */
|
||||
while ((c = fgetc(file)) != EOF) {
|
||||
|
@ -878,6 +881,7 @@ void show_file_content(void)
|
|||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
int pipe_fd[2];
|
||||
if (pipe(pipe_fd) == -1) {
|
||||
perror("pipe");
|
||||
|
@ -887,14 +891,26 @@ void show_file_content(void)
|
|||
if (pid == 0) {
|
||||
/* Child */
|
||||
move_cursor(1, half_width);
|
||||
if (strstr(current_file.name, ".jpg") || strstr(current_file.name, ".png")) {
|
||||
sixel_encoder_t *encoder = NULL;
|
||||
sixel_encoder_new(&encoder, NULL);
|
||||
/* Should be enough for most terminal */
|
||||
char width[5];
|
||||
snprintf(width, 5, "%d", (cols - half_width) * 6);
|
||||
sixel_encoder_setopt(encoder, 'w', width);
|
||||
sixel_encoder_encode(encoder, current_file.name);
|
||||
sixel_encoder_unref(encoder);
|
||||
} else {
|
||||
close(pipe_fd[0]);
|
||||
dup2(pipe_fd[1], STDOUT_FILENO);
|
||||
dup2(pipe_fd[1], STDERR_FILENO);
|
||||
close(pipe_fd[1]);
|
||||
execlp("vip", "vip", "-c", current_file.name, NULL);
|
||||
}
|
||||
_exit(1);
|
||||
} else if (pid > 0) {
|
||||
/* Parent */
|
||||
if (!strstr(current_file.name, ".jpg") && !strstr(current_file.name, ".png")) {
|
||||
close(pipe_fd[1]);
|
||||
char buffer[4096];
|
||||
int row = 1;
|
||||
|
@ -949,6 +965,7 @@ void show_file_content(void)
|
|||
/* Line ends with ANSI reset, print it */
|
||||
printf("\033[0m");
|
||||
}
|
||||
}
|
||||
fclose(stream);
|
||||
}
|
||||
waitpid(pid, NULL, 0);
|
||||
|
|
Loading…
Reference in a new issue