Apply better colours to various builtin bits in modes.

This commit is contained in:
nicm
2026-06-26 08:10:48 +00:00
parent 569e9d932f
commit 3631f81236
9 changed files with 364 additions and 198 deletions

View File

@@ -36,6 +36,24 @@ enum mode_tree_preview {
MODE_TREE_PREVIEW_BIG
};
#define MODE_TREE_PREFIX_STYLE \
"#{?mode_tree_selected,#[default]#[noacs]," \
"#[fg=themelightgrey]#[bg=default]#[noacs]}"
#define MODE_TREE_PREFIX_FORMAT \
MODE_TREE_PREFIX_STYLE \
"#{p/#{mode_tree_key_width}:" \
"#{?#{!=:#{mode_tree_key},},(#{mode_tree_key}),}}" \
"#{R:#{?mode_tree_parent_last, ," \
"#[acs]x" MODE_TREE_PREFIX_STYLE " }," \
"#{mode_tree_repeat}}" \
"#{?mode_tree_branch," \
"#[acs]#{?mode_tree_last,mq,tq}" MODE_TREE_PREFIX_STYLE "> ,}" \
"#{?mode_tree_has_children," \
"#{?mode_tree_expanded,#[fg=themered]-" MODE_TREE_PREFIX_STYLE " ," \
"#[fg=themegreen]+" MODE_TREE_PREFIX_STYLE " }," \
"#{?mode_tree_flat,, }}"
struct mode_tree_item;
struct mode_tree_prompt;
TAILQ_HEAD(mode_tree_list, mode_tree_item);
@@ -159,31 +177,53 @@ static const struct menu_item mode_tree_menu_items[] = {
};
static const char* mode_tree_help_start[] = {
"#[bold] Up, k #[default]#[acs]x#[default] Move cursor up",
"#[bold] Down, j #[default]#[acs]x#[default] Move cursor down",
"#[bold] g #[default]#[acs]x#[default] Go to top",
"#[bold] G #[default]#[acs]x#[default] Go to bottom",
"#[bold] PPage, C-b #[default]#[acs]x#[default] Page up",
"#[bold] NPage, C-f #[default]#[acs]x#[default] Page down",
"#[bold] Left, h #[default]#[acs]x#[default] Collapse %1",
"#[bold] Right, l #[default]#[acs]x#[default] Expand %1",
"#[bold] M-- #[default]#[acs]x#[default] Collapse all %1s",
"#[bold] M-+ #[default]#[acs]x#[default] Expand all %1s",
"#[bold] t #[default]#[acs]x#[default] Toggle %1 tag",
"#[bold] T #[default]#[acs]x#[default] Untag all %1s",
"#[bold] C-t #[default]#[acs]x#[default] Tag all %1s",
"#[bold] C-s #[default]#[acs]x#[default] Search forward",
"#[bold] C-r #[default]#[acs]x#[default] Search backward",
"#[bold] n #[default]#[acs]x#[default] Repeat search forward",
"#[bold] N #[default]#[acs]x#[default] Repeat search backward",
"#[bold] f #[default]#[acs]x#[default] Filter %1s",
"#[bold] O #[default]#[acs]x#[default] Change sort order",
"#[bold] r #[default]#[acs]x#[default] Reverse sort order",
"#[bold] v #[default]#[acs]x#[default] Toggle preview",
"#[fg=themelightgrey]"
" Up, k #[#{E:tree-mode-border-style},acs]x#[default] Move cursor up",
"#[fg=themelightgrey]"
" Down, j #[#{E:tree-mode-border-style},acs]x#[default] Move cursor down",
"#[fg=themelightgrey]"
" g #[#{E:tree-mode-border-style},acs]x#[default] Go to top",
"#[fg=themelightgrey]"
" G #[#{E:tree-mode-border-style},acs]x#[default] Go to bottom",
"#[fg=themelightgrey]"
" PPage, C-b #[#{E:tree-mode-border-style},acs]x#[default] Page up",
"#[fg=themelightgrey]"
" NPage, C-f #[#{E:tree-mode-border-style},acs]x#[default] Page down",
"#[fg=themelightgrey]"
" Left, h #[#{E:tree-mode-border-style},acs]x#[default] Collapse %1",
"#[fg=themelightgrey]"
" Right, l #[#{E:tree-mode-border-style},acs]x#[default] Expand %1",
"#[fg=themelightgrey]"
" M-- #[#{E:tree-mode-border-style},acs]x#[default] Collapse all %1s",
"#[fg=themelightgrey]"
" M-+ #[#{E:tree-mode-border-style},acs]x#[default] Expand all %1s",
"#[fg=themelightgrey]"
" t #[#{E:tree-mode-border-style},acs]x#[default] Toggle %1 tag",
"#[fg=themelightgrey]"
" T #[#{E:tree-mode-border-style},acs]x#[default] Untag all %1s",
"#[fg=themelightgrey]"
" C-t #[#{E:tree-mode-border-style},acs]x#[default] Tag all %1s",
"#[fg=themelightgrey]"
" C-s #[#{E:tree-mode-border-style},acs]x#[default] Search forward",
"#[fg=themelightgrey]"
" C-r #[#{E:tree-mode-border-style},acs]x#[default] Search backward",
"#[fg=themelightgrey]"
" n #[#{E:tree-mode-border-style},acs]x#[default] Repeat search forward",
"#[fg=themelightgrey]"
" N #[#{E:tree-mode-border-style},acs]x#[default] Repeat search backward",
"#[fg=themelightgrey]"
" f #[#{E:tree-mode-border-style},acs]x#[default] Filter %1s",
"#[fg=themelightgrey]"
" O #[#{E:tree-mode-border-style},acs]x#[default] Change sort order",
"#[fg=themelightgrey]"
" r #[#{E:tree-mode-border-style},acs]x#[default] Reverse sort order",
"#[fg=themelightgrey]"
" v #[#{E:tree-mode-border-style},acs]x#[default] Toggle preview",
NULL
};
static const char* mode_tree_help_end[] = {
"#[bold] q, Escape #[default]#[acs]x#[default] Exit mode",
"#[fg=themelightgrey]"
" q, Escape #[#{E:tree-mode-border-style},acs]x#[default] Exit mode",
NULL
};
#define MODE_TREE_HELP_DEFAULT_WIDTH 39
@@ -784,12 +824,14 @@ mode_tree_draw(struct mode_tree_data *mtd)
struct mode_tree_item *mti;
struct options *oo = wp->window->options;
struct screen_write_ctx ctx;
struct grid_cell gc0, gc;
u_int w, h, i, j, sy, box_x, box_y, width;
char *text, *start, *key;
const char *tag, *symbol;
size_t size, n;
int keylen, pad, alignlen[mtd->maxdepth + 1];
struct format_tree *ft;
struct grid_cell gc0, gc, box_gc;
u_int w, h, i, sy, box_x, box_y;
u_int width, text_width, prefix_width, left;
char *text, *prefix;
const char *tag, *separator;
size_t n;
int keylen, alignlen[mtd->maxdepth + 1];
if (mtd->line_size == 0)
return;
@@ -797,12 +839,15 @@ mode_tree_draw(struct mode_tree_data *mtd)
memcpy(&gc0, &grid_default_cell, sizeof gc0);
memcpy(&gc, &grid_default_cell, sizeof gc);
style_apply(&gc, oo, "mode-style", NULL);
memcpy(&box_gc, &grid_default_cell, sizeof box_gc);
style_apply(&box_gc, oo, "tree-mode-border-style", NULL);
w = mtd->width;
h = mtd->height;
screen_write_start(&ctx, s);
screen_write_clearscreen(&ctx, 8);
ft = format_create_defaults(NULL, NULL, NULL, NULL, wp);
keylen = 0;
for (i = 0; i < mtd->line_size; i++) {
@@ -833,52 +878,53 @@ mode_tree_draw(struct mode_tree_data *mtd)
screen_write_cursormove(&ctx, 0, i - mtd->offset, 0);
pad = keylen - 2 - mti->keylen;
if (mti->key != KEYC_NONE)
xasprintf(&key, "(%s)%*s", mti->keystr, pad, "");
format_add(ft, "mode_tree_key", "%s", mti->keystr);
else
key = xstrdup("");
if (line->flat)
symbol = "";
else if (TAILQ_EMPTY(&mti->children))
symbol = " ";
else if (mti->expanded)
symbol = "- ";
else
symbol = "+ ";
if (line->depth == 0)
start = xstrdup(symbol);
else {
size = (4 * line->depth) + 32;
start = xcalloc(1, size);
for (j = 1; j < line->depth; j++) {
if (mti->parent != NULL &&
mtd->line_list[mti->parent->line].last)
strlcat(start, " ", size);
else
strlcat(start, "\001x\001 ", size);
}
if (line->last)
strlcat(start, "\001mq\001> ", size);
format_add(ft, "mode_tree_key", "%s", "");
format_add(ft, "mode_tree_key_width", "%d", keylen);
format_add(ft, "mode_tree_selected", "%d", i == mtd->current);
if (line->depth == 0) {
format_add(ft, "mode_tree_repeat", "%u", 0);
format_add(ft, "mode_tree_branch", "0");
format_add(ft, "mode_tree_parent_last", "0");
} else {
format_add(ft, "mode_tree_repeat", "%u",
line->depth - 1);
format_add(ft, "mode_tree_branch", "1");
if (mti->parent != NULL &&
mtd->line_list[mti->parent->line].last)
format_add(ft, "mode_tree_parent_last", "1");
else
strlcat(start, "\001tq\001> ", size);
strlcat(start, symbol, size);
format_add(ft, "mode_tree_parent_last", "0");
}
if (TAILQ_EMPTY(&mti->children))
format_add(ft, "mode_tree_has_children", "0");
else
format_add(ft, "mode_tree_has_children", "1");
format_add(ft, "mode_tree_last", "%d", line->last);
format_add(ft, "mode_tree_expanded", "%d", mti->expanded);
format_add(ft, "mode_tree_flat", "%d", line->flat);
prefix = format_expand(ft, MODE_TREE_PREFIX_FORMAT);
prefix_width = format_width(prefix);
if (prefix_width > w)
prefix_width = w;
if (mti->tagged)
tag = "*";
else
tag = "";
xasprintf(&text, "%-*s%s%*s%s%s", keylen, key, start,
mti->align * alignlen[line->depth], mti->name, tag,
(mti->text != NULL) ? ": " : "" );
width = utf8_cstrwidth(text);
if (width > w)
width = w;
free(start);
if (mti->text != NULL)
separator = "#[fg=themelightgrey]: #[default]";
else
separator = "";
xasprintf(&text, "%*s%s%s",
mti->align * alignlen[line->depth], mti->name, tag, separator);
text_width = format_width(text);
left = (prefix_width < w) ? (w - prefix_width) : 0;
if (text_width > left)
text_width = left;
width = prefix_width + text_width;
if (mti->tagged) {
gc.attr ^= GRID_ATTR_BRIGHT;
@@ -887,27 +933,43 @@ mode_tree_draw(struct mode_tree_data *mtd)
if (i != mtd->current) {
screen_write_clearendofline(&ctx, 8);
screen_write_nputs(&ctx, w, &gc0, "%s", text);
if (mti->text != NULL) {
format_draw(&ctx, &gc0, w - width, mti->text,
NULL, 0);
format_draw(&ctx, &grid_default_cell, prefix_width,
prefix, NULL, 0);
if (left != 0) {
screen_write_cursormove(&ctx, prefix_width,
i - mtd->offset, 0);
format_draw(&ctx, &gc0, left, text, NULL, 0);
if (mti->text != NULL && width < w) {
screen_write_cursormove(&ctx, width,
i - mtd->offset, 0);
format_draw(&ctx, &gc0, w - width,
mti->text, NULL, 0);
}
}
} else {
screen_write_clearendofline(&ctx, gc.bg);
screen_write_nputs(&ctx, w, &gc, "%s", text);
if (mti->text != NULL) {
format_draw(&ctx, &gc, w - width, mti->text,
NULL, 0);
format_draw(&ctx, &gc, prefix_width, prefix, NULL, 0);
if (left != 0) {
screen_write_cursormove(&ctx, prefix_width,
i - mtd->offset, 0);
format_draw(&ctx, &gc, left, text, NULL, 0);
if (mti->text != NULL && width < w) {
screen_write_cursormove(&ctx, width,
i - mtd->offset, 0);
format_draw(&ctx, &gc, w - width,
mti->text, NULL, 0);
}
}
}
free(text);
free(key);
free(prefix);
if (mti->tagged) {
gc.attr ^= GRID_ATTR_BRIGHT;
gc0.attr ^= GRID_ATTR_BRIGHT;
}
}
format_free(ft);
if (mtd->preview == MODE_TREE_PREVIEW_OFF)
goto done;
@@ -922,7 +984,7 @@ mode_tree_draw(struct mode_tree_data *mtd)
mti = mti->parent;
screen_write_cursormove(&ctx, 0, h, 0);
screen_write_box(&ctx, w, sy - h, BOX_LINES_DEFAULT, NULL, NULL);
screen_write_box(&ctx, w, sy - h, BOX_LINES_DEFAULT, &box_gc, NULL);
if (mtd->sort_crit.order_seq != NULL) {
xasprintf(&text, " %s (sort: %s%s)%s%s%s", mti->name,
@@ -935,21 +997,21 @@ mode_tree_draw(struct mode_tree_data *mtd)
xasprintf(&text, " %s", mti->name);
if (w - 2 >= strlen(text)) {
screen_write_cursormove(&ctx, 1, h, 0);
screen_write_puts(&ctx, &gc0, "%s", text);
screen_write_puts(&ctx, &box_gc, "%s", text);
if (mtd->no_matches)
n = (sizeof "no matches") - 1;
else
n = (sizeof "active") - 1;
if (mtd->filter != NULL && w - 2 >= strlen(text) + 10 + n + 2) {
screen_write_puts(&ctx, &gc0, " (filter: ");
screen_write_puts(&ctx, &box_gc, " (filter: ");
if (mtd->no_matches)
screen_write_puts(&ctx, &gc, "no matches");
screen_write_puts(&ctx, &box_gc, "no matches");
else
screen_write_puts(&ctx, &gc0, "active");
screen_write_puts(&ctx, &gc0, ") ");
screen_write_puts(&ctx, &box_gc, "active");
screen_write_puts(&ctx, &box_gc, ") ");
} else
screen_write_puts(&ctx, &gc0, " ");
screen_write_puts(&ctx, &box_gc, " ");
}
free(text);
@@ -1353,12 +1415,14 @@ mode_tree_display_menu(struct mode_tree_data *mtd, struct client *c, u_int x,
static void
mode_tree_draw_help_line(struct screen_write_ctx *ctx,
const struct grid_cell *gc, const char *line, const char *item, u_int x,
u_int y, u_int w)
const struct grid_cell *gc, struct format_tree *ft, const char *line,
const char *item, u_int x, u_int y, u_int w)
{
char *expanded;
char *expanded, *replaced;
expanded = cmd_template_replace(line, item, 1);
replaced = cmd_template_replace(line, item, 1);
expanded = format_expand(ft, replaced);
free(replaced);
screen_write_cursormove(ctx, x, y, 0);
screen_write_clearcharacter(ctx, w, gc->bg);
screen_write_cursormove(ctx, x, y, 0);
@@ -1370,7 +1434,9 @@ static void
mode_tree_draw_help(struct mode_tree_data *mtd, struct screen_write_ctx *ctx)
{
struct screen *s = &mtd->screen;
struct grid_cell gc;
struct options *oo = mtd->wp->window->options;
struct grid_cell box_gc, gc;
struct format_tree *ft;
const char **line, **lines = NULL, *item = "item";
u_int sx = screen_size_x(s), sy = screen_size_y(s);
u_int x, y, w, h = 0, box_w, box_h;
@@ -1396,18 +1462,22 @@ mode_tree_draw_help(struct mode_tree_data *mtd, struct screen_write_ctx *ctx)
x = (sx - box_w) / 2;
y = (sy - box_h) / 2;
memcpy(&box_gc, &grid_default_cell, sizeof box_gc);
style_apply(&box_gc, oo, "tree-mode-border-style", NULL);
memcpy(&gc, &grid_default_cell, sizeof gc);
ft = format_create_defaults(NULL, NULL, NULL, NULL, mtd->wp);
screen_write_cursormove(ctx, x, y, 0);
screen_write_box(ctx, box_w, box_h, BOX_LINES_DEFAULT, &gc, NULL);
screen_write_box(ctx, box_w, box_h, BOX_LINES_DEFAULT, &box_gc, NULL);
y++;
x++;
for (line = mode_tree_help_start; *line != NULL; line++, y++)
mode_tree_draw_help_line(ctx, &gc, *line, item, x, y, w);
mode_tree_draw_help_line(ctx, &gc, ft, *line, item, x, y, w);
for (line = lines; line != NULL && *line != NULL; line++, y++)
mode_tree_draw_help_line(ctx, &gc, *line, item, x, y, w);
mode_tree_draw_help_line(ctx, &gc, ft, *line, item, x, y, w);
for (line = mode_tree_help_end; *line != NULL; line++, y++)
mode_tree_draw_help_line(ctx, &gc, *line, item, x, y, w);
mode_tree_draw_help_line(ctx, &gc, ft, *line, item, x, y, w);
format_free(ft);
}
static void
@@ -1488,6 +1558,10 @@ mode_tree_key(struct mode_tree_data *mtd, struct client *c, key_code *key,
*key = KEYC_NONE;
return (0);
}
if (*key == KEYC_FOCUS_IN || *key == KEYC_FOCUS_OUT) {
*key = KEYC_NONE;
return (0);
}
mtd->help = 0;
mode_tree_draw(mtd);
*key = KEYC_NONE;