Skip to content

Commit

Permalink
feat: add ImgClipDebug command (#43)
Browse files Browse the repository at this point in the history
* feat: add ImgClipDebug command

* docs: update readme
  • Loading branch information
HakonHarnes authored Mar 1, 2024
1 parent fc365b2 commit 6ed4218
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ return {
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.

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

Expand All @@ -77,7 +78,6 @@ The plugin comes with the following defaults:
```lua
{
default = {
debug = false, -- enable debug mode
dir_path = "assets", -- directory path to save images to, can be relative (cwd or current file) or absolute
file_name = "%Y-%m-%d-%H-%M-%S", -- file name format (see lua.org/pil/22.1.html)
url_encode_path = false, -- encode spaces and special characters in file path
Expand Down
1 change: 0 additions & 1 deletion doc/img-clip.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ The plugin comes with the following defaults:
>lua
{
default = {
debug = false, -- enable debug mode
dir_path = "assets", -- directory path to save images to, can be relative (cwd or current file) or absolute
file_name = "%Y-%m-%d-%H-%M-%S", -- file name format (see lua.org/pil/22.1.html)
url_encode_path = false, -- encode spaces and special characters in file path
Expand Down
1 change: 0 additions & 1 deletion lua/img-clip/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ M.opts = {}

local defaults = {
default = {
debug = false, -- enable debug mode
dir_path = "assets", -- directory path to save images to, can be relative (cwd or current file) or absolute
file_name = "%Y-%m-%d-%H-%M-%S", -- file name format (see lua.org/pil/22.1.html)
url_encode_path = false, -- encode spaces and special characters in file path
Expand Down
15 changes: 15 additions & 0 deletions lua/img-clip/debug.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
local M = {}

M.debug_log = {}

M.log = function(msg)
table.insert(M.debug_log, msg)
end

M.print_log = function()
for _, msg in ipairs(M.debug_log) do
print(msg)
end
end

return M
11 changes: 5 additions & 6 deletions lua/img-clip/util.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local config = require("img-clip.config")
local debug = require("img-clip.debug")

local M = {}

Expand Down Expand Up @@ -36,12 +37,10 @@ M.execute = function(input_cmd)
local output = vim.fn.system(cmd)
local exit_code = vim.v.shell_error

if config.get_opt("debug") then
print("Shell: " .. shell)
print("Command: " .. cmd)
print("Exit code: " .. exit_code)
print("Output: " .. output)
end
debug.log("Shell: " .. shell)
debug.log("Command: " .. cmd)
debug.log("Exit code: " .. exit_code)
debug.log("Output: " .. output)

return output, exit_code
end
Expand Down
17 changes: 8 additions & 9 deletions plugin/img-clip.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local debug = require("img-clip.debug")
local config = require("img-clip.config")
local util = require("img-clip.util")
local plugin = require("img-clip")
Expand All @@ -8,6 +9,10 @@ vim.api.nvim_create_user_command("PasteImage", function()
plugin.pasteImage()
end, {})

vim.api.nvim_create_user_command("ImgClipDebug", function()
debug.print_log()
end, {})

local buffer = ""

---@param lines string[]
Expand Down Expand Up @@ -35,9 +40,7 @@ end
-- it will contain the path to the image or file, or a link to the image
vim.paste = (function(original)
return function(lines, phase)
if config.get_opt("debug") then
print("Paste: " .. vim.inspect(lines))
end
debug.log("Paste: " .. vim.inspect(lines))

if config.get_opt("drag_and_drop.enabled") == false then
return original(lines, phase)
Expand All @@ -57,9 +60,7 @@ vim.paste = (function(original)

local line = lines[1]

if config.get_opt("debug") then
print("Line: " .. line)
end
debug.log("Line: " .. line)

-- probably not a file path or url to an image if the input is this long
if string.len(line) > 512 then
Expand All @@ -68,9 +69,7 @@ vim.paste = (function(original)

util.verbose = false
if not plugin.pasteImage({}, line) then
if config.get_opt("debug") then
print("Did not handle paste, calling original vim.paste")
end
debug.log("Did not handle paste, calling original vim.paste")
util.verbose = true
return original(lines, phase) -- if we did not handle the paste, call the original vim.paste function
end
Expand Down
3 changes: 1 addition & 2 deletions tests/config_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ describe("config", function()
end)

it("should have default values for all configuration options", function()
assert.is_false(config.get_opt("debug"))
assert.equals("assets", config.get_opt("dir_path"))
assert.equals("%Y-%m-%d-%H-%M-%S", config.get_opt("file_name"))
assert.is_false(config.get_opt("url_encode_path"))
Expand Down Expand Up @@ -58,7 +57,7 @@ describe("config", function()

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

it("should execute functions that are passed in the API", function()
Expand Down

0 comments on commit 6ed4218

Please sign in to comment.