From edb6185a3fbb0d84bfe741ba5910c5bf68a9ebae Mon Sep 17 00:00:00 2001 From: Marco Kellershoff Date: Sat, 27 Jul 2024 10:50:11 +0200 Subject: [PATCH] feat(ui): add `close()` function closes #75. --- docs/docs/public_methods.md | 6 ++++++ lua/kulala/globals/init.lua | 2 +- lua/kulala/init.lua | 4 ++++ lua/kulala/ui/init.lua | 16 +++++++++++++++- package.json | 2 +- 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/docs/docs/public_methods.md b/docs/docs/public_methods.md index 08ba2b4..3eb3933 100644 --- a/docs/docs/public_methods.md +++ b/docs/docs/public_methods.md @@ -9,6 +9,12 @@ `require('kulala').copy()` copies the current request (as cURL command) to the clipboard. +### close + +`require('kulala').close()` closes the kulala window and also the current buffer. + +> (it will not close the current buffer, if it is not a `.http` or `.rest` file) + ### toggle_view `require('kulala').toggle_view()` toggles between diff --git a/lua/kulala/globals/init.lua b/lua/kulala/globals/init.lua index 5f3766f..6dd9a6b 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 = "2.6.0" +M.VERSION = "2.7.0" M.UI_ID = "kulala://ui" M.HEADERS_FILE = FS.get_plugin_tmp_dir() .. "/headers.txt" M.BODY_FILE = FS.get_plugin_tmp_dir() .. "/body.txt" diff --git a/lua/kulala/init.lua b/lua/kulala/init.lua index c2ed1ac..a983486 100644 --- a/lua/kulala/init.lua +++ b/lua/kulala/init.lua @@ -43,6 +43,10 @@ M.toggle_view = function() UI:toggle_headers() end +M.close = function() + UI:close() +end + M.set_selected_env = function(env) if env == nil then local has_telescope, telescope = pcall(require, "telescope") diff --git a/lua/kulala/ui/init.lua b/lua/kulala/ui/init.lua index e363c27..8ad6255 100644 --- a/lua/kulala/ui/init.lua +++ b/lua/kulala/ui/init.lua @@ -13,6 +13,10 @@ local open_buffer = function() vim.api.nvim_set_current_win(prev_win) end +local close_buffer = function() + vim.cmd("bdelete " .. GLOBALS.UI_ID) +end + local get_buffer = function() -- Iterate through all buffers for _, buf in ipairs(vim.api.nvim_list_bufs()) do @@ -122,6 +126,16 @@ M.open = function() end) end +M.close = function() + if buffer_exists() then + close_buffer() + end + local ext = vim.fn.expand("%:e") + if ext == "http" or ext == "rest" then + vim.cmd("bdelete") + end +end + M.show_body = function() if FS.file_exists(GLOBALS.BODY_FILE) then if not buffer_exists() then @@ -149,7 +163,7 @@ M.show_headers = function() end M.toggle_headers = function() - cfg = CONFIG.get() + local cfg = CONFIG.get() if cfg.default_view == "headers" then cfg.default_view = "body" else diff --git a/package.json b/package.json index 1a1a921..6507a4d 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,4 @@ { "name": "kulala.nvim", - "version": "2.6.0" + "version": "2.7.0" }