From 62ccfe59ae63858dc5c21039e907a9277134d171 Mon Sep 17 00:00:00 2001 From: Rudy Dellomas III Date: Sat, 20 Apr 2024 03:17:14 +1000 Subject: Add Lua function to Win for directly editing cell styling by position --- ui-terminal.c | 15 +++++++++++++++ ui.h | 1 + vis-lua.c | 27 +++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/ui-terminal.c b/ui-terminal.c index dd04f76..6ec1e41 100644 --- a/ui-terminal.c +++ b/ui-terminal.c @@ -69,6 +69,9 @@ struct UiTermWin { #include "ui-terminal-vt100.c" #endif +/* helper macro for handling UiTerm.cells */ +#define CELL_AT_POS(UI, X, Y) (((UI)->cells) + (X) + ((Y) * (UI)->width)); + __attribute__((noreturn)) static void ui_die(Ui *ui, const char *msg, va_list ap) { UiTerm *tui = (UiTerm*)ui; ui_term_backend_free(tui); @@ -305,6 +308,17 @@ static void ui_window_style_set(UiWin *w, Cell *cell, enum UiStyle id) { memcpy(&cell->style, &set, sizeof(CellStyle)); } +static bool ui_window_style_set_pos(UiWin *w, int x, int y, enum UiStyle id) { + UiTermWin *win = (UiTermWin*)w; + UiTerm *tui = win->ui; + if (x < 0 || y < 0 || y >= win->height || x >= win->width) { + return false; + } + Cell *cell = CELL_AT_POS(tui, win->x + x, win->y + y) + ui_window_style_set(w, cell, id); + return true; +} + static void ui_window_status(UiWin *w, const char *status) { UiTermWin *win = (UiTermWin*)w; if (!(win->options & UI_OPTION_STATUSBAR)) @@ -534,6 +548,7 @@ static UiWin *ui_window_new(Ui *ui, Win *w, enum UiOption options) { win->uiwin = (UiWin) { .style_set = ui_window_style_set, + .style_set_pos = ui_window_style_set_pos, .status = ui_window_status, .options_set = ui_window_options_set, .options_get = ui_window_options_get, diff --git a/ui.h b/ui.h index 76bdcce..45b6065 100644 --- a/ui.h +++ b/ui.h @@ -97,6 +97,7 @@ struct Ui { struct UiWin { void (*style_set)(UiWin*, Cell*, enum UiStyle); + bool (*style_set_pos)(UiWin*, int x, int y, enum UiStyle); void (*status)(UiWin*, const char *txt); void (*options_set)(UiWin*, enum UiOption); enum UiOption (*options_get)(UiWin*); diff --git a/vis-lua.c b/vis-lua.c index a3029fa..3d80293 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -2106,6 +2106,32 @@ static int window_style(lua_State *L) { return 0; } +/*** + * Style the single terminal cell at the given coordinates, relative to this window. + * + * Completely independent of the file buffer, and can be used to style UI elements, + * such as the status bar. + * The style will be cleared after every window redraw. + * @function style_pos + * @tparam int id display style registered with @{style_define} + * @tparam int x 0-based x coordinate within Win, where (0,0) is the top left corner + * @tparam int y See above + * @treturn bool false if the coordinates would be outside the window's dimensions + * @see style_define + * @usage + * win:style_pos(win.STYLE_COLOR_COLUMN, 0, win.height - 1) + * -- Styles the first character of the status bar (or the last line, if disabled) + */ +static int window_style_pos(lua_State *L) { + Win *win = obj_ref_check(L, 1, VIS_LUA_TYPE_WINDOW); + enum UiStyle style = luaL_checkunsigned(L, 2); + size_t x = checkpos(L, 3); + size_t y = checkpos(L, 4); + bool ret = win->ui->style_set_pos(win->ui, (int)x, (int)y, style); + lua_pushboolean(L, ret); + return 1; +} + /*** * Set window status line. * @@ -2175,6 +2201,7 @@ static const struct luaL_Reg window_funcs[] = { { "unmap", window_unmap }, { "style_define", window_style_define }, { "style", window_style }, + { "style_pos", window_style_pos }, { "status", window_status }, { "draw", window_draw }, { "close", window_close }, -- cgit v1.2.3