Skip to content

Commit

Permalink
refactor: switch to vim api for file and path manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
HakonHarnes committed Dec 8, 2023
1 parent f18d7d9 commit 53ef988
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 129 deletions.
90 changes: 45 additions & 45 deletions lua/img-clip/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,79 +5,79 @@ 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
M.normalize_path = function(path)
return vim.fn.simplify(path):gsub(M.sep .. "$", "") .. M.sep
end

M.get_dir_path_from_filepath = function(filepath)
local dir_path = filepath:match("(.*" .. M.sep .. ").*")
return dir_path
M.add_file_ext = function(str, ext)
str = vim.fn.fnamemodify(str, ":r")
return str .. "." .. ext
end

M.get_filepath = function(opts)
M.get_file_path = function(ext, opts)
local config_dir_path = config.get_option("dir_path", opts)
local config_filename = os.date(config.get_option("filename", opts))

local dir_path

local dir_path = config_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)
dir_path = vim.fn.fnamemodify(dir_path, ":p")
end

local filepath
dir_path = M.normalize_path(dir_path)

local file_path
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,
local input_file_path = util.input({
prompt = "File path: ",
default = dir_path,
completion = "file",
})
if input_filepath ~= "" and input_filepath ~= default_filepath then
filepath = vim.fn.resolve(input_filepath)
if input_file_path and input_file_path ~= "" and input_file_path ~= dir_path then
file_path = input_file_path
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)
local input_filename = util.input({
prompt = "Filename: ",
completion = "file",
})
if input_filename and input_filename ~= "" then
file_path = dir_path .. input_filename
end
end
end

if not filepath then
filepath = vim.fn.resolve(dir_path .. M.sep .. config_filename)
-- use default path and filename if none was provided
if not file_path then
file_path = dir_path .. config_filename
end

filepath = M.add_file_ext(filepath, "png")
return filepath
file_path = M.add_file_ext(file_path, ext)
return file_path
end

M.mkdirs = function(filepath)
local is_windows = util.has("win32" or util.has("wsl"))
M.mkdirp = function(dir, mode)
dir = vim.fn.resolve(dir)
mode = mode or 493

local mod = ""
local path = dir

local dir_path = M.get_dir_path_from_filepath(filepath)
if not dir_path then
return
end -- if no directory in path, return
while vim.fn.isdirectory(path) == 0 do
mod = mod .. ":h"
path = vim.fn.fnamemodify(dir, mod)
end

while mod ~= "" do
mod = mod:sub(3)
path = vim.fn.fnamemodify(dir, mod)

local command
if is_windows then
command = string.format('mkdir "%s"', dir_path)
else
command = string.format('mkdir -p "%s"', dir_path)
if not vim.loop.fs_mkdir(path, mode) then
return false
end
end

local exit_code = os.execute(command)
return exit_code == 0
return true
end

return M
11 changes: 6 additions & 5 deletions lua/img-clip/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,26 @@ M.pasteImage = function(opts)
end

-- get the file path
local filepath = fs.get_filepath(opts)
if not filepath then
local file_path = fs.get_file_path("png", opts)
if not file_path then
return util.error("Could not determine filepath.")
end

-- mkdir if not exists
local dir_ok = fs.mkdirs(filepath)
local dir_path = vim.fn.fnamemodify(file_path, ":h")
local dir_ok = fs.mkdirp(dir_path)
if not dir_ok then
return util.error("Could not create directories.")
end

-- save image to specified file path
local save_ok = clipboard.save_clipboard_image(clip_cmd, filepath)
local save_ok = clipboard.save_clipboard_image(clip_cmd, file_path)
if not save_ok then
return util.error("Could not save image to disk.")
end

-- get the markup for the image
local markup_ok = markup.insert_markup(filepath, opts)
local markup_ok = markup.insert_markup(file_path, opts)
if not markup_ok then
return util.error("Could not insert markup code.")
end
Expand Down
3 changes: 1 addition & 2 deletions lua/img-clip/markup.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local fs = require("img-clip.fs")
local config = require("img-clip.config")

local M = {}
Expand Down Expand Up @@ -42,7 +41,7 @@ function M.insert_markup(filepath, opts)
return false
end

local filename = fs.get_filename_from_filepath(filepath)
local filename = vim.fn.fnamemodify(filepath, ":t")

template = template:gsub("$FILEPATH", filepath)
template = template:gsub("$FILENAME", filename)
Expand Down
6 changes: 5 additions & 1 deletion lua/img-clip/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,14 @@ M.debug = function(msg)
end

M.input = function(args)
local _, output = pcall(function()
local completed, output = pcall(function()
return vim.fn.input(args)
end)

if not completed then
return nil
end

return output
end

Expand Down
Loading

0 comments on commit 53ef988

Please sign in to comment.