From a338f4fd81a00c14f416f4bfa004583e5ede18c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Tue, 24 May 2016 21:45:00 +0200 Subject: vis-lua: cleanup Lua status bar display code --- README.md | 2 +- vis-lua.c | 11 ++++++++++- vis.lua | 26 +++++++++++--------------- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 58e6060..b2b10f9 100644 --- a/README.md +++ b/README.md @@ -623,7 +623,7 @@ At this time there exists no API stability guarantees. - `style(id, start, end)` apply style to file range, will be cleared after every redraw - `viewport` a range denoting the currently visible area of the window - `width` and `height`, readonly, query window dimension - - `status(text)` set the content of the window status bar + - `status(left [,right])` set the content of the window status bar - `cursor` - `line` (1 based), `col` (1 based) - `to(line, col)` diff --git a/vis-lua.c b/vis-lua.c index db8ec96..3a4f0dd 100644 --- a/vis-lua.c +++ b/vis-lua.c @@ -919,7 +919,16 @@ static int window_style(lua_State *L) { static int window_status(lua_State *L) { Win *win = obj_ref_check(L, 1, "vis.window"); if (win) { - const char *status = luaL_checkstring(L, 2); + char status[1024] = ""; + int width = vis_window_width_get(win); + const char *left = luaL_checkstring(L, 2); + const char *right = luaL_optstring(L, 3, ""); + int left_width = text_string_width(left, strlen(left)); + int right_width = text_string_width(right, strlen(right)); + int spaces = width - left_width - right_width; + if (spaces < 1) + spaces = 1; + snprintf(status, sizeof(status)-1, "%s%*s%s", left, spaces, " ", right); vis_window_status(win, status); } return 0; diff --git a/vis.lua b/vis.lua index 4dfb705..ee3ba41 100644 --- a/vis.lua +++ b/vis.lua @@ -296,43 +296,39 @@ local modes = { } vis.events.win_status = function(win) - local left = {} - local right = {} + local left_parts = {} + local right_parts = {} local file = win.file local cursor = win.cursor - local delim_len = 1 local mode = modes[vis.mode] if mode ~= '' and vis.win == win then - table.insert(left, mode) + table.insert(left_parts, mode) end - table.insert(left, (file.name or '[No Name]') .. + table.insert(left_parts, (file.name or '[No Name]') .. (file.modified and ' [+]' or '') .. (vis.recording and ' @' or '')) if file.newlines ~= "nl" then - table.insert(right, "␊") + table.insert(right_parts, "␊") end if #win.cursors > 1 then - table.insert(right, cursor.number..'/'..#win.cursors) + table.insert(right_parts, cursor.number..'/'..#win.cursors) end local size = file.size - table.insert(right, (size == 0 and "0" or math.ceil(cursor.pos/size*100)).."%") + table.insert(right_parts, (size == 0 and "0" or math.ceil(cursor.pos/size*100)).."%") if not win.large then local col = cursor.col - table.insert(right, cursor.line..', '..col) + table.insert(right_parts, cursor.line..', '..col) if size > 33554432 or col > 65536 then win.large = true end end - local left_str = ' ' .. table.concat(left, " » ") .. ' ' - local right_str = ' ' .. table.concat(right, " « ") .. ' ' - local delim_count = math.max(#left-1, 0) + math.max(#right-1, 0) - local spaces = string.rep(' ', win.width - #left_str - #right_str + delim_count*delim_len) - local status = left_str .. spaces .. right_str - win:status(status) + local left = ' ' .. table.concat(left_parts, " » ") .. ' ' + local right = ' ' .. table.concat(right_parts, " « ") .. ' ' + win:status(left, right); end -- cgit v1.2.3