Skip to content

Commit

Permalink
feat: config command (#45)
Browse files Browse the repository at this point in the history
* feat: add ImgClipConfig command

* docs: document ImgClipConfig command
  • Loading branch information
HakonHarnes authored Mar 1, 2024
1 parent 6ed4218 commit 5f17013
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ The plugin comes with the following commands:

- `PasteImage` Inserts the image from the clipboard into the document.
- `ImgClipDebug` Prints the debug log, including the output of shell commands.
- `ImgClipConfig` Prints the current configuration.

Consider binding `PasteImage` to something like `<leader>p`.

Expand Down
27 changes: 21 additions & 6 deletions lua/img-clip/config.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
local M = {}

M.config_file = "Default"
M.sorted_files = {}
M.sorted_dirs = {}
M.configs = {}
M.opts = {}

Expand Down Expand Up @@ -99,6 +102,8 @@ defaults.filetypes.plaintex = defaults.filetypes.tex
defaults.filetypes.rmd = defaults.filetypes.markdown
defaults.filetypes.md = defaults.filetypes.markdown

---sorts the files and dirs tables by length of the path
---so more specific paths have priority over less specific paths
---@param opts table
---@return table
M.sort_config = function(opts)
Expand All @@ -116,23 +121,24 @@ M.sort_config = function(opts)
return sorted_keys
end

opts["sorted_files"] = sort_keys(opts["files"])
opts["sorted_dirs"] = sort_keys(opts["dirs"])
M.sorted_files = sort_keys(opts["files"])
M.sorted_dirs = sort_keys(opts["dirs"])

return opts
end

---get the config
---Can be either the default config or the config from the config file
---can be either the default config or the config from the config file
---@return table
M.get_config = function()
-- use cached config if available
local dir_path = vim.fn.expand("%:p:h")
if M.configs[dir_path] and M.configs[dir_path] ~= {} then
return M.configs[dir_path]

-- no config file found, use default config
-- no cached config file found, use default config
elseif M.configs[dir_path] == {} then
M.config_file = "Default"
return M.opts
end

Expand All @@ -144,6 +150,7 @@ M.get_config = function()
if success then
local opts = vim.tbl_deep_extend("force", {}, defaults, output)
M.configs[dir_path] = M.sort_config(opts)
M.config_file = config_file
return M.configs[dir_path]
else
M.configs[dir_path] = {}
Expand All @@ -152,6 +159,7 @@ M.get_config = function()
end

-- use default config if no config file is found
M.config_file = "Default"
return M.opts
end

Expand Down Expand Up @@ -214,7 +222,7 @@ local function get_file_opt(key, opts, args, file)
return string.sub(f1:lower(), -#f2:lower()) == f2:lower()
end

for _, config_file in ipairs(opts["sorted_files"]) do
for _, config_file in ipairs(M.sorted_files) do
if file_matches(file, config_file) or file_matches(file, vim.fn.resolve(vim.fn.expand(config_file))) then
return M.get_opt(key, {}, args, opts["files"][config_file])
end
Expand All @@ -236,7 +244,7 @@ local function get_dir_opt(key, opts, args, dir)
return string.find(d1:lower(), d2:lower(), 1, true)
end

for _, config_dir in ipairs(opts["sorted_dirs"]) do
for _, config_dir in ipairs(M.sorted_dirs) do
if dir_matches(dir, config_dir) or dir_matches(dir, vim.fn.resolve(vim.fn.expand(config_dir))) then
return M.get_opt(key, {}, args, opts["dirs"][config_dir])
end
Expand Down Expand Up @@ -309,4 +317,11 @@ function M.setup(config_opts)
M.opts = M.sort_config(M.opts)
end

M.print_config = function()
local config = M.get_config()

print("Config file: " .. M.config_file)
vim.print(config)
end

return M
4 changes: 4 additions & 0 deletions plugin/img-clip.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ vim.api.nvim_create_user_command("ImgClipDebug", function()
debug.print_log()
end, {})

vim.api.nvim_create_user_command("ImgClipConfig", function()
config.print_config()
end, {})

local buffer = ""

---@param lines string[]
Expand Down

0 comments on commit 5f17013

Please sign in to comment.