aboutsummaryrefslogtreecommitdiff
path: root/configs/nvim/lua/crupest/system
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2024-09-07 02:59:28 +0800
committercrupest <crupest@outlook.com>2024-09-07 02:59:28 +0800
commitb109ae78fb51c8f288e18c28cfd711f0192b20c8 (patch)
treef1c05e14d3e4a2955e49121ddf8341e7fc8e951b /configs/nvim/lua/crupest/system
parent669048c81d4974adc4d795cff158ab8551218bd9 (diff)
downloadcrupest-b109ae78fb51c8f288e18c28cfd711f0192b20c8.tar.gz
crupest-b109ae78fb51c8f288e18c28cfd711f0192b20c8.tar.bz2
crupest-b109ae78fb51c8f288e18c28cfd711f0192b20c8.zip
config(nvim): refactor a lot.
Diffstat (limited to 'configs/nvim/lua/crupest/system')
-rw-r--r--configs/nvim/lua/crupest/system/find.lua153
-rw-r--r--configs/nvim/lua/crupest/system/fs.lua101
2 files changed, 0 insertions, 254 deletions
diff --git a/configs/nvim/lua/crupest/system/find.lua b/configs/nvim/lua/crupest/system/find.lua
deleted file mode 100644
index 0876622..0000000
--- a/configs/nvim/lua/crupest/system/find.lua
+++ /dev/null
@@ -1,153 +0,0 @@
-local system = require("crupest.system")
-local fs = require("crupest.system.fs");
-
-local win_exe_exts = { "exe", "CMD", "cmd", "ps1" }
-
-
-local function get_exe(path)
- if system.is_win then
- for _, ext in ipairs(win_exe_exts) do
- if string.find(path, "%." .. ext .. "$") and fs.isfile(path) then
- return path
- end
- end
- for _, ext in ipairs(win_exe_exts) do
- local p = path .. "." .. ext
- if fs.isfile(p) then return p end
- end
- return nil
- end
-
- if vim.fn.executable(path) ~= 0 then
- return path
- end
-
- return nil
-end
-
-local function find_global_exe(name)
- if vim.fn.executable(name) ~= 0 then
- return name
- end
-
- return nil
-end
-
-local function first_exe(paths)
- for _, v in ipairs(paths) do
- local exe = get_exe(v)
- if exe then return exe end
- end
-
- return nil
-end
-
-local function find_file_or_directory(path, name)
- return fs.walk_up(path, function(current_path)
- local p = current_path .. "/" .. name
- if fs.isdir(p) then
- return p, "directory"
- elseif fs.isfile(p) then
- return p, "file"
- end
- return nil
- end)
-end
-
-local function find_file(path, name)
- return fs.walk_up(path, function(current_path)
- local p = current_path .. "/" .. name
- if fs.isfile(p) then
- return p
- end
- return nil
- end)
-end
-
-local function find_files_or_directories(path, names)
- for _, name in ipairs(names) do
- local p, type = find_file_or_directory(path, name)
- if p then return p, type end
- end
- return nil
-end
-
-local function find_files(path, names)
- for _, name in ipairs(names) do
- local p = find_file(path, name)
- if p then return p end
- end
- return nil
-end
-
-local function find_node_modules(path)
- return fs.walk_up(path, function(current_path)
- local node_modules_path = current_path .. "/node_modules"
- if fs.isdir(node_modules_path) then
- return node_modules_path
- end
- return nil
- end)
-end
-
-local function find_npm_exe(path, exe)
- local node_modules_path = find_node_modules(path)
- if not node_modules_path then return nil end
- local try_exe_path = node_modules_path .. "/.bin/" .. exe
- local exe_path = get_exe(try_exe_path)
- if exe_path then return exe_path end
- return nil
-end
-
-local function find_exe(path, exe, places)
- for _, place in ipairs(places) do
- if place == "npm" then
- local r = find_npm_exe(path, exe)
- if r then return r end
- end
- if place == "global" then
- local r = find_global_exe(exe)
- if r then return r end
- end
- end
- return nil
-end
-
-local function find_exe_for_buf(buf, opts)
- local r = {}
- r.name = opts.name
- r.file = vim.api.nvim_buf_get_name(buf)
- r.filetype = vim.api.nvim_buf_get_option(buf, "filetype")
- r.exe_name = opts.name
- r.exe_places = opts.exe_places or { "global" }
-
- if opts.config_files then
- r.config_file = find_files(r.file, opts.config_files)
- if r.config_file == nil then return nil end
- end
-
- if opts.filetypes then
- if not require("crupest.table").includes(opts.filetypes, r.filetype) then
- return nil
- end
- end
-
- r.exe_path = find_exe(r.file, r.exe_name, r.exe_places)
- if r.exe_path == nil then return nil end
-
- return r
-end
-
-return {
- get_exe = get_exe,
- find_global_exe = find_global_exe,
- first_exe = first_exe,
- find_file_or_directory = find_file_or_directory,
- find_files_or_directories = find_files_or_directories,
- find_file = find_file,
- find_files = find_files,
- find_node_modules = find_node_modules,
- find_npm_exe = find_npm_exe,
- find_exe = find_exe,
- find_exe_for_buf = find_exe_for_buf,
-}
diff --git a/configs/nvim/lua/crupest/system/fs.lua b/configs/nvim/lua/crupest/system/fs.lua
deleted file mode 100644
index d34c2f6..0000000
--- a/configs/nvim/lua/crupest/system/fs.lua
+++ /dev/null
@@ -1,101 +0,0 @@
-local function clean_path(path)
- if path == "/" then return path end
- path = string.gsub(path, "[/\\]+", "/")
- if string.sub(path, string.len(path)) == '/' then
- path = string.sub(path, 1, string.len(path) - 1)
- end
- return path
-end
-
-local function full_path(name)
- local path = vim.fn.fnamemodify(name, ":p")
- return clean_path(path)
-end
-
-local function escape_space(str)
- return (string.gsub(str, " ", "\\ "))
-end
-
-local function path_get_dir(path)
- return full_path(vim.fn.fnamemodify(clean_path(path), ":h"))
-end
-
-local function walk_up(path, func)
- local current_path = full_path(path)
- while true do
- local result = func(current_path)
- if result ~= nil then
- return result
- end
- local new_path = path_get_dir(current_path)
- if new_path == current_path then
- break
- end
- current_path = new_path
- end
- return nil
-end
-
-local function exist(path)
- return vim.uv.fs_stat(path)
-end
-
-local function isfile(path)
- local s = vim.uv.fs_stat(path)
- if not s then return false end
- return s.type == "file"
-end
-
-local function isdir(path)
- local s = vim.uv.fs_stat(path)
- if not s then return false end
- return s.type == "directory"
-end
-
-local function mkdir(dir)
- local parents = {}
-
- walk_up(dir, function(p)
- table.insert(parents, 1, p)
- end)
-
- for _, v in ipairs(parents) do
- if exist(v) and not isdir(v) then
- vim.notify(v .. " is not a dir. Can't make dir " .. dir, vim.log.levels.ERROR)
- return
- end
- if not exist(v) then
- vim.notify("Creating dir " .. v)
- assert(vim.uv.fs_mkdir(v, 504)) -- mode = 0770
- end
- end
-end
-
-local function copy(old, new)
- mkdir(path_get_dir(new))
- assert(vim.uv.fs_copyfile(old, new))
-end
-
-local function remove(path)
- assert(vim.uv.fs_unlink(path))
-end
-
-local function move(old, new)
- mkdir(path_get_dir(new))
- assert(vim.uv.fs_rename(old, new))
-end
-
-return {
- clean_path = clean_path,
- full_path = full_path,
- escape_space = escape_space,
- path_get_dir = path_get_dir,
- walk_up = walk_up,
- exist = exist,
- isfile = isfile,
- isdir = isdir,
- mkdir = mkdir,
- copy = copy,
- remove = remove,
- move = move
-}