From 0004ec13a6334f6a279922c4f7111277bec20a60 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 29 Dec 2022 20:26:42 -0700 Subject: [PATCH] [view] Write ansi output to --output-file Was writing to stdout all this time! --- util/ansi-print.hh | 23 ++++++++++++++++------- util/helper-cairo-ansi.hh | 12 ++++++++---- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/util/ansi-print.hh b/util/ansi-print.hh index f53c62356..ab9087343 100644 --- a/util/ansi-print.hh +++ b/util/ansi-print.hh @@ -29,6 +29,8 @@ #include "hb.hh" +#include + #include #include #include @@ -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; } } diff --git a/util/helper-cairo-ansi.hh b/util/helper-cairo-ansi.hh index 9708ee9e8..fc870180c 100644 --- a/util/helper-cairo-ansi.hh +++ b/util/helper-cairo-ansi.hh @@ -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);