Skip to content

Commit

Permalink
fix: error-cases + reformat + versionbump
Browse files Browse the repository at this point in the history
  • Loading branch information
gorillamoe committed Jul 10, 2024
1 parent bcf4ff6 commit 9904d82
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 34 deletions.
2 changes: 1 addition & 1 deletion docs/_coverpage.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# kulala.nvim <small>1.5.2</small>
# kulala.nvim <small>1.6.0</small>

> A minimal 🤏 HTTP-client 🐼 interface 🖥️ for Neovim ❤️.
Expand Down
30 changes: 21 additions & 9 deletions lua/kulala/cmd/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,27 @@ local FS = require("kulala.utils.fs")
local M = {}

-- runs the cmd and maybe formats the result
M.run = function(result)
vim.fn.jobstart(result.cmd, {
on_stdout = function()
if result.ft then
local body = FS.read_file(GLOBALS.BODY_FILE)
FS.write_file(GLOBALS.BODY_FILE, FORMATTER.format(result.ft, body))
end
end,
})
M.run = function(result, callback)
vim.fn.jobstart(result.cmd, {
on_stdout = function(_, datalist)
if result.ft then
local body = FS.read_file(GLOBALS.BODY_FILE)
FS.write_file(GLOBALS.BODY_FILE, FORMATTER.format(result.ft, body))
end
end,
on_stderr = function(_, datalist)
if callback then
if #datalist > 0 and #datalist[1] > 0 then
vim.notify(vim.inspect(datalist), vim.log.levels.ERROR)
end
end
end,
on_exit = function(_, code)
if callback then
callback(code == 0)
end
end,
})
end

return M
3 changes: 2 additions & 1 deletion lua/kulala/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ M.defaults = {
icons = {
inlay = {
loading = "",
done = ""
done = "",
error = "",
},
lualine = "🐼",
},
Expand Down
2 changes: 1 addition & 1 deletion lua/kulala/globals/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local FS = require("kulala.utils.fs")

local M = {}

M.VERSION = "1.5.2"
M.VERSION = "1.6.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"
Expand Down
18 changes: 10 additions & 8 deletions lua/kulala/inlay/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local CONFIG = require("kulala.config")

local M = {}

local function get_current_line_number()
M.get_current_line_number = function()
local linenr = vim.api.nvim_win_get_cursor(0)[1]
return linenr
end
Expand All @@ -12,22 +12,24 @@ M.clear = function()
vim.api.nvim_buf_clear_namespace(0, NS, 0, -1)
end

M.show_loading = function()
M.show(CONFIG.get().icons.inlay.loading)
M.show_loading = function(self, linenr)
M.show(CONFIG.get().icons.inlay.loading, linenr)
end

M.show_done = function(self, elapsed_time)
M.show_error = function(self, linenr)
M.show(CONFIG.get().icons.inlay.error, linenr)
end

M.show_done = function(self, linenr, elapsed_time)
icon = ""
if string.len(CONFIG.get().icons.inlay.done) > 0 then
icon = CONFIG.get().icons.inlay.done .. " "
end
M.show(icon .. elapsed_time)
M.show(icon .. elapsed_time, linenr)
end


M.show = function(t)
M.show = function(t, linenr)
M.clear()
local linenr = get_current_line_number()
local bufnr = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_extmark(bufnr, NS, linenr - 1, 0, {
virt_text = { { t } }
Expand Down
4 changes: 4 additions & 0 deletions lua/kulala/parser/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ function M.parse()
-- build the command to exectute the request
table.insert(res.cmd, "curl")
table.insert(res.cmd, "-s")
table.insert(res.cmd, "--show-error")
table.insert(res.cmd, "--fail")
table.insert(res.cmd, "-D")
table.insert(res.cmd, PLUGIN_TMP_DIR .. "/headers.txt")
table.insert(res.cmd, "-o")
Expand Down Expand Up @@ -467,6 +469,8 @@ function M.parse()
elseif res.headers['accept'] == "text/html" then
res.ft = "html"
end
FS.delete_file(PLUGIN_TMP_DIR .. "/headers.txt")
FS.delete_file(PLUGIN_TMP_DIR .. "/body.txt")
FS.write_file(PLUGIN_TMP_DIR .. "/ft.txt", res.ft)
if CONFIG.get().debug then
FS.write_file(PLUGIN_TMP_DIR .. "/request.txt", table.concat(res.cmd, " "))
Expand Down
33 changes: 20 additions & 13 deletions lua/kulala/ui/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,22 +69,29 @@ local function pretty_ms(ms)
end

M.open = function()
INLAY:show_loading()
local linenr = INLAY.get_current_line_number()
INLAY:show_loading(linenr)
local result = PARSER:parse()
vim.schedule(function()
local start = vim.loop.hrtime()
local result_body = CMD.run(result)
local elapsed = vim.loop.hrtime() - start
local elapsed_ms = pretty_ms(elapsed / 1e6)
INLAY:show_done(elapsed_ms)
if not buffer_exists() then
open_buffer()
end
if CONFIG.get().default_view == "body" then
M.show_body()
else
M.show_headers()
end
CMD.run(result, function(success)
if not success then
INLAY:show_error(linenr)
return
else
local elapsed = vim.loop.hrtime() - start
local elapsed_ms = pretty_ms(elapsed / 1e6)
INLAY:show_done(linenr, elapsed_ms)
if not buffer_exists() then
open_buffer()
end
if CONFIG.get().default_view == "body" then
M.show_body()
else
M.show_headers()
end
end
end)
end)
end

Expand Down
11 changes: 11 additions & 0 deletions lua/kulala/utils/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ M.write_file = function(filename, content)
return true
end

-- Delete a file
-- @param filename: string
-- @return boolean
-- @usage local p = fs.delete_file('Makefile')
M.delete_file = function(filename)
if vim.fn.delete(filename) == 0 then
return false
end
return true
end

-- Check if a file exists
-- @param filename: string
-- @return boolean
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kulala.nvim",
"version": "1.5.2",
"version": "1.6.0",
"scripts": {
"docs": "docsify serve docs"
},
Expand Down

0 comments on commit 9904d82

Please sign in to comment.