diff options
| author | Rudy Dellomas III <dther@dther.xyz> | 2024-04-20 03:17:14 +1000 |
|---|---|---|
| committer | Randy Palamar <randy@rnpnr.xyz> | 2024-04-29 08:36:44 -0600 |
| commit | 62ccfe59ae63858dc5c21039e907a9277134d171 (patch) | |
| tree | dd068f380b40f85174f1d2c71441a4070caf153c | |
| parent | 8be1f68ec8a8310109a9204ec9a8599614d8d623 (diff) | |
| download | vis-62ccfe59ae63858dc5c21039e907a9277134d171.tar.gz vis-62ccfe59ae63858dc5c21039e907a9277134d171.tar.xz | |
Add Lua function to Win for directly editing cell styling by position
| -rw-r--r-- | ui-terminal.c | 15 | ||||
| -rw-r--r-- | ui.h | 1 | ||||
| -rw-r--r-- | vis-lua.c | 27 |
3 files changed, 43 insertions, 0 deletions
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, @@ -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*); @@ -2107,6 +2107,32 @@ static int window_style(lua_State *L) { } /*** + * 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. * * @function status @@ -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 }, |
