Skip to content

Commit

Permalink
libchafa: Add sequences to reset, set and query default FG/BG colors
Browse files Browse the repository at this point in the history
  • Loading branch information
hpjansson committed Dec 7, 2022
1 parent 000fca3 commit c3f932b
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 8 deletions.
14 changes: 14 additions & 0 deletions chafa/chafa-term-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,22 @@ static const SeqStr sixel_seqs [] =
{ CHAFA_TERM_SEQ_MAX, NULL }
};

static const SeqStr default_color_seqs [] =
{
{ CHAFA_TERM_SEQ_RESET_DEFAULT_FG, "\033]110\033\\" },
{ CHAFA_TERM_SEQ_SET_DEFAULT_FG, "\033]10;rgb:%1/%2/%3\e\\" },
{ CHAFA_TERM_SEQ_QUERY_DEFAULT_FG, "\033]10;?\033\\" },

{ CHAFA_TERM_SEQ_RESET_DEFAULT_BG, "\033]111\033\\" },
{ CHAFA_TERM_SEQ_SET_DEFAULT_BG, "\033]11;rgb:%1/%2/%3\e\\" },
{ CHAFA_TERM_SEQ_QUERY_DEFAULT_BG, "\033]11;?\033\\" },

{ CHAFA_TERM_SEQ_MAX, NULL }
};

static const SeqStr color_direct_seqs [] =
{
/* ISO 8613-6 */
{ CHAFA_TERM_SEQ_SET_COLOR_FG_DIRECT, "\033[38;2;%1;%2;%3m" },
{ CHAFA_TERM_SEQ_SET_COLOR_BG_DIRECT, "\033[48;2;%1;%2;%3m" },
{ CHAFA_TERM_SEQ_SET_COLOR_FGBG_DIRECT, "\033[38;2;%1;%2;%3;48;2;%4;%5;%6m" },
Expand Down
28 changes: 22 additions & 6 deletions chafa/chafa-term-info.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,10 @@ parse_seq_args (gchar *out, SeqArgInfo *arg_info, const gchar *in,
return result;
}

#define EMIT_SEQ_DEF(inttype, intformatter) \
#define EMIT_SEQ_DEF(name, inttype, intformatter) \
static gchar * \
emit_seq_##inttype (const ChafaTermInfo *term_info, gchar *out, ChafaTermSeq seq, \
inttype *args, gint n_args) \
emit_seq_##name (const ChafaTermInfo *term_info, gchar *out, ChafaTermSeq seq, \
inttype *args, gint n_args) \
{ \
const gchar *seq_str; \
const SeqArgInfo *seq_args; \
Expand All @@ -262,7 +262,7 @@ parse_seq_args (gchar *out, SeqArgInfo *arg_info, const gchar *in,
copy_bytes (out, &seq_str [ofs], seq_args [i].pre_len); \
out += seq_args [i].pre_len; \
ofs += seq_args [i].pre_len; \
out = intformatter (out, args [seq_args [i].arg_index]); \
out = intformatter (out, args [seq_args [i].arg_index]); \
} \
\
copy_bytes (out, &seq_str [ofs], seq_args [i].pre_len); \
Expand All @@ -271,8 +271,9 @@ parse_seq_args (gchar *out, SeqArgInfo *arg_info, const gchar *in,
return out; \
}

EMIT_SEQ_DEF(guint, chafa_format_dec_uint_0_to_9999)
EMIT_SEQ_DEF(guint8, chafa_format_dec_u8)
EMIT_SEQ_DEF(guint, guint, chafa_format_dec_uint_0_to_9999)
EMIT_SEQ_DEF(guint8, guint8, chafa_format_dec_u8)
EMIT_SEQ_DEF(guint16_hex, guint16, chafa_format_dec_u16_hex)

static gchar *
emit_seq_0_args_uint (const ChafaTermInfo *term_info, gchar *out, ChafaTermSeq seq)
Expand Down Expand Up @@ -362,6 +363,17 @@ emit_seq_6_args_uint8 (const ChafaTermInfo *term_info, gchar *out, ChafaTermSeq
return emit_seq_guint8 (term_info, out, seq, args, 6);
}

static gchar *
emit_seq_3_args_uint16_hex (const ChafaTermInfo *term_info, gchar *out, ChafaTermSeq seq, guint16 arg0, guint16 arg1, guint16 arg2)
{
guint16 args [3];

args [0] = arg0;
args [1] = arg1;
args [2] = arg2;
return emit_seq_guint16_hex (term_info, out, seq, args, 3);
}

/* Public */

G_DEFINE_QUARK (chafa-term-info-error-quark, chafa_term_info_error)
Expand Down Expand Up @@ -676,6 +688,10 @@ gchar *chafa_term_info_emit_##func_name(const ChafaTermInfo *term_info, gchar *d
gchar *chafa_term_info_emit_##func_name(const ChafaTermInfo *term_info, gchar *dest, guint8 arg0, guint8 arg1, guint8 arg2, guint8 arg3, guint8 arg4, guint8 arg5) \
{ return emit_seq_6_args_uint8 (term_info, dest, CHAFA_TERM_SEQ_##seq_name, arg0, arg1, arg2, arg3, arg4, arg5); }

#define DEFINE_EMIT_SEQ_3_u16hex_guint16(func_name, seq_name) \
gchar *chafa_term_info_emit_##func_name(const ChafaTermInfo *term_info, gchar *dest, guint16 arg0, guint16 arg1, guint16 arg2) \
{ return emit_seq_3_args_uint16_hex (term_info, dest, CHAFA_TERM_SEQ_##seq_name, arg0, arg1, arg2); }

#define CHAFA_TERM_SEQ_DEF(name, NAME, n_args, arg_proc, arg_type, ...) \
DEFINE_EMIT_SEQ_##n_args##_##arg_proc##_##arg_type(name, NAME)
#include "chafa-term-seq-def.h"
Expand Down
113 changes: 113 additions & 0 deletions chafa/chafa-term-seq-def.h
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,119 @@ CHAFA_TERM_SEQ_DEF(set_color_bg_8, SET_COLOR_BG_8, 1, 8bg, guint8, CHAFA_TERM_SE
**/
CHAFA_TERM_SEQ_DEF(set_color_fgbg_8, SET_COLOR_FGBG_8, 2, 8fgbg, guint8, CHAFA_TERM_SEQ_ARGS guint8 fg_pen, guint8 bg_pen)

/* --- Available in 1.14+ --- */

#undef CHAFA_TERM_SEQ_AVAILABILITY
#define CHAFA_TERM_SEQ_AVAILABILITY CHAFA_AVAILABLE_IN_1_14

/**
* chafa_term_info_emit_reset_default_fg:
* @term_info: A #ChafaTermInfo
* @dest: String destination
*
* Prints the control sequence for #CHAFA_TERM_SEQ_RESET_DEFAULT_FG.
*
* @dest must have enough space to hold
* #CHAFA_TERM_SEQ_LENGTH_MAX bytes, even if the emitted sequence is
* shorter. The output will not be zero-terminated.
*
* Returns: Pointer to first byte after emitted string
*
* Since: 1.14
**/
CHAFA_TERM_SEQ_DEF(reset_default_fg, RESET_DEFAULT_FG, 0, none, char)

/**
* chafa_term_info_emit_set_default_fg:
* @term_info: A #ChafaTermInfo
* @dest: String destination
* @r: Red component (0-65535)
* @g: Green component (0-65535)
* @b: Blue component (0-65535)
*
* Prints the control sequence for #CHAFA_TERM_SEQ_SET_DEFAULT_FG.
*
* @dest must have enough space to hold
* #CHAFA_TERM_SEQ_LENGTH_MAX bytes, even if the emitted sequence is
* shorter. The output will not be zero-terminated.
*
* Returns: Pointer to first byte after emitted string
*
* Since: 1.14
**/
CHAFA_TERM_SEQ_DEF(set_default_fg, SET_DEFAULT_FG, 3, u16hex, guint16, CHAFA_TERM_SEQ_ARGS guint16 r, guint16 g, guint16 b)

/**
* chafa_term_info_emit_query_default_fg:
* @term_info: A #ChafaTermInfo
* @dest: String destination
*
* Prints the control sequence for #CHAFA_TERM_SEQ_QUERY_DEFAULT_FG.
*
* @dest must have enough space to hold
* #CHAFA_TERM_SEQ_LENGTH_MAX bytes, even if the emitted sequence is
* shorter. The output will not be zero-terminated.
*
* Returns: Pointer to first byte after emitted string
*
* Since: 1.14
**/
CHAFA_TERM_SEQ_DEF(query_default_fg, QUERY_DEFAULT_FG, 0, none, char)

/**
* chafa_term_info_emit_reset_default_bg:
* @term_info: A #ChafaTermInfo
* @dest: String destination
*
* Prints the control sequence for #CHAFA_TERM_SEQ_RESET_DEFAULT_BG.
*
* @dest must have enough space to hold
* #CHAFA_TERM_SEQ_LENGTH_MAX bytes, even if the emitted sequence is
* shorter. The output will not be zero-terminated.
*
* Returns: Pointer to first byte after emitted string
*
* Since: 1.14
**/
CHAFA_TERM_SEQ_DEF(reset_default_bg, RESET_DEFAULT_BG, 0, none, char)

/**
* chafa_term_info_emit_set_default_bg:
* @term_info: A #ChafaTermInfo
* @dest: String destination
* @r: Red component (0-65535)
* @g: Green component (0-65535)
* @b: Blue component (0-65535)
*
* Prints the control sequence for #CHAFA_TERM_SEQ_SET_DEFAULT_BG.
*
* @dest must have enough space to hold
* #CHAFA_TERM_SEQ_LENGTH_MAX bytes, even if the emitted sequence is
* shorter. The output will not be zero-terminated.
*
* Returns: Pointer to first byte after emitted string
*
* Since: 1.14
**/
CHAFA_TERM_SEQ_DEF(set_default_bg, SET_DEFAULT_BG, 3, u16hex, guint16, CHAFA_TERM_SEQ_ARGS guint16 r, guint16 g, guint16 b)

/**
* chafa_term_info_emit_query_default_bg:
* @term_info: A #ChafaTermInfo
* @dest: String destination
*
* Prints the control sequence for #CHAFA_TERM_SEQ_QUERY_DEFAULT_BG.
*
* @dest must have enough space to hold
* #CHAFA_TERM_SEQ_LENGTH_MAX bytes, even if the emitted sequence is
* shorter. The output will not be zero-terminated.
*
* Returns: Pointer to first byte after emitted string
*
* Since: 1.14
**/
CHAFA_TERM_SEQ_DEF(query_default_bg, QUERY_DEFAULT_BG, 0, none, char)

#undef CHAFA_TERM_SEQ_AVAILABILITY

#undef CHAFA_TERM_SEQ_ARGS
10 changes: 8 additions & 2 deletions tests/term-info-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ static void
formatting_test (void)
{
ChafaTermInfo *ti;
gchar buf [CHAFA_TERM_SEQ_LENGTH_MAX * 12 + 1];
gchar buf [CHAFA_TERM_SEQ_LENGTH_MAX * 14 + 1];
gchar *out = buf;

ti = chafa_term_info_new ();
Expand All @@ -21,6 +21,8 @@ formatting_test (void)
chafa_term_info_set_seq (ti, CHAFA_TERM_SEQ_SET_COLOR_FG_16, "aix%1,", NULL);
chafa_term_info_set_seq (ti, CHAFA_TERM_SEQ_SET_COLOR_BG_16, "aix%1,", NULL);
chafa_term_info_set_seq (ti, CHAFA_TERM_SEQ_SET_COLOR_FGBG_16, "aix-%1-%2,", NULL);
chafa_term_info_set_seq (ti, CHAFA_TERM_SEQ_SET_DEFAULT_FG, "def-fg-%1-%2-%3,", NULL);
chafa_term_info_set_seq (ti, CHAFA_TERM_SEQ_SET_DEFAULT_BG, "def-bg-%1-%2-%3,", NULL);

out = chafa_term_info_emit_reset_terminal_soft (ti, out);
out = chafa_term_info_emit_cursor_up (ti, out, 9876);
Expand All @@ -34,6 +36,8 @@ formatting_test (void)
out = chafa_term_info_emit_set_color_bg_16 (ti, out, 8);
out = chafa_term_info_emit_set_color_fgbg_16 (ti, out, 0, 0);
out = chafa_term_info_emit_set_color_fgbg_16 (ti, out, 8, 8);
out = chafa_term_info_emit_set_default_fg (ti, out, 0xffff, 0x0000, 0x1234);
out = chafa_term_info_emit_set_default_bg (ti, out, 0x1234, 0xffff, 0x0000);

*out = '\0';

Expand All @@ -51,7 +55,9 @@ formatting_test (void)
"aix40,"
"aix100,"
"aix-30-40,"
"aix-90-100,");
"aix-90-100,"
"def-fg-ffff-0000-1234,"
"def-bg-1234-ffff-0000,");
}

int
Expand Down

0 comments on commit c3f932b

Please sign in to comment.