-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract functionality from util.lua into fs.lua and markup.lua
- Loading branch information
1 parent
c03807c
commit 67c2242
Showing
5 changed files
with
179 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.