From a578d9544171f43d97bdad0b3c320cd365c70cae Mon Sep 17 00:00:00 2001 From: Marco Kellershoff <1384938+gorillamoe@users.noreply.github.com> Date: Thu, 15 Aug 2024 12:23:11 +0200 Subject: [PATCH] fix: winbar + default_view (#141) closes #139 --- lua/kulala/globals/init.lua | 2 +- lua/kulala/ui/init.lua | 39 +++++++++++++++---------------------- lua/kulala/ui/winbar.lua | 17 ++++++++++++---- 3 files changed, 30 insertions(+), 28 deletions(-) diff --git a/lua/kulala/globals/init.lua b/lua/kulala/globals/init.lua index 5d4ad57..dc5acd9 100644 --- a/lua/kulala/globals/init.lua +++ b/lua/kulala/globals/init.lua @@ -2,7 +2,7 @@ local FS = require("kulala.utils.fs") local M = {} -M.VERSION = "3.1.3" +M.VERSION = "3.1.4" M.UI_ID = "kulala://ui" M.SCRATCHPAD_ID = "kulala://scratchpad" M.HEADERS_FILE = FS.get_plugin_tmp_dir() .. "/headers.txt" diff --git a/lua/kulala/ui/init.lua b/lua/kulala/ui/init.lua index 2871fc6..f8a32ef 100644 --- a/lua/kulala/ui/init.lua +++ b/lua/kulala/ui/init.lua @@ -167,19 +167,10 @@ M.open = function() end if CONFIG.get().default_view == "body" then M.show_body() - if CONFIG.get().winbar then - WINBAR.toggle_winbar_tab(get_win(), "body") - end elseif CONFIG.get().default_view == "headers" then M.show_headers() - if CONFIG.get().winbar then - WINBAR.toggle_winbar_tab(get_win(), "headers") - end - else + elseif CONFIG.get().default_view == "headers_body" then M.show_headers_body() - if CONFIG.get().winbar then - WINBAR.toggle_winbar_tab(get_win(), "headers_body") - end end end end) @@ -207,6 +198,10 @@ M.show_body = function() body = FORMATTER.format(contenttype.formatter, body) end set_buffer_contents(body, contenttype.ft) + if CONFIG.get().winbar then + WINBAR.toggle_winbar_tab(get_win(), "body") + end + CONFIG.options.default_view = "body" else vim.notify("No body found", vim.log.levels.WARN) end @@ -220,6 +215,10 @@ M.show_headers = function() local h = FS.read_file(GLOBALS.HEADERS_FILE) h = h:gsub("\r\n", "\n") set_buffer_contents(h, "text") + if CONFIG.get().winbar then + WINBAR.toggle_winbar_tab(get_win(), "headers") + end + CONFIG.options.default_view = "headers" else vim.notify("No headers found", vim.log.levels.WARN) end @@ -238,6 +237,10 @@ M.show_headers_body = function() body = FORMATTER.format(contenttype.formatter, body) end set_buffer_contents(h .. "\n" .. body, contenttype.ft) + if CONFIG.get().winbar then + WINBAR.toggle_winbar_tab(get_win(), "headers_body") + end + CONFIG.options.default_view = "headers_body" else vim.notify("No headers or body found", vim.log.levels.WARN) end @@ -260,14 +263,10 @@ M.replay = function() end if CONFIG.get().default_view == "body" then M.show_body() - if CONFIG.get().winbar then - WINBAR.toggle_winbar_tab(get_win(), "body") - end - else + elseif CONFIG.get().default_view == "headers" then M.show_headers() - if CONFIG.get().winbar then - WINBAR.toggle_winbar_tab(get_win(), "headers") - end + elseif CONFIG.get().default_view == "headers_body" then + M.show_headers_body() end end end) @@ -291,14 +290,8 @@ M.toggle_headers = function() CONFIG.set(cfg) if cfg.default_view == "body" then M.show_body() - if cfg.winbar then - WINBAR.toggle_winbar_tab(get_win(), "body") - end else M.show_headers() - if cfg.winbar then - WINBAR.toggle_winbar_tab(get_win(), "headers") - end end end diff --git a/lua/kulala/ui/winbar.lua b/lua/kulala/ui/winbar.lua index 3216f54..2648f3c 100644 --- a/lua/kulala/ui/winbar.lua +++ b/lua/kulala/ui/winbar.lua @@ -13,10 +13,13 @@ end M.winbar_set_key_mapping = function(buf) if buf then vim.keymap.set("n", "B", function() - require("kulala.ui").toggle_headers() + require("kulala.ui").show_body() end, { silent = true, buffer = buf }) vim.keymap.set("n", "H", function() - require("kulala.ui").toggle_headers() + require("kulala.ui").show_headers() + end, { silent = true, buffer = buf }) + vim.keymap.set("n", "A", function() + require("kulala.ui").show_headers_body() end, { silent = true, buffer = buf }) end end @@ -28,13 +31,19 @@ M.toggle_winbar_tab = function(win_id, view) if view == "body" then vim.api.nvim_set_option_value( "winbar", - "%#KulalaTabSel# Body (B) %* %#KulalaTab# Headers (H) %* ", + "%#KulalaTabSel# Body (B) %* %#KulalaTab# Headers (H) %* %#KulalaTab# All (A) %* ", { win = win_id } ) elseif view == "headers" then vim.api.nvim_set_option_value( "winbar", - "%#KulalaTab# Body (B) %* %#KulalaTabSel# Headers (H) %* ", + "%#KulalaTab# Body (B) %* %#KulalaTabSel# Headers (H) %* %#KulalaTab# All (A) %* ", + { win = win_id } + ) + elseif view == "headers_body" then + vim.api.nvim_set_option_value( + "winbar", + "%#KulalaTab# Body (B) %* %#KulalaTab# Headers (H) %* %#KulalaTabSel# All (A) %* ", { win = win_id } ) end