Skip to content

Commit 50c7057

Browse files
author
jghauser
committed
refactor!: replace plenary.path with pathlib
1 parent e3f0ec3 commit 50c7057

File tree

6 files changed

+37
-33
lines changed

6 files changed

+37
-33
lines changed

lua/papis/completion/source.lua

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
-- The cmp source.
66
--
77

8-
local Path = require("plenary.path")
8+
local Path = require("pathlib")
99
local ts = vim.treesitter
1010
local api = vim.api
1111

@@ -51,9 +51,8 @@ end
5151
---@return boolean #True if info_name file, false otherwise
5252
function M:is_available()
5353
local is_available = false
54-
local current_filepath = Path:new((api.nvim_buf_get_name(0)))
55-
local split_path = current_filepath:_split()
56-
local filename = current_filepath:_split()[#split_path]
54+
local current_filepath = Path(api.nvim_buf_get_name(0))
55+
local filename = current_filepath:basename()
5756

5857
local info_name = db.config:get_value({ id = 1 }, "info_name")
5958
if filename == info_name then

lua/papis/fs-watcher.lua

+10-8
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77
-- Adapted from: https://github.com/rktjmp/fwatch.nvim
88
--
99

10-
local Path = require("plenary.path")
11-
local Scan = require("plenary.scandir")
10+
local Path = require("pathlib")
1211

1312
local uv = vim.loop
1413
local fs_stat = uv.fs_stat
@@ -45,15 +44,18 @@ local function do_watch(path, on_event, on_error)
4544
on_event(filename, unwatch_cb)
4645
end
4746
end
48-
uv.fs_event_start(handle, path, {}, event_cb)
47+
uv.fs_event_start(handle, tostring(path), {}, event_cb)
4948
table.insert(handles, handle)
5049
end
5150

5251
---Gets all directories in the library_dir
5352
---@return table #A list of all directories in library_dir
5453
local function get_library_dirs()
55-
local library_dir = Path:new(db.config:get_value({ id = 1 }, "dir")):expand()
56-
local library_dirs = Scan.scan_dir(library_dir, { depth = 1, only_dirs = true })
54+
local library_dir = Path(db.config:get_value({ id = 1 }, "dir"))
55+
local library_dirs = {}
56+
for path in library_dir:fs_iterdir() do
57+
table.insert(library_dirs, path)
58+
end
5759
return library_dirs
5860
end
5961

@@ -75,7 +77,7 @@ local function init_fs_watcher(dir_to_watch, is_library_root)
7577
local do_update = true
7678
if is_library_root then
7779
log.debug("Filesystem event in the library root directory")
78-
entry_dir = Path:new(dir_to_watch, filename)
80+
entry_dir = Path(dir_to_watch, filename)
7981
info_path = entry_dir:joinpath(info_name)
8082
if entry_dir:exists() and entry_dir:is_dir() then
8183
log.debug(string.format("Filesystem event: path '%s' added", entry_dir:absolute()))
@@ -93,7 +95,7 @@ local function init_fs_watcher(dir_to_watch, is_library_root)
9395
end
9496
else
9597
log.debug("Filesystem event in entry directory")
96-
entry_dir = Path:new(dir_to_watch)
98+
entry_dir = Path(dir_to_watch)
9799
info_path = entry_dir:joinpath(info_name)
98100
if info_path:exists() then
99101
-- info file exists, update with new info
@@ -151,7 +153,7 @@ end
151153
---Starts file system watchers for root dir and all entry dirs
152154
local function start_fs_watchers()
153155
log.debug("Set db state to indicate fswatcher is active")
154-
local library_dir = Path:new(db.config:get_value({ id = 1 }, "dir")):expand()
156+
local library_dir = Path(db.config:get_value({ id = 1 }, "dir"))
155157
db.state:set_fw_running(uv.os_getpid())
156158

157159
log.debug("Setting up fswatcher for library root directory")

lua/papis/health.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
-- Implements checkhealth funtionality.
66
--
77

8-
local Path = require("plenary.path")
8+
local Path = require("pathlib")
99

1010
local uv = vim.loop
1111

@@ -22,7 +22,7 @@ local reports = {}
2222
---Creates a report for the sqlite database
2323
reports["sqlite-wrapper"] = function()
2424
local sqlite_is_executable = vim.fn.executable("sqlite3")
25-
local db_exists = Path:new(config["db_path"]):exists()
25+
local db_exists = Path(config["db_path"]):exists()
2626
local db_is_empty = db.data:empty()
2727

2828
health.start("Sqlite database")

lua/papis/papis-storage.lua

+16-12
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
-- Reads out all the Papis yaml files and creates data ready for db
66
--
77

8-
local Path = require("plenary.path")
9-
local Scan = require("plenary.scandir")
8+
local Path = require("pathlib")
109

1110
local fs_stat = vim.loop.fs_stat
1211

@@ -85,8 +84,8 @@ end
8584
local function read_yaml(path)
8685
log.trace("Reading path: " .. path)
8786
local entry
88-
local filepath = Path:new(path)
89-
local handler = io.popen(yq_bin .. ' -oj "' .. filepath:absolute() .. '" 2>/dev/null')
87+
local filepath = Path(path)
88+
local handler = io.popen(yq_bin .. ' -oj "' .. tostring(filepath) .. '" 2>/dev/null')
9089
if handler then
9190
local as_json = handler:read("*all")
9291
handler:close()
@@ -124,7 +123,7 @@ local function make_full_paths(filenames, path)
124123

125124
local full_paths = {}
126125
for _, filename in ipairs(filenames) do
127-
local full_path = Path:new(path, filename):expand()
126+
local full_path = tostring(Path(path, filename))
128127
table.insert(full_paths, full_path)
129128
end
130129
return full_paths
@@ -142,15 +141,20 @@ end
142141
---@param paths? table #A list with paths of papis entries
143142
---@return table #A list of { path = path, mtime = mtime } values
144143
function M.get_metadata(paths)
145-
local library_dir = Path:new(db.config:get_value({ id = 1 }, "dir"))
144+
local library_dir = Path(db.config:get_value({ id = 1 }, "dir"))
146145
local info_name = db.config:get_value({ id = 1 }, "info_name")
147-
paths = paths or Scan.scan_dir(library_dir:expand(), { depth = 2, search_pattern = info_name })
146+
if not paths then
147+
paths = {}
148+
for path in library_dir:fs_iterdir() do
149+
if path:basename() == info_name then
150+
table.insert(paths, path)
151+
end
152+
end
153+
end
148154
local metadata = {}
149155
for _, path in ipairs(paths) do
150-
local mtime = fs_stat(path).mtime.sec
151-
-- path = Path:new(path)
152-
-- path = path:parent():absolute()
153-
table.insert(metadata, { path = path, mtime = mtime })
156+
local mtime = fs_stat(tostring(path)).mtime.sec
157+
table.insert(metadata, { path = tostring(path), mtime = mtime })
154158
end
155159
return metadata
156160
end
@@ -183,7 +187,7 @@ function M.get_data_full(metadata)
183187
entry[key] = ensure_tags_are_tbl(entry[key])
184188
end
185189
if (key == "files") or (key == "notes") then
186-
local entry_path = Path:new(path):parent()
190+
local entry_path = Path(path):parent()
187191
entry[key] = make_full_paths(entry[key], entry_path)
188192
end
189193

lua/papis/sqlite-wrapper.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ end
1515

1616
local sqlite = require("sqlite.db") --- for constructing sql databases
1717

18-
local Path = require("plenary.path")
18+
local Path = require("pathlib")
1919
local config = require("papis.config")
20-
local db_uri = Path:new(config["db_path"])
20+
local db_uri = Path(config["db_path"])
2121
local data_tbl_schema = config["data_tbl_schema"]
2222

2323
if not db_uri:exists() then
@@ -72,7 +72,7 @@ function tbl_methods.update(tbl, where, new_values)
7272
end
7373

7474
local M = sqlite({
75-
uri = db_uri:absolute(),
75+
uri = tostring(db_uri),
7676
opts = { busy_timeout = 30000 },
7777
})
7878

lua/papis/utils.lua

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
local NuiLine = require("nui.line")
99
local NuiPopup = require("nui.popup")
1010
local nuiEvent = require("nui.utils.autocmd").event
11-
local Path = require("plenary.path")
1211
local strdisplaywidth = require("plenary.strings").strdisplaywidth
1312
local job = require("plenary.job")
13+
local Path = require("pathlib")
1414

1515
local new_timer = vim.loop.new_timer
1616
local os_name = vim.loop.os_uname()
@@ -90,8 +90,7 @@ function M.get_filenames(full_paths)
9090
local filenames = {}
9191
if full_paths then
9292
for _, full_path in ipairs(full_paths) do
93-
local filename = Path:new(full_path):_split()
94-
filename = filename[#filename]
93+
local filename = Path(full_path):basename()
9594
table.insert(filenames, filename)
9695
end
9796
end
@@ -141,7 +140,7 @@ function M:do_open_text_file(papis_id, type)
141140
end
142141
log.debug("Opening a text file")
143142
local entry = db.data:get({ papis_id = papis_id }, { "notes", "id" })[1]
144-
local info_path = Path:new(db.metadata:get_value({ entry = entry["id"] }, "path"))
143+
local info_path = Path(db.metadata:get_value({ entry = entry["id"] }, "path"))
145144
log.debug("Text file in folder: " .. info_path:absolute())
146145
local cmd = ""
147146
if type == "note" then

0 commit comments

Comments
 (0)