From 2078c60477842d7cf6991148e23979565737a8b5 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Sat, 26 Jul 2025 11:59:38 +0530 Subject: Refactor heavy + extras.tabline module (incomplete) --- lua/null-browser/api.lua | 1 + lua/null-browser/defaults/vi.lua | 2 +- lua/null-browser/extras/tabline.lua | 77 +++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 lua/null-browser/extras/tabline.lua (limited to 'lua') diff --git a/lua/null-browser/api.lua b/lua/null-browser/api.lua index 8b5cebd..598594e 100644 --- a/lua/null-browser/api.lua +++ b/lua/null-browser/api.lua @@ -318,6 +318,7 @@ local function create_decoration_api(type) __internals.decorations_set_enabled(type, enabled, (opts or {}).win) end; return { + type = function() return type end, enable = function(opts) set_enabled(true, opts) end, disable = function(opts) set_enabled(false, opts) end, is_enabled = function(opts) diff --git a/lua/null-browser/defaults/vi.lua b/lua/null-browser/defaults/vi.lua index 0ef5314..d6193de 100644 --- a/lua/null-browser/defaults/vi.lua +++ b/lua/null-browser/defaults/vi.lua @@ -82,7 +82,7 @@ function M.initialize() web.keymap.set('n', 'b', function() local views_list = {} local views = web.view.list() - for index, view in ipairs(web.view.list()) do + for index, view in ipairs(views) do table.insert(views_list, index .. ': ' .. view.title .. ' (' .. view.url .. ')') end diff --git a/lua/null-browser/extras/tabline.lua b/lua/null-browser/extras/tabline.lua new file mode 100644 index 0000000..c278df5 --- /dev/null +++ b/lua/null-browser/extras/tabline.lua @@ -0,0 +1,77 @@ +local tabline = {} + +-- TODO: click interaction +-- TODO: vertical tabs +function tabline.init(opts) + local decoration = (opts or {}).decoration or web.decorations.top + + web.event.add_listener('WinCreated', { + callback = function(event) + decoration.enable({ win = event.win_id }) + tabline.show_tabs_in_window(event.win_id, decoration); + end, + }) + + -- Toggle tabline in current window + web.keymap.set('n', '', function() + if decoration.is_enabled({ win = 0 }) then + decoration.disable({ win = 0 }) + else + decoration.enable({ win = 0 }) + end + end) +end + +function tabline.show_tabs_in_window(win_id, decoration) + web.event.add_listener('UrlChanged', { + callback = function() + if not decoration.is_enabled({ win = win_id }) then return end + + web.view.set_html(tabline.tabs_html(), { + view = decoration.view({ win = win_id }), + }) + end, + }) +end + +function tabline.tabs_html() + local views_html = '' + for index, view in ipairs(web.view.list()) do + -- TODO: Sanitize this stuff + local text = index .. ': ' .. view.title .. ' (' .. view.url .. ')' + local classes = 'tab' + if web.view.current() == view.id then classes = classes .. ' current' end + local html = '
' .. text .. '
' + views_html = views_html .. html + end + + return '' .. + '
' .. views_html .. '
' +end + +function tabline.css() + return [[ + html, body { + background: #000; + } + .tabs { + display: flex; + align-items: stretch; + height: 100vh; + } + .tab { + flex: 1; + width: 100%; + overflow: hidden; + text-overflow: ellipsis; + text-wrap: nowrap; + min-width: 0; + border-left: 1px solid white; + } + .tab.current { + background: #333; + } + ]] +end + +return tabline -- cgit v1.3.1