Skip to content

Commit 07795cc

Browse files
vo_chafa: tie rendering options to terminal
vo_chafa: select pixel and canvas mode automatically by default DOCS/man/vo.rst: document that pixel and canvas modes are automatically selected by default vo_chafa: initialize term_info, pixel, and canvas modes in the `update_chafa_canvas` function vo_chafa: use the TA_FREEP macro where applicable
1 parent 35be816 commit 07795cc

2 files changed

Lines changed: 26 additions & 10 deletions

File tree

DOCS/man/vo.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,10 @@ Available video output drivers are:
630630
Selects the graphics protocol that chafa will render graphics with.
631631
Can be one of the below list
632632

633-
symbols (Default)
633+
auto (Default)
634+
Infers the best pixel mode to use based on
635+
what your terminal supports.
636+
symbols
634637
Writes unicode characters to the terminal
635638
sixels
636639
Uses the sixel protocol to render graphics.
@@ -643,6 +646,9 @@ Available video output drivers are:
643646
Selects the colorspace that chafa will render graphics with.
644647
Can be one of the below list as per chafa's documentation.
645648

649+
auto (Default)
650+
Infers the best canvas mode to use based on
651+
what your terminal supports.
646652
truecolor (Default)
647653
Truecolor (sRGB)
648654
256

video/out/vo_chafa.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ static void dealloc_canvas_and_buffers(struct vo *vo)
9797
}
9898

9999
if (priv->frame) {
100-
talloc_free(priv->frame);
101-
priv->frame = NULL;
100+
TA_FREEP(&priv->frame);
102101
}
103102
}
104103

@@ -253,6 +252,10 @@ static int update_chafa_canvas(struct vo *vo, struct mp_image_params *params)
253252
if (mp_sws_reinit(priv->sws) < 0)
254253
return -1;
255254

255+
gchar **envp = g_get_environ();
256+
priv->term_info = chafa_term_db_detect(chafa_term_db_get_default (), envp);
257+
g_strfreev (envp);
258+
256259
priv->config = chafa_canvas_config_new();
257260

258261
// Set geometry based on terminal character cells
@@ -267,10 +270,16 @@ static int update_chafa_canvas(struct vo *vo, struct mp_image_params *params)
267270

268271
if (priv->opts.canvas_mode >= 0 && priv->opts.canvas_mode < CHAFA_CANVAS_MODE_MAX) {
269272
chafa_canvas_config_set_canvas_mode(priv->config, priv->opts.canvas_mode);
273+
} else {
274+
ChafaCanvasMode mode = chafa_term_info_get_best_canvas_mode(priv->term_info);
275+
chafa_canvas_config_set_canvas_mode(priv->config, mode);
270276
}
271277

272278
if (priv->opts.dither_mode >= 0 && priv->opts.dither_mode < CHAFA_DITHER_MODE_MAX) {
273279
chafa_canvas_config_set_dither_mode(priv->config, priv->opts.dither_mode);
280+
} else {
281+
ChafaPixelMode mode = chafa_term_info_get_best_pixel_mode(priv->term_info);
282+
chafa_canvas_config_set_pixel_mode(priv->config, mode);
274283
}
275284

276285
if (priv->opts.work_factor > 0) {
@@ -398,7 +407,7 @@ static bool draw_frame(struct vo *vo, struct vo_frame *frame)
398407
priv->frame->stride[0]);
399408

400409
if (mpi)
401-
talloc_free(mpi);
410+
TA_FREEP(&mpi);
402411

403412
done:
404413
return VO_TRUE;
@@ -423,6 +432,7 @@ static void flip_page(struct vo *vo)
423432

424433
chafa_canvas_print_rows(priv->canvas, priv->term_info, &output, &rows);
425434

435+
chafa_strwrite(TERM_ESC_SYNC_UPDATE_BEGIN);
426436

427437
for (int i = 0; output [i]; i++)
428438
{
@@ -433,6 +443,8 @@ static void flip_page(struct vo *vo)
433443
chafa_write(output[i]->str, output[i]->len, stdout);
434444
}
435445

446+
chafa_strwrite(TERM_ESC_SYNC_UPDATE_END);
447+
436448
chafa_free_gstring_array (output);
437449
}
438450

@@ -455,10 +467,6 @@ static int preinit(struct vo *vo)
455467
priv->config = NULL;
456468
priv->symbol_map = NULL;
457469

458-
gchar **envp = g_get_environ();
459-
priv->term_info = chafa_term_db_detect(chafa_term_db_get_default (), envp);
460-
g_strfreev (envp);
461-
462470
// Comment from Chafa repo
463471
/* Chafa may create and destroy GThreadPools multiple times while rendering
464472
* an image. This reduces thread churn and saves a decent amount of CPU. */
@@ -512,8 +520,8 @@ const struct vo_driver video_out_chafa = {
512520
.uninit = uninit,
513521
.priv_size = sizeof(struct priv),
514522
.priv_defaults = &(const struct priv) {
515-
.opts.pixel_mode = CHAFA_PIXEL_MODE_SYMBOLS,
516-
.opts.canvas_mode = CHAFA_CANVAS_MODE_TRUECOLOR,
523+
.opts.pixel_mode = CHAFA_PIXEL_MODE_MAX,
524+
.opts.canvas_mode = CHAFA_CANVAS_MODE_MAX,
517525
.opts.dither_mode = CHAFA_DITHER_MODE_NONE,
518526
.opts.work_factor = 50,
519527
.opts.pad_y = -1,
@@ -525,11 +533,13 @@ const struct vo_driver video_out_chafa = {
525533
{"width", OPT_INT(opts.width)},
526534
{"height", OPT_INT(opts.height)},
527535
{"pixel-mode", OPT_CHOICE(opts.pixel_mode,
536+
{"auto", CHAFA_PIXEL_MODE_MAX},
528537
{"symbols", CHAFA_PIXEL_MODE_SYMBOLS},
529538
{"sixels", CHAFA_PIXEL_MODE_SIXELS},
530539
{"kitty", CHAFA_PIXEL_MODE_KITTY},
531540
{"iterm2", CHAFA_PIXEL_MODE_ITERM2})},
532541
{"canvas-mode", OPT_CHOICE(opts.canvas_mode,
542+
{"auto", CHAFA_CANVAS_MODE_MAX},
533543
{"truecolor", CHAFA_CANVAS_MODE_TRUECOLOR},
534544
{"256", CHAFA_CANVAS_MODE_INDEXED_256},
535545
{"240", CHAFA_CANVAS_MODE_INDEXED_240},

0 commit comments

Comments
 (0)