Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change ui_window_style_set and change the way cursorline is drawn #1229

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

infastin
Copy link

Changes:

  • ui_window_style_set now accepts keep flag, that allows to not override non-default style values (fg and bg);
  • ui_window_style_set_pos and win_style also accept the same flag;
  • win:style and win:style_pos both accept the same keep flag, but it is optional;
  • Now when drawing cursorline the CURSOR_LINE style won't override non-default values of other styles on the line.

This was mainly done so that CURSOR_LINE wouldn't override other styles, but plugins can potentially benefit from this as well.

@rnpnr
Copy link
Collaborator

rnpnr commented Jan 20, 2025

Hi! I'm not sure I understand the point? CURSOR_LINE already doesn't override non-default styles if the user didn't request it. For example the zenburn theme only specifies the background for CURSOR_LINE so whatever foreground was set by the other rules remains unchanged when CURSOR_LINE is applied.

@infastin
Copy link
Author

infastin commented Jan 20, 2025

Hi! I'm not sure I understand the point? CURSOR_LINE already doesn't override non-default styles if the user didn't request it. For example the zenburn theme only specifies the background for CURSOR_LINE so whatever foreground was set by the other rules remains unchanged when CURSOR_LINE is applied.

If other styles on the same line have non-default background, then CURSOR_LINE will override them. That's is kind of the problem when using vis-sneak plugin, which sets CURSOR style on matches, which is then "overriden" by CURSOR_LINE when you are on the same line with the match.

Before:

image

After:

image

Copy link
Collaborator

@rnpnr rnpnr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delayed review. I'm fine with this if you address the comments I left inline.

Comment on lines -43 to 49
static bool is_default_fg(CellColor c) {
return is_default_color(c);
static bool is_default_fg(CellColor c, CellColor default_fg) {
return is_default_color(c) || cell_color_equal(c, default_fg);
}

static bool is_default_bg(CellColor c) {
return is_default_color(c);
static bool is_default_bg(CellColor c, CellColor default_bg) {
return is_default_color(c) || cell_color_equal(c, default_bg);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do the cell_color_equal() inline where its needed. The current change makes it very hard to understand what the is_default_* functions are checking.

@@ -269,25 +269,29 @@ static void ui_window_draw(Win *win) {
}
}

void ui_window_style_set(Ui *tui, int win_id, Cell *cell, enum UiStyle id) {
void ui_window_style_set(Ui *tui, int win_id, Cell *cell, enum UiStyle id, bool keep) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a more descriptive name than keep. What is being kept? It should be immediately clear.

set.bg = is_default_bg(set.bg)? cell->style.bg : set.bg;
CellStyle def = tui->styles[win_id * UI_STYLE_MAX + UI_STYLE_DEFAULT];
if (keep) {
set.fg = is_default_fg(cell->style.fg, def.fg) ? set.fg : cell->style.fg;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
set.fg = is_default_fg(cell->style.fg, def.fg) ? set.fg : cell->style.fg;
if (!is_default_fg(cell->style.fg) && !cell_color_equal(cell->style.fg, def.fg))
set.fg = cell->style.fg;

These ternary operations are too difficult to follow with the proposed changes.

@@ -2038,7 +2039,8 @@ static int window_style(lua_State *L) {
enum UiStyle style = luaL_checkunsigned(L, 2);
size_t start = checkpos(L, 3);
size_t end = checkpos(L, 4);
win_style(win, style, start, end);
bool keep = lua_isboolean(L, 5) && lua_toboolean(L, 5);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bool keep = lua_isboolean(L, 5) && lua_toboolean(L, 5);
if (!lua_isboolean(L, 5))
return luaL_argerror(L, 5, "boolean expected");

I think we should raise an error rather than assume false when the caller passes something weird.

@@ -2063,7 +2066,8 @@ static int window_style_pos(lua_State *L) {
enum UiStyle style = luaL_checkunsigned(L, 2);
size_t x = checkpos(L, 3);
size_t y = checkpos(L, 4);
bool ret = ui_window_style_set_pos(win, (int)x, (int)y, style);
bool keep = lua_isboolean(L, 5) && lua_toboolean(L, 5);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants