Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ enum {
OPTION_STATUSBAR,
OPTION_NUMBER,
OPTION_NUMBER_RELATIVE,
OPTION_NUMBER_WIDTH,
OPTION_CURSOR_LINE,
OPTION_COLOR_COLUMN,
OPTION_SAVE_METHOD,
Expand Down Expand Up @@ -352,6 +353,11 @@ static const OptionDef options[] = {
VIS_OPTION_TYPE_BOOL|VIS_OPTION_NEED_WINDOW,
VIS_HELP("Display relative line numbers")
},
[OPTION_NUMBER_WIDTH] = {
{ "numberwidth", "nuw" },
VIS_OPTION_TYPE_NUMBER|VIS_OPTION_NEED_WINDOW,
VIS_HELP("minimum sidebar width")
},
[OPTION_CURSOR_LINE] = {
{ "cursorline", "cul" },
VIS_OPTION_TYPE_BOOL|VIS_OPTION_NEED_WINDOW,
Expand Down
24 changes: 23 additions & 1 deletion ui-terminal.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,23 @@ static void ui_draw_string(Ui *tui, int x, int y, const char *str, int win_id, e
}
}

static uint8_t
u32_count_digits(uint32_t a)
{
// NOTE(rnp): guaranteed branchless, fixed cost regardless of input
uint8_t result = (a >= 1000000000) +
(a >= 100000000) +
(a >= 10000000) +
(a >= 1000000) +
(a >= 100000) +
(a >= 10000) +
(a >= 1000) +
(a >= 100) +
(a >= 10) +
1;
return result;
}

static void ui_window_draw(Win *win) {
Ui *ui = &win->vis->ui;
View *view = &win->view;
Expand All @@ -210,7 +227,12 @@ static void ui_window_draw(Win *win) {
bool sidebar = nu || rnu;

int width = win->width, height = win->height;
int sidebar_width = sidebar ? snprintf(NULL, 0, "%zd ", line->lineno + height - 2) : 0;

int sidebar_width = 0;
if (sidebar) {
sidebar_width = u32_count_digits(line->lineno + height - 2) + 1;
sidebar_width = MAX(sidebar_width, win->min_sidebar_width);
}
if (sidebar_width != win->sidebar_width) {
view_resize(view, width - sidebar_width, status ? height - 1 : height);
win->sidebar_width = sidebar_width;
Expand Down
4 changes: 4 additions & 0 deletions vis-cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@ static bool cmd_set(Vis *vis, Win *win, Command *cmd, const char *argv[], Select
win_options_set(win, opt);
break;
}
case OPTION_NUMBER_WIDTH: {
win->min_sidebar_width = MAX(0, arg.i);
break;
}
case OPTION_CURSOR_LINE: {
enum UiOption opt = win->options;
if (arg.b || (toggle && !(opt & UI_OPTION_CURSOR_LINE)))
Expand Down
1 change: 1 addition & 0 deletions vis-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ struct Win {
int width, height; /* window dimension including status bar */
int x, y; /* window position */
int sidebar_width; /* width of the sidebar showing line numbers etc. */
int min_sidebar_width; /* user specified minimum sidebar width */
enum UiOption options; /* display settings for this window */
View view; /* currently displayed part of underlying text */
bool expandtab; /* whether typed tabs should be converted to spaces in this window*/
Expand Down
6 changes: 6 additions & 0 deletions vis-lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,8 @@ static int window_options_assign(Win *win, lua_State *L, const char *key, int ne
else
flags &= ~UI_OPTION_LINE_NUMBERS_RELATIVE;
win_options_set(win, flags);
} else if (strcmp(key, "numberwidth") == 0) {
win->min_sidebar_width = MAX(0, luaL_checkinteger(L, next));
} else if (strcmp(key, "showeof") == 0) {
if (lua_toboolean(L, next))
flags |= UI_OPTION_SYMBOL_EOF;
Expand Down Expand Up @@ -2188,6 +2190,7 @@ static const struct luaL_Reg window_funcs[] = {
* @tfield[opt=false] boolean expandtab {et}
* @tfield[opt=false] boolean numbers {nu}
* @tfield[opt=false] boolean relativenumbers {rnu}
* @tfield[opt=0] int numberwidth
* @tfield[opt=true] boolean showeof
* @tfield[opt=false] boolean shownewlines
* @tfield[opt=false] boolean showspaces
Expand Down Expand Up @@ -2222,6 +2225,9 @@ static int window_options_index(lua_State *L) {
} else if (strcmp(key, "relativenumbers") == 0 || strcmp(key, "rnu") == 0) {
lua_pushboolean(L, win->options & UI_OPTION_LINE_NUMBERS_RELATIVE);
return 1;
} else if (strcmp(key, "numberwidth") == 0) {
lua_pushinteger(L, win->min_sidebar_width);
return 1;
} else if (strcmp(key, "showeof") == 0) {
lua_pushboolean(L, win->options & UI_OPTION_SYMBOL_EOF);
return 1;
Expand Down
Loading