img2sixel exits by Ctrl+C. Enable GIF Animation if 'I' is pressed to show it.

This commit is contained in:
Araki Ken
2014-12-06 20:47:05 +09:00
committed by Tatsuya Kinoshita
parent 75e89acd80
commit 4459dbe26e
2 changed files with 50 additions and 12 deletions
+7 -1
View File
@@ -5917,8 +5917,14 @@ deleteFiles()
Firstbuf = buf; Firstbuf = buf;
} }
} }
while ((f = popText(fileToDelete)) != NULL) while ((f = popText(fileToDelete)) != NULL) {
unlink(f); unlink(f);
if (enable_inline_image == 2 && strcmp(f+strlen(f)-4, ".gif") == 0) {
Str firstframe = Strnew_charp(f);
Strcat_charp(firstframe, "-1");
unlink(firstframe->ptr);
}
}
} }
void void
+43 -11
View File
@@ -511,7 +511,7 @@ skip_gif_header(u_char *p)
return p; return p;
} }
static void static Str
save_first_animation_frame(const char *path) save_first_animation_frame(const char *path)
{ {
int fd; int fd;
@@ -521,14 +521,21 @@ save_first_animation_frame(const char *path)
u_char *body; u_char *body;
u_char *p; u_char *p;
ssize_t len; ssize_t len;
Str new_path;
new_path = Strnew_charp(path);
Strcat_charp(new_path, "-1");
if (stat(new_path->ptr, &st) == 0) {
return new_path;
}
if ((fd = open( path, O_RDONLY)) < 0) { if ((fd = open( path, O_RDONLY)) < 0) {
return 0; return NULL;
} }
if (fstat( fd, &st) != 0 || ! (header = GC_malloc( st.st_size))){ if (fstat( fd, &st) != 0 || ! (header = GC_malloc( st.st_size))){
close( fd); close( fd);
return 0; return NULL;
} }
len = read(fd, header, st.st_size); len = read(fd, header, st.st_size);
@@ -537,7 +544,7 @@ save_first_animation_frame(const char *path)
/* Header */ /* Header */
if (len != st.st_size || strncmp(header, "GIF89a", 6) != 0) { if (len != st.st_size || strncmp(header, "GIF89a", 6) != 0) {
return 0; return NULL;
} }
p = skip_gif_header(header); p = skip_gif_header(header);
@@ -554,7 +561,7 @@ save_first_animation_frame(const char *path)
if (*(p++) == 0x21 && *(p++) == 0xf9 && *(p++) == 0x04) { if (*(p++) == 0x21 && *(p++) == 0xf9 && *(p++) == 0x04) {
if( body) { if( body) {
/* Graphic Control Extension */ /* Graphic Control Extension */
save_gif(path, header, header_size, body, p - 3 - body); save_gif(new_path->ptr, header, header_size, body, p - 3 - body);
break; break;
} }
else { else {
@@ -564,30 +571,48 @@ save_first_animation_frame(const char *path)
} }
} }
return 1; return new_path;
} }
void ttymode_set(int mode, int imode);
void void
put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh) put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, int sh)
{ {
pid_t pid; pid_t pid;
int do_anim;
MySignalHandler(*volatile previntr) (SIGNAL_ARG);
MySignalHandler(*volatile prevquit) (SIGNAL_ARG);
MySignalHandler(*volatile prevstop) (SIGNAL_ARG);
MOVE(y,x); MOVE(y,x);
flush_tty(); flush_tty();
if (!strstr(url, "://")) { do_anim = (x == 0 && y == 0 && sx == 0 && sy == 0) ;
save_first_animation_frame(url);
} previntr = mySignal(SIGINT, SIG_IGN);
prevquit = mySignal(SIGQUIT, SIG_IGN);
prevstop = mySignal(SIGTSTP, SIG_IGN);
if ((pid = fork()) == 0) { if ((pid = fork()) == 0) {
char *argv[11]; char *argv[11];
char digit[2][11+1]; char digit[2][11+1];
char clip[44+3+1]; char clip[44+3+1];
Str str_url;
close(STDERR_FILENO); /* Don't output error message. */
if (do_anim) {
writestr("\x1b[?80h");
}
else if (!strstr(url, "://") && strcmp(url+strlen(url)-4, ".gif") == 0 &&
(str_url = save_first_animation_frame(url))) {
url = str_url->ptr;
}
ttymode_set(ISIG, 1);
close(STDERR_FILENO);
argv[0] = "img2sixel"; argv[0] = "img2sixel";
argv[1] = "-l"; argv[1] = "-l";
argv[2] = "disable"; argv[2] = do_anim ? "auto" : "disable";
argv[3] = "-w"; argv[3] = "-w";
sprintf(digit[0], "%d", w*pixel_per_char_i); sprintf(digit[0], "%d", w*pixel_per_char_i);
argv[4] = digit[0]; argv[4] = digit[0];
@@ -606,6 +631,13 @@ put_image_sixel(char *url, int x, int y, int w, int h, int sx, int sy, int sw, i
else if (pid > 0) { else if (pid > 0) {
int status; int status;
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
ttymode_set(ISIG, 0);
mySignal(SIGINT, previntr);
mySignal(SIGQUIT, prevquit);
mySignal(SIGTSTP, prevstop);
if (do_anim) {
writestr("\x1b[?80l");
}
} }
MOVE(Currentbuf->cursorY,Currentbuf->cursorX); MOVE(Currentbuf->cursorY,Currentbuf->cursorX);