Skip to content

Commit

Permalink
Merge pull request #38 from mistweaverco/fix/config-reset
Browse files Browse the repository at this point in the history
fix: config-reset
  • Loading branch information
gorillamoe authored Jul 8, 2024
2 parents 005cd22 + fbd533e commit 065e15e
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 31 deletions.
21 changes: 13 additions & 8 deletions lua/kulala/config/init.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
KULALA_CONFIG = KULALA_CONFIG or {
local M = {}

M.defaults = {
-- default_view, body or headers
default_view = "body",
-- dev, test, prod, can be anything
Expand All @@ -16,7 +18,7 @@ KULALA_CONFIG = KULALA_CONFIG or {
icons = {
inlay = {
loading = "",
done = " "
done = ""
},
lualine = "🐼",
},
Expand All @@ -25,15 +27,18 @@ KULALA_CONFIG = KULALA_CONFIG or {
additional_curl_options = {},
}

local M = {}
M.options = {}

M.setup = function(config)
M.options = vim.tbl_deep_extend("force", M.defaults, config or {})
end

M.set_config = function(config)
config = config or {}
KULALA_CONFIG = vim.tbl_deep_extend("force", KULALA_CONFIG, config)
M.set = function(config)
M.options = vim.tbl_deep_extend("force", M.options, config or {})
end

M.get_config = function()
return KULALA_CONFIG
M.get = function()
return M.options
end

return M
8 changes: 4 additions & 4 deletions lua/kulala/formatter/init.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
local CONFIG = require("kulala.config")
local FS = require("kulala.utils.fs")
local CFG = CONFIG.get_config()
local M = {}

M.format = function(ft, contents)
local cfg = CONFIG.get()
local cmd = {}
local cmd_exists = false
if ft == "json" then
cmd = CFG.formatters.json
cmd = cfg.formatters.json
cmd_exists = FS.command_exists("jq")
elseif ft == "xml" then
cmd = CFG.formatters.xml
cmd = cfg.formatters.xml
cmd_exists = FS.command_exists("xmllint")
elseif ft == "html" then
cmd = CFG.formatters.html
cmd = cfg.formatters.html
cmd_exists = FS.command_exists("xmllint")
end
if not cmd_exists then
Expand Down
6 changes: 3 additions & 3 deletions lua/kulala/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ local JUMPS = require("kulala.jumps")
local M = {}

M.setup = function(config)
CONFIG.set_config(config)
GLOBAL_STORE.set("selected_env", CONFIG.get_config().default_env)
vim.g.kulala_selected_env = CONFIG.get_config().default_env
CONFIG.setup(config)
GLOBAL_STORE.set("selected_env", CONFIG.get().default_env)
vim.g.kulala_selected_env = CONFIG.get().default_env
ENV_PARSER.load_envs()
end

Expand Down
8 changes: 6 additions & 2 deletions lua/kulala/inlay/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ M.clear = function()
end

M.show_loading = function()
M.show(CONFIG.get_config().icons.inlay.loading)
M.show(CONFIG.get().icons.inlay.loading)
end

M.show_done = function(self, elapsed_time)
M.show(CONFIG.get_config().icons.inlay.done .. 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)
end


Expand Down
5 changes: 2 additions & 3 deletions lua/kulala/parser/dynamic_vars.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
local Config = require("kulala.config")
local CONFIG = require("kulala.config")
local M = {}

local config = Config.get_config()
local random = math.random
math.randomseed(os.time())

Expand All @@ -20,7 +19,7 @@ end
---the user on his configuration
---@return { [string]: fun():string }[] An array-like table of tables which contains dynamic variables definition
function M.retrieve_all()
local user_variables = config.custom_dynamic_variables or {}
local user_variables = CONFIG.get().custom_dynamic_variables or {}
local rest_variables = {
["$uuid"] = uuid,
["$date"] = function()
Expand Down
5 changes: 2 additions & 3 deletions lua/kulala/parser/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ local CONFIG = require("kulala.config")
local DYNAMIC_VARS = require("kulala.parser.dynamic_vars")
local STRING_UTILS = require("kulala.utils.string")
local ENV_PARSER = require("kulala.parser.env")
local CFG = CONFIG.get_config()
local PLUGIN_TMP_DIR = FS.get_plugin_tmp_dir()
local M = {}

Expand Down Expand Up @@ -457,7 +456,7 @@ function M.parse()
end
table.insert(res.cmd, "-A")
table.insert(res.cmd, "kulala.nvim/".. GLOBALS.VERSION)
for _, additional_curl_option in pairs(CFG.additional_curl_options) do
for _, additional_curl_option in pairs(CONFIG.get().additional_curl_options) do
table.insert(res.cmd, additional_curl_option)
end
table.insert(res.cmd, res.request.url)
Expand All @@ -469,7 +468,7 @@ function M.parse()
res.ft = "html"
end
FS.write_file(PLUGIN_TMP_DIR .. "/ft.txt", res.ft)
if CFG.debug then
if CONFIG.get().debug then
FS.write_file(PLUGIN_TMP_DIR .. "/request.txt", table.concat(res.cmd, " "))
end
return res
Expand Down
14 changes: 7 additions & 7 deletions lua/kulala/ui/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local GLOBALS = require("kulala.globals")
local CONFIG = require("kulala.config")
local CFG = CONFIG.get_config()
local INLAY = require("kulala.inlay")
local PARSER = require("kulala.parser")
local CMD = require("kulala.cmd")
Expand Down Expand Up @@ -81,7 +80,7 @@ M.open = function()
if not buffer_exists() then
open_buffer()
end
if CFG.default_view == "body" then
if CONFIG.get().default_view == "body" then
M.show_body()
else
M.show_headers()
Expand Down Expand Up @@ -116,13 +115,14 @@ M.show_headers = function()
end

M.toggle_headers = function()
if CFG.default_view == "headers" then
CFG.default_view = "body"
cfg = CONFIG.get()
if cfg.default_view == "headers" then
cfg.default_view = "body"
else
CFG.default_view = "headers"
cfg.default_view = "headers"
end
CONFIG.set_config(CFG)
if CFG.default_view == "body" then
CONFIG.set(cfg)
if cfg.default_view == "body" then
M.show_body()
else
M.show_headers()
Expand Down
2 changes: 1 addition & 1 deletion lua/lualine/components/kulala.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local CONFIG = require("kulala.config")

local default_options = {
fg = "#10B1FE",
icon = CONFIG.get_config().icons.lualine,
icon = CONFIG.get().icons.lualine,
}

function M:init(options)
Expand Down

0 comments on commit 065e15e

Please sign in to comment.