Skip to content

Commit

Permalink
refactor: extract functionality from util.lua into fs.lua and markup.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
HakonHarnes committed Dec 7, 2023
1 parent c03807c commit 67c2242
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 109 deletions.
83 changes: 83 additions & 0 deletions lua/img-clip/fs.lua
Original file line number Diff line number Diff line change
@@ -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
12 changes: 7 additions & 5 deletions lua/img-clip/init.lua
Original file line number Diff line number Diff line change
@@ -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 = {}

Expand All @@ -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
Expand All @@ -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
Expand Down
65 changes: 65 additions & 0 deletions lua/img-clip/markup.lua
Original file line number Diff line number Diff line change
@@ -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
82 changes: 1 addition & 81 deletions lua/img-clip/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 67c2242

Please sign in to comment.