[view] Write ansi output to --output-file

Was writing to stdout all this time!
This commit is contained in:
Behdad Esfahbod 2022-12-29 20:26:42 -07:00
parent 3a319b59bd
commit 0004ec13a6
2 changed files with 24 additions and 11 deletions

View File

@ -29,6 +29,8 @@
#include "hb.hh"
#include <cairo.h>
#include <assert.h>
#include <stdlib.h>
#include <stddef.h>
@ -377,9 +379,12 @@ static inline void
ansi_print_image_rgb24 (const uint32_t *data,
unsigned int width,
unsigned int height,
unsigned int stride)
unsigned int stride,
cairo_write_func_t write_func,
void *closure)
{
image_t image (width, height, data, stride);
char buf[16];
unsigned int rows = (height + CELL_H - 1) / CELL_H;
unsigned int cols = (width + CELL_W - 1) / CELL_W;
@ -392,31 +397,35 @@ ansi_print_image_rgb24 (const uint32_t *data,
bi.set (cell);
if (bi.unicolor) {
if (last_bg != bi.bg) {
printf ("\e[%dm", 40 + bi.bg);
snprintf (buf, sizeof (buf), "\e[%dm", 40 + bi.bg);
write_func (closure, (unsigned char *) buf, strlen (buf));
last_bg = bi.bg;
}
printf (" ");
write_func (closure, (unsigned char *) " ", 1);
} else {
/* Figure out the closest character to the biimage */
bool inverse = false;
const char *c = block_best (bi, &inverse);
if (inverse) {
if (last_bg != bi.fg || last_fg != bi.bg) {
printf ("\e[%d;%dm", 30 + bi.bg, 40 + bi.fg);
snprintf (buf, sizeof (buf), "\e[%d;%dm", 30 + bi.bg, 40 + bi.fg);
write_func (closure, (unsigned char *) buf, strlen (buf));
last_bg = bi.fg;
last_fg = bi.bg;
}
} else {
if (last_bg != bi.bg || last_fg != bi.fg) {
printf ("\e[%d;%dm", 40 + bi.bg, 30 + bi.fg);
snprintf (buf, sizeof (buf), "\e[%d;%dm", 40 + bi.bg, 30 + bi.fg);
write_func (closure, (unsigned char *) buf, strlen (buf));
last_bg = bi.bg;
last_fg = bi.fg;
}
}
printf ("%s", c);
write_func (closure, (unsigned char *) c, strlen (c));
}
}
printf ("\e[0m\n"); /* Reset */
snprintf (buf, sizeof (buf), "\e[0m\n"); /* Reset */
write_func (closure, (unsigned char *) buf, strlen (buf));
last_bg = last_fg = -1;
}
}

View File

@ -41,7 +41,9 @@
# define CELL_H (2 * CELL_W)
static void
chafa_print_image_rgb24 (const void *data, int width, int height, int stride, int level)
chafa_print_image_rgb24 (const void *data, int width, int height, int stride, int level,
cairo_write_func_t write_func,
void *closure)
{
ChafaTermInfo *term_info;
ChafaSymbolMap *symbol_map;
@ -114,7 +116,7 @@ chafa_print_image_rgb24 (const void *data, int width, int height, int stride, in
/* Print the string */
fwrite (gs->str, sizeof (char), gs->len, stdout);
write_func (closure, (const unsigned char *) gs->str, gs->len);
if (pixel_mode != CHAFA_PIXEL_MODE_SIXELS)
fputc ('\n', stdout);
@ -209,10 +211,12 @@ helper_cairo_surface_write_to_ansi_stream (cairo_surface_t *surface,
if (env)
chafa_level = atoi (env);
if (chafa_level)
chafa_print_image_rgb24 (data, width, height, stride, chafa_level);
chafa_print_image_rgb24 (data, width, height, stride, chafa_level,
write_func, closure);
else
#endif
ansi_print_image_rgb24 (data, width, height, stride / 4);
ansi_print_image_rgb24 (data, width, height, stride / 4,
write_func, closure);
}
cairo_surface_destroy (surface);