diff --git a/lua/img-clip/fs.lua b/lua/img-clip/fs.lua new file mode 100644 index 0000000..51d4e81 --- /dev/null +++ b/lua/img-clip/fs.lua @@ -0,0 +1,83 @@ +local config = require("img-clip.config") +local util = require("img-clip.util") + +local M = {} + +M.sep = package.config:sub(1, 1) + +M.add_file_ext = function(str, ext) + local str_without_ext = str:gsub("%.[^" .. M.sep .. "]-$", "") + return str_without_ext .. "." .. ext +end + +M.get_filename_from_filepath = function(filepath) + local filename = filepath:match("([^" .. M.sep .. "]+)$") + return filename +end + +M.get_dir_path_from_filepath = function(filepath) + local dir_path = filepath:match("(.*" .. M.sep .. ").*") + return dir_path +end + +M.get_filepath = function(opts) + local config_dir_path = config.get_option("dir_path", opts) + local config_filename = os.date(config.get_option("filename", opts)) + + local dir_path + + if config.get_option("absolute_path", opts) then + local cwd = vim.fn.getcwd() + dir_path = vim.fn.resolve(cwd .. M.sep .. config_dir_path) + else + dir_path = vim.fn.resolve(config_dir_path) + end + + local filepath + if config.get_option("prompt_for_filename", opts) then + if config.get_option("include_filepath_in_prompt", opts) then + local default_filepath = dir_path .. M.sep + local input_filepath = M.input({ + prompt = "Filepath: ", + default = default_filepath, + completion = "file", + }) + if input_filepath ~= "" and input_filepath ~= default_filepath then + filepath = vim.fn.resolve(input_filepath) + end + else + local input_filename = M.input({ prompt = "Filename: ", completion = "file" }) + if input_filename ~= "" then + filepath = vim.fn.resolve(dir_path .. M.sep .. input_filename) + end + end + end + + if not filepath then + filepath = vim.fn.resolve(dir_path .. M.sep .. config_filename) + end + + filepath = M.add_file_ext(filepath, "png") + return filepath +end + +M.mkdirs = function(filepath) + local is_windows = util.has("win32" or util.has("wsl")) + + local dir_path = M.get_dir_path_from_filepath(filepath) + if not dir_path then + return + end -- if no directory in path, return + + local command + if is_windows then + command = string.format('mkdir "%s"', dir_path) + else + command = string.format('mkdir -p "%s"', dir_path) + end + + local exit_code = os.execute(command) + return exit_code == 0 +end + +return M diff --git a/lua/img-clip/init.lua b/lua/img-clip/init.lua index 49d9221..1556e2c 100644 --- a/lua/img-clip/init.lua +++ b/lua/img-clip/init.lua @@ -1,6 +1,8 @@ -local util = require("img-clip.util") -local config = require("img-clip.config") local clipboard = require("img-clip.clipboard") +local markup = require("img-clip.markup") +local config = require("img-clip.config") +local util = require("img-clip.util") +local fs = require("img-clip.fs") local M = {} @@ -25,13 +27,13 @@ M.pasteImage = function(opts) end -- get the file path - local filepath = util.get_filepath(opts) + local filepath = fs.get_filepath(opts) if not filepath then return util.error("Could not determine filepath.") end -- mkdir if not exists - local dir_ok = util.mkdirs(filepath) + local dir_ok = fs.mkdirs(filepath) if not dir_ok then return util.error("Could not create directories.") end @@ -43,7 +45,7 @@ M.pasteImage = function(opts) end -- get the markup for the image - local markup_ok = util.insert_markup(filepath, opts) + local markup_ok = markup.insert_markup(filepath, opts) if not markup_ok then return util.error("Could not insert markup code.") end diff --git a/lua/img-clip/markup.lua b/lua/img-clip/markup.lua new file mode 100644 index 0000000..0a0b657 --- /dev/null +++ b/lua/img-clip/markup.lua @@ -0,0 +1,65 @@ +local fs = require("img-clip.fs") +local config = require("img-clip.config") + +local M = {} + +function M.split_lines(template) + local lines = vim.split(template, "\n") + + if lines[1] and lines[1]:match("^%s*$") then + table.remove(lines, 1) + end + + if lines[#lines] and lines[#lines]:match("^%s*$") then + table.remove(lines) + end + + return lines +end + +function M.get_new_row(row, lines) + for i, line in ipairs(lines) do + if line:match("$CURSOR") then + return row + i, line, i + end + end + + return row + #lines, lines[#lines], #lines +end + +function M.get_new_col(line) + local cursor_pos = line:find("$CURSOR") + if cursor_pos then + return cursor_pos - 1 + end + + return string.len(line) - 1 +end + +function M.insert_markup(filepath, opts) + local template = config.get_option("template", opts) + if not template then + return false + end + + local filename = fs.get_filename_from_filepath(filepath) + + template = template:gsub("$FILEPATH", filepath) + template = template:gsub("$FILENAME", filename) + local lines = M.split_lines(template) + + local cur_pos = vim.api.nvim_win_get_cursor(0) + local cur_row = cur_pos[1] + + local new_row, line, index = M.get_new_row(cur_row, lines) + local new_col = M.get_new_col(line) + + lines[index] = line:gsub("$CURSOR", "") + + vim.api.nvim_put(lines, "l", true, true) + vim.api.nvim_win_set_cursor(0, { new_row, new_col }) + + return true +end + +return M diff --git a/lua/img-clip/util.lua b/lua/img-clip/util.lua index b3d17d8..28d8879 100644 --- a/lua/img-clip/util.lua +++ b/lua/img-clip/util.lua @@ -47,86 +47,6 @@ M.input = function(args) return output end -M.add_file_ext = function(str, ext) - local path_separator = package.config:sub(1, 1) - local str_without_ext = str:gsub("%.[^" .. path_separator .. "]-$", "") - return str_without_ext .. "." .. ext -end - -M.get_filename_from_filepath = function(filepath) - local path_separator = package.config:sub(1, 1) - local filename = filepath:match("([^" .. path_separator .. "]+)$") - return filename -end - -M.get_dir_path_from_filepath = function(filepath) - local path_separator = package.config:sub(1, 1) - local dir_path = filepath:match("(.*" .. path_separator .. ").*") - return dir_path -end - -M.get_filepath = function(opts) - local path_separator = package.config:sub(1, 1) - - local config_dir_path = config.get_option("dir_path", opts) - local config_filename = os.date(config.get_option("filename", opts)) - - local dir_path - - if config.get_option("absolute_path", opts) then - local cwd = vim.fn.getcwd() - dir_path = vim.fn.resolve(cwd .. path_separator .. config_dir_path) - else - dir_path = vim.fn.resolve(config_dir_path) - end - - local filepath - if config.get_option("prompt_for_filename", opts) then - if config.get_option("include_filepath_in_prompt", opts) then - local default_filepath = dir_path .. path_separator - local input_filepath = M.input({ - prompt = "Filepath: ", - default = default_filepath, - completion = "file", - }) - if input_filepath ~= "" and input_filepath ~= default_filepath then - filepath = vim.fn.resolve(input_filepath) - end - else - local input_filename = M.input({ prompt = "Filename: ", completion = "file" }) - if input_filename ~= "" then - filepath = vim.fn.resolve(dir_path .. path_separator .. input_filename) - end - end - end - - if not filepath then - filepath = vim.fn.resolve(dir_path .. path_separator .. config_filename) - end - - filepath = M.add_file_ext(filepath, "png") - return filepath -end - -M.mkdirs = function(filepath) - local is_windows = M.has("win32" or M.has("wsl")) - - local dir_path = M.get_dir_path_from_filepath(filepath) - if not dir_path then - return - end -- if no directory in path, return - - local command - if is_windows then - command = string.format('mkdir "%s"', dir_path) - else - command = string.format('mkdir -p "%s"', dir_path) - end - - local exit_code = os.execute(command) - return exit_code == 0 -end - M.split_lines = function(template) local lines = vim.split(template, "\n") @@ -166,7 +86,7 @@ M.insert_markup = function(filepath, opts) return false end - local filename = M.get_filename_from_filepath(filepath) + local filename = fs.get_filename_from_filepath(filepath) template = template:gsub("$FILEPATH", filepath) template = template:gsub("$FILENAME", filename) diff --git a/tests/util_spec.lua b/tests/fs_spec.lua similarity index 61% rename from tests/util_spec.lua rename to tests/fs_spec.lua index 5cb122c..df85f4e 100644 --- a/tests/util_spec.lua +++ b/tests/fs_spec.lua @@ -1,16 +1,16 @@ -local util = require("img-clip.util") +local fs = require("img-clip.fs") -describe("util", function() +describe("fs", function() it("can get the directory path from the file path", function() local path_separator = package.config:sub(1, 1) local simple_filepath = "path" .. path_separator .. "file.txt" local expected_simple_dirpath = "path" .. path_separator - assert.equals(expected_simple_dirpath, util.get_dir_path_from_filepath(simple_filepath)) + assert.equals(expected_simple_dirpath, fs.get_dir_path_from_filepath(simple_filepath)) local full_filepath = "path" .. path_separator .. "to" .. path_separator .. "file.txt" local expected_full_dirpath = "path" .. path_separator .. "to" .. path_separator - assert.equals(expected_full_dirpath, util.get_dir_path_from_filepath(full_filepath)) + assert.equals(expected_full_dirpath, fs.get_dir_path_from_filepath(full_filepath)) local nested_filepath = "path" .. path_separator @@ -20,15 +20,15 @@ describe("util", function() .. path_separator .. "file.txt" local expected_nested_dirpath = "path" .. path_separator .. "to" .. path_separator .. "nested" .. path_separator - assert.equals(expected_nested_dirpath, util.get_dir_path_from_filepath(nested_filepath)) + assert.equals(expected_nested_dirpath, fs.get_dir_path_from_filepath(nested_filepath)) local no_ext_filepath = "path" .. path_separator .. "to" .. path_separator .. "file" local expected_no_ext_dirpath = "path" .. path_separator .. "to" .. path_separator - assert.equals(expected_no_ext_dirpath, util.get_dir_path_from_filepath(no_ext_filepath)) + assert.equals(expected_no_ext_dirpath, fs.get_dir_path_from_filepath(no_ext_filepath)) local space_in_path = "path" .. path_separator .. "to space" .. path_separator .. "my file.txt" local expected_space_in_dirpath = "path" .. path_separator .. "to space" .. path_separator - assert.equals(expected_space_in_dirpath, util.get_dir_path_from_filepath(space_in_path)) + assert.equals(expected_space_in_dirpath, fs.get_dir_path_from_filepath(space_in_path)) end) it("can extract the filename from a file path", function() @@ -36,11 +36,11 @@ describe("util", function() local simple_filepath = "file.txt" local expected_simple_filename = "file.txt" - assert.equals(expected_simple_filename, util.get_filename_from_filepath(simple_filepath)) + assert.equals(expected_simple_filename, fs.get_filename_from_filepath(simple_filepath)) local full_filepath = "path" .. path_separator .. "to" .. path_separator .. "file.txt" local expected_full_filename = "file.txt" - assert.equals(expected_full_filename, util.get_filename_from_filepath(full_filepath)) + assert.equals(expected_full_filename, fs.get_filename_from_filepath(full_filepath)) local nested_filepath = "path" .. path_separator @@ -50,24 +50,24 @@ describe("util", function() .. path_separator .. "file.txt" local expected_nested_filename = "file.txt" - assert.equals(expected_nested_filename, util.get_filename_from_filepath(nested_filepath)) + assert.equals(expected_nested_filename, fs.get_filename_from_filepath(nested_filepath)) local no_ext_filepath = "path" .. path_separator .. "to" .. path_separator .. "file" local expected_no_ext_filename = "file" - assert.equals(expected_no_ext_filename, util.get_filename_from_filepath(no_ext_filepath)) + assert.equals(expected_no_ext_filename, fs.get_filename_from_filepath(no_ext_filepath)) local space_in_filename = "path" .. path_separator .. "to" .. path_separator .. "my file.txt" local expected_space_in_filename = "my file.txt" - assert.equals(expected_space_in_filename, util.get_filename_from_filepath(space_in_filename)) + assert.equals(expected_space_in_filename, fs.get_filename_from_filepath(space_in_filename)) end) it("can add a file extension to a filename", function() - assert.equals("file.png", util.add_file_ext("file", "png")) - assert.equals("file.png", util.add_file_ext("file.png", "png")) - assert.equals("file.png", util.add_file_ext("file.jpg", "png")) - assert.equals("file.png", util.add_file_ext("file.hello.world", "png")) - assert.equals("file.jpg", util.add_file_ext("file", "jpg")) - assert.equals("my file.png", util.add_file_ext("my file", "png")) + assert.equals("file.png", fs.add_file_ext("file", "png")) + assert.equals("file.png", fs.add_file_ext("file.png", "png")) + assert.equals("file.png", fs.add_file_ext("file.jpg", "png")) + assert.equals("file.png", fs.add_file_ext("file.hello.world", "png")) + assert.equals("file.jpg", fs.add_file_ext("file", "jpg")) + assert.equals("my file.png", fs.add_file_ext("my file", "png")) end) it("can add a file extension to a filepath", function() @@ -75,11 +75,11 @@ describe("util", function() local filepath_no_ext = "path" .. path_separator .. "to" .. path_separator .. "file" local expected_filepath_no_ext = filepath_no_ext .. ".png" - assert.equals(expected_filepath_no_ext, util.add_file_ext(filepath_no_ext, "png")) + assert.equals(expected_filepath_no_ext, fs.add_file_ext(filepath_no_ext, "png")) local filepath_with_ext = "path" .. path_separator .. "to" .. path_separator .. "file.jpg" local expected_filepath_with_ext = "path" .. path_separator .. "to" .. path_separator .. "file.png" - assert.equals(expected_filepath_with_ext, util.add_file_ext(filepath_with_ext, "png")) + assert.equals(expected_filepath_with_ext, fs.add_file_ext(filepath_with_ext, "png")) local nested_dir_filepath = "path" .. path_separator @@ -89,14 +89,14 @@ describe("util", function() .. path_separator .. "file" local expected_nested_dir_filepath = nested_dir_filepath .. ".png" - assert.equals(expected_nested_dir_filepath, util.add_file_ext(nested_dir_filepath, "png")) + assert.equals(expected_nested_dir_filepath, fs.add_file_ext(nested_dir_filepath, "png")) local dot_in_dir_filepath = "path" .. path_separator .. "to.version" .. path_separator .. "file" local expected_dot_in_dir_filepath = dot_in_dir_filepath .. ".png" - assert.equals(expected_dot_in_dir_filepath, util.add_file_ext(dot_in_dir_filepath, "png")) + assert.equals(expected_dot_in_dir_filepath, fs.add_file_ext(dot_in_dir_filepath, "png")) local filepath_with_space = "path" .. path_separator .. "to" .. path_separator .. "my file" local expected_filepath_with_space = filepath_with_space .. ".png" - assert.equals(expected_filepath_with_space, util.add_file_ext(filepath_with_space, "png")) + assert.equals(expected_filepath_with_space, fs.add_file_ext(filepath_with_space, "png")) end) end)