Skip to content

Commit

Permalink
Merge branch 'main' into feat/process-cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
HakonHarnes committed Mar 23, 2024
2 parents 7dbac7b + 2dc1d02 commit ff4e2f5
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 81 deletions.
28 changes: 14 additions & 14 deletions lua/img-clip/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ M.config_file = "Default"
M.sorted_files = {}
M.sorted_dirs = {}
M.configs = {}
M.api_opts = {}
M.opts = {}

local defaults = {
Expand Down Expand Up @@ -205,7 +206,7 @@ local function get_custom_opt(key, opts, args)

for _, config_opts in ipairs(opts["custom"]) do
if config_opts["trigger"] and get_val(config_opts["trigger"]) then
return M.get_opt(key, {}, args, config_opts)
return M.get_opt(key, args, config_opts)
end
end
end
Expand All @@ -225,7 +226,7 @@ local function get_file_opt(key, opts, args, file)

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])
return M.get_opt(key, args, opts["files"][config_file])
end
end

Expand All @@ -247,7 +248,7 @@ local function get_dir_opt(key, opts, args, dir)

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])
return M.get_opt(key, args, opts["dirs"][config_dir])
end
end

Expand Down Expand Up @@ -276,22 +277,21 @@ local function get_unscoped_opt(key, opts)
end

---@param key string: The key, may be nested (e.g. "default.debug")
---@param api_opts? table: The opts passed to pasteImage function
---@param args? table: Args that should be passed to the option function
---@param opts? table: The opts table to use instead of the config
---@param opts? table: Opts passed explicitly to the function
---@return string | nil
M.get_opt = function(key, api_opts, args, opts)
if api_opts then
local val = M.get_opt(key, nil, args, api_opts)
if val then
return get_val(val, args)
M.get_opt = function(key, args, opts)
-- use explicit opts if provided
-- otherwise, try to get the value from the api_opts
-- and then from the config file
if not opts then
local val = M.get_opt(key, args, M.api_opts)
if val ~= nil then
return val
end
return M.get_opt(key, args, M.get_config())
end

-- if options are passed explicitly, use those instead of the config
-- otherwise use the config (either from file or neovim config)
opts = opts or M.get_config()

local val = get_custom_opt(key, opts, args)
if val == nil then
val = get_file_opt(key, opts, args, vim.fn.expand("%:p"))
Expand Down
15 changes: 7 additions & 8 deletions lua/img-clip/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,29 +71,28 @@ M.add_file_ext = function(str, ext)
end

---@param ext string
---@param opts? table
---@return string
M.get_file_path = function(ext, opts)
local config_dir_path = config.get_opt("dir_path", opts)
local config_file_name = os.date(config.get_opt("file_name", opts))
M.get_file_path = function(ext)
local config_dir_path = config.get_opt("dir_path")
local config_file_name = os.date(config.get_opt("file_name"))

local dir_path = config_dir_path
if config.get_opt("relative_to_current_file", opts) then
if config.get_opt("relative_to_current_file") then
local current_file_path = vim.fn.expand("%:.:h")
if current_file_path ~= "." and current_file_path ~= "" then
dir_path = current_file_path .. M.sep .. config_dir_path
end
end

if config.get_opt("use_absolute_path", opts) then
if config.get_opt("use_absolute_path") then
dir_path = vim.fn.fnamemodify(dir_path, ":p")
end

dir_path = M.normalize_path(dir_path)

local file_path
if config.get_opt("prompt_for_file_name", opts) then
if config.get_opt("show_dir_path_in_prompt", opts) then
if config.get_opt("prompt_for_file_name") then
if config.get_opt("show_dir_path_in_prompt") then
local input_file_path = util.input({
prompt = "File path: ",
default = dir_path,
Expand Down
20 changes: 11 additions & 9 deletions lua/img-clip/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ local paste = require("img-clip.paste")

local M = {}

---@param opts? table
M.setup = function(opts)
config.setup(opts)
---@param config_opts? table
M.setup = function(config_opts)
config.setup(config_opts)
end

---@param opts? table
---@param api_opts? table
---@param input? string
M.paste_image = function(opts, input)
return paste.paste_image(opts, input)
M.paste_image = function(api_opts, input)
config.api_opts = api_opts or {}
return paste.paste_image(input)
end

---@param opts? table
---@param api_opts? table
---@param input? string
M.pasteImage = function(opts, input)
return paste.paste_image(opts, input)
M.pasteImage = function(api_opts, input)
config.api_opts = api_opts or {}
return paste.paste_image(input)
end

return M
20 changes: 9 additions & 11 deletions lua/img-clip/markup.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ M.url_encode = function(str)
end

---@param file_path string the file path or base64 string
---@param opts? table
---@return boolean
function M.insert_markup(file_path, opts)
function M.insert_markup(file_path)
local file_name = vim.fn.fnamemodify(file_path, ":t")
local file_name_no_ext = vim.fn.fnamemodify(file_path, ":t:r")
local label = file_name_no_ext:gsub("%s+", "-"):lower()
Expand All @@ -83,13 +82,13 @@ function M.insert_markup(file_path, opts)
cursor = "$CURSOR",
label = label,
}
local template = config.get_opt("template", opts, template_args)
local template = config.get_opt("template", template_args)
if not template then
return false
end

-- url encode path
if config.get_opt("url_encode_path", opts) then
if config.get_opt("url_encode_path") then
file_path = M.url_encode(file_path)
file_path = file_path:gsub("%%", "%%%%") -- escape % so we can call gsub again
end
Expand All @@ -99,7 +98,7 @@ function M.insert_markup(file_path, opts)
template = template:gsub("$FILE_PATH", file_path)
template = template:gsub("$LABEL", label)

if not config.get_opt("use_cursor_in_template", opts) then
if not config.get_opt("use_cursor_in_template") then
template = template:gsub("$CURSOR", "")
end

Expand All @@ -117,7 +116,7 @@ function M.insert_markup(file_path, opts)

vim.api.nvim_win_set_cursor(0, { new_row, new_col })

if config.get_opt("insert_mode_after_paste", opts) and vim.api.nvim_get_mode().mode ~= "i" then
if config.get_opt("insert_mode_after_paste") and vim.api.nvim_get_mode().mode ~= "i" then
if new_col == string.len(line) - 1 then
vim.api.nvim_input("a")
else
Expand All @@ -129,9 +128,8 @@ function M.insert_markup(file_path, opts)
end

---@param base64 string the file path or base64 string
---@param opts? table
---@return boolean
function M.insert_base64_markup(base64, opts)
function M.insert_base64_markup(base64)
-- pass args to template
local template_args = {
file_path = "",
Expand All @@ -140,7 +138,7 @@ function M.insert_base64_markup(base64, opts)
cursor = "$CURSOR",
label = "",
}
local template = config.get_opt("template", opts, template_args)
local template = config.get_opt("template", template_args)
if not template then
return false
end
Expand All @@ -150,7 +148,7 @@ function M.insert_base64_markup(base64, opts)
template = template:gsub("$FILE_PATH", base64)
template = template:gsub("$LABEL", "")

if not config.get_opt("use_cursor_in_template", opts) then
if not config.get_opt("use_cursor_in_template") then
template = template:gsub("$CURSOR", "")
end

Expand All @@ -168,7 +166,7 @@ function M.insert_base64_markup(base64, opts)

vim.api.nvim_win_set_cursor(0, { new_row, new_col })

if config.get_opt("insert_mode_after_paste", opts) and vim.api.nvim_get_mode().mode ~= "i" then
if config.get_opt("insert_mode_after_paste") and vim.api.nvim_get_mode().mode ~= "i" then
if new_col == string.len(line) - 1 then
vim.api.nvim_input("a")
else
Expand Down
41 changes: 18 additions & 23 deletions lua/img-clip/paste.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,17 @@ local fs = require("img-clip.fs")

local M = {}

---@param opts? table
---@param input? string file path or url
---@return boolean
M.paste_image = function(opts, input)
M.paste_image = function(input)
-- check if input (file path or url) is provided
if input then
input = util.sanitize_input(input)

if util.is_image_url(input) then
return M.paste_image_from_url(input, opts)
return M.paste_image_from_url(input)
elseif util.is_image_path(input) then
return M.paste_image_from_path(input, opts)
return M.paste_image_from_path(input)
end

util.warn("Content is not an image.")
Expand All @@ -32,23 +31,22 @@ M.paste_image = function(opts, input)

-- if no input is provided, check clipboard content
if clipboard.content_is_image() then
return M.paste_image_from_clipboard(opts)
return M.paste_image_from_clipboard()
end

-- if clipboard does not contain an image, then get the
-- clipboard content as text and check attempt to paste it
local clipboard_content = clipboard.get_content()
if clipboard_content then
return M.paste_image(opts, clipboard_content)
return M.paste_image(clipboard_content)
end

util.warn("Content is not an image.")
return false
end

---@param url string
---@param opts? table
M.paste_image_from_url = function(url, opts)
M.paste_image_from_url = function(url)
if not config.get_opt("drag_and_drop.download_images") then
if not markup.insert_markup(url) then
util.error("Could not insert markup code.")
Expand All @@ -58,7 +56,7 @@ M.paste_image_from_url = function(url, opts)
return true
end

local file_path = fs.get_file_path("png", opts)
local file_path = fs.get_file_path("png")
if not file_path then
util.error("Could not determine file path.")
return false
Expand Down Expand Up @@ -97,10 +95,9 @@ M.paste_image_from_url = function(url, opts)
end

---@param src_path string
---@param opts? table
M.paste_image_from_path = function(src_path, opts)
if config.get_opt("embed_image_as_base64", opts) then
if M.embed_image_as_base64(src_path, opts) then
M.paste_image_from_path = function(src_path)
if config.get_opt("embed_image_as_base64") then
if M.embed_image_as_base64(src_path) then
return true
end
end
Expand All @@ -114,7 +111,7 @@ M.paste_image_from_path = function(src_path, opts)
return true
end

local file_path = fs.get_file_path("png", opts)
local file_path = fs.get_file_path("png")
if not file_path then
util.error("Could not determine file path.")
return false
Expand Down Expand Up @@ -145,15 +142,14 @@ M.paste_image_from_path = function(src_path, opts)
return true
end

---@param opts? table
M.paste_image_from_clipboard = function(opts)
if config.get_opt("embed_image_as_base64", opts) then
if M.embed_image_as_base64(nil, opts) then
M.paste_image_from_clipboard = function()
if config.get_opt("embed_image_as_base64") then
if M.embed_image_as_base64(nil) then
return true
end
end

local file_path = fs.get_file_path("png", opts)
local file_path = fs.get_file_path("png")
if not file_path then
util.error("Could not determine file path.")
return false
Expand Down Expand Up @@ -185,8 +181,7 @@ M.paste_image_from_clipboard = function(opts)
end

---@param file_path? string
---@param opts? table
M.embed_image_as_base64 = function(file_path, opts)
M.embed_image_as_base64 = function(file_path)
local ft = vim.bo.filetype
if not M.lang_supports_base64(ft) then
util.warn("Filetype " .. ft .. " does not support base64 encoding.")
Expand All @@ -207,14 +202,14 @@ M.embed_image_as_base64 = function(file_path, opts)

-- check if base64 string is too long (max_base64_size is in KB)
local base64_size_kb = math.floor((string.len(base64) * 6) / (8 * 1024))
local max_size_kb = config.get_opt("max_base64_size", opts)
local max_size_kb = config.get_opt("max_base64_size")
if base64_size_kb > max_size_kb then
util.warn("Base64 string is too large (" .. base64_size_kb .. " KB). Max allowed size is " .. max_size_kb .. " KB.")
return false
end

local prefix = M.get_base64_prefix()
if not markup.insert_base64_markup(prefix .. base64, opts) then
if not markup.insert_base64_markup(prefix .. base64) then
util.error("Could not insert markup code.")
return false
end
Expand Down
28 changes: 14 additions & 14 deletions tests/config_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,27 @@ describe("config", function()
end)

it("should prioritize API options over config values", function()
assert.equals("custom-filename", config.get_opt("file_name", { file_name = "custom-filename" }))
assert.equals("some-dir-path", config.get_opt("dir_path", { dir_path = "some-dir-path" }))
config.api_opts = { dir_path = "some-dir-path", file_name = "custom-filename" }
assert.equals("custom-filename", config.get_opt("file_name"))
assert.equals("some-dir-path", config.get_opt("dir_path"))
config.api_opts = {}
end)

it("should execute functions that are passed in the API", function()
assert.equals(
42,
config.get_opt("life", {
life = function()
return 40 + 2
end,
})
)
config.api_opts = {
life = function()
return 42
end,
}
assert.equals(42, config.get_opt("life"))
config.api_opts = {}
end)

it("should allow nested API options", function()
vim.bo.filetype = "markdown"

assert.equals(
"markdown-template",
config.get_opt("template", { filetypes = { markdown = { template = "markdown-template" } } })
)
config.api_opts = { filetypes = { markdown = { template = "markdown-template" } } }
assert.equals("markdown-template", config.get_opt("template"))
config.api_opts = {}
end)
end)
Loading

0 comments on commit ff4e2f5

Please sign in to comment.