Skip to content

Commit 62ccfe5

Browse files
dtherrnpnr
authored andcommitted
Add Lua function to Win for directly editing cell styling by position
1 parent 8be1f68 commit 62ccfe5

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

ui-terminal.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ struct UiTermWin {
6969
#include "ui-terminal-vt100.c"
7070
#endif
7171

72+
/* helper macro for handling UiTerm.cells */
73+
#define CELL_AT_POS(UI, X, Y) (((UI)->cells) + (X) + ((Y) * (UI)->width));
74+
7275
__attribute__((noreturn)) static void ui_die(Ui *ui, const char *msg, va_list ap) {
7376
UiTerm *tui = (UiTerm*)ui;
7477
ui_term_backend_free(tui);
@@ -305,6 +308,17 @@ static void ui_window_style_set(UiWin *w, Cell *cell, enum UiStyle id) {
305308
memcpy(&cell->style, &set, sizeof(CellStyle));
306309
}
307310

311+
static bool ui_window_style_set_pos(UiWin *w, int x, int y, enum UiStyle id) {
312+
UiTermWin *win = (UiTermWin*)w;
313+
UiTerm *tui = win->ui;
314+
if (x < 0 || y < 0 || y >= win->height || x >= win->width) {
315+
return false;
316+
}
317+
Cell *cell = CELL_AT_POS(tui, win->x + x, win->y + y)
318+
ui_window_style_set(w, cell, id);
319+
return true;
320+
}
321+
308322
static void ui_window_status(UiWin *w, const char *status) {
309323
UiTermWin *win = (UiTermWin*)w;
310324
if (!(win->options & UI_OPTION_STATUSBAR))
@@ -534,6 +548,7 @@ static UiWin *ui_window_new(Ui *ui, Win *w, enum UiOption options) {
534548

535549
win->uiwin = (UiWin) {
536550
.style_set = ui_window_style_set,
551+
.style_set_pos = ui_window_style_set_pos,
537552
.status = ui_window_status,
538553
.options_set = ui_window_options_set,
539554
.options_get = ui_window_options_get,

ui.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ struct Ui {
9797

9898
struct UiWin {
9999
void (*style_set)(UiWin*, Cell*, enum UiStyle);
100+
bool (*style_set_pos)(UiWin*, int x, int y, enum UiStyle);
100101
void (*status)(UiWin*, const char *txt);
101102
void (*options_set)(UiWin*, enum UiOption);
102103
enum UiOption (*options_get)(UiWin*);

vis-lua.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,6 +2106,32 @@ static int window_style(lua_State *L) {
21062106
return 0;
21072107
}
21082108

2109+
/***
2110+
* Style the single terminal cell at the given coordinates, relative to this window.
2111+
*
2112+
* Completely independent of the file buffer, and can be used to style UI elements,
2113+
* such as the status bar.
2114+
* The style will be cleared after every window redraw.
2115+
* @function style_pos
2116+
* @tparam int id display style registered with @{style_define}
2117+
* @tparam int x 0-based x coordinate within Win, where (0,0) is the top left corner
2118+
* @tparam int y See above
2119+
* @treturn bool false if the coordinates would be outside the window's dimensions
2120+
* @see style_define
2121+
* @usage
2122+
* win:style_pos(win.STYLE_COLOR_COLUMN, 0, win.height - 1)
2123+
* -- Styles the first character of the status bar (or the last line, if disabled)
2124+
*/
2125+
static int window_style_pos(lua_State *L) {
2126+
Win *win = obj_ref_check(L, 1, VIS_LUA_TYPE_WINDOW);
2127+
enum UiStyle style = luaL_checkunsigned(L, 2);
2128+
size_t x = checkpos(L, 3);
2129+
size_t y = checkpos(L, 4);
2130+
bool ret = win->ui->style_set_pos(win->ui, (int)x, (int)y, style);
2131+
lua_pushboolean(L, ret);
2132+
return 1;
2133+
}
2134+
21092135
/***
21102136
* Set window status line.
21112137
*
@@ -2175,6 +2201,7 @@ static const struct luaL_Reg window_funcs[] = {
21752201
{ "unmap", window_unmap },
21762202
{ "style_define", window_style_define },
21772203
{ "style", window_style },
2204+
{ "style_pos", window_style_pos },
21782205
{ "status", window_status },
21792206
{ "draw", window_draw },
21802207
{ "close", window_close },

0 commit comments

Comments
 (0)