Skip to content

Commit

Permalink
Add Lua function to Win for directly editing cell styling by position
Browse files Browse the repository at this point in the history
  • Loading branch information
dther authored and rnpnr committed Apr 29, 2024
1 parent 8be1f68 commit 62ccfe5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ui-terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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*);
Expand Down
27 changes: 27 additions & 0 deletions vis-lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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 },
Expand Down

0 comments on commit 62ccfe5

Please sign in to comment.