ccc/patches/image-preview.patch

89 lines
2.4 KiB
Diff
Raw Permalink Normal View History

2024-11-13 10:48:13 +01:00
From: night0721 <night@night0721.xyz>
2024-11-21 13:06:27 +01:00
Date: Thu, 21 Nov 2024 12:04:40 +0000
2024-11-13 10:50:08 +01:00
Subject: [PATCH] Image preview support with libsixel
2024-11-13 10:48:13 +01:00
---
2024-11-21 13:06:27 +01:00
Makefile | 4 +++-
ccc.c | 17 +++++++++++++++++
2 files changed, 20 insertions(+), 1 deletion(-)
2024-11-13 10:48:13 +01:00
diff --git a/Makefile b/Makefile
index 2d4cde2..b7d9f80 100644
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,9 @@ PREFIX ?= /usr/local
BINDIR = $(PREFIX)/bin
MANDIR = $(PREFIX)/share/man/man1
-CFLAGS = -Os -march=native -mtune=native -pipe -s -flto -std=c99 -pedantic -Wall -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600
+LDFLAGS != pkg-config --libs libsixel
+INCFLAGS != pkg-config --cflags libsixel
+CFLAGS = -Os -march=native -mtune=native -pipe -s -flto -std=c99 -pedantic -Wall -D_DEFAULT_SOURCE -D_XOPEN_SOURCE=600 $(INCFLAGS)
SRC = ccc.c util.c file.c icons.c
diff --git a/ccc.c b/ccc.c
2024-11-21 13:06:27 +01:00
index c3df5ff..2e3cc14 100644
2024-11-13 10:48:13 +01:00
--- a/ccc.c
+++ b/ccc.c
@@ -13,6 +13,8 @@
2024-11-21 13:06:27 +01:00
#include <termios.h>
#include <time.h>
2024-11-13 10:48:13 +01:00
+#include <sixel.h>
+
#include "icons.h"
2024-11-21 13:06:27 +01:00
#include "file.h"
#include "util.h"
@@ -921,12 +923,14 @@ void show_file_content(void)
2024-11-13 10:48:13 +01:00
2024-11-21 13:06:27 +01:00
int c;
/* Check if its binary */
2024-11-13 10:48:13 +01:00
+ if (strstr(current_file.name, ".jpg") == NULL && strstr(current_file.name, ".png") == NULL) {
2024-11-21 13:06:27 +01:00
while ((c = fgetc(file)) != EOF) {
if (c == '\0') {
bprintf("binary");
return;
2024-11-13 10:48:13 +01:00
}
}
2024-11-21 13:06:27 +01:00
+ }
2024-11-13 10:48:13 +01:00
int pipe_fd[2];
2024-11-21 13:06:27 +01:00
if (pipe(pipe_fd) == -1) {
perror("pipe");
@@ -936,14 +940,26 @@ void show_file_content(void)
2024-11-13 10:48:13 +01:00
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 {
2024-11-21 13:06:27 +01:00
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);
2024-11-13 10:48:13 +01:00
+ }
_exit(1);
} else if (pid > 0) {
/* Parent */
+ if (!strstr(current_file.name, ".jpg") && !strstr(current_file.name, ".png")) {
2024-11-21 13:06:27 +01:00
close(pipe_fd[1]);
char buffer[4096];
int row = 1;
@@ -988,6 +1004,7 @@ void show_file_content(void)
2024-11-13 10:48:13 +01:00
}
}
2024-11-21 13:06:27 +01:00
fclose(stream);
+ }
2024-11-13 10:48:13 +01:00
waitpid(pid, NULL, 0);
} else {
wpprintw("fork failed: %s", strerror(errno));
2024-11-21 13:06:27 +01:00
--