Skip to content

Commit 1cd81e4

Browse files
committed
attempt 2
1 parent ed715ef commit 1cd81e4

File tree

4 files changed

+102
-46
lines changed

4 files changed

+102
-46
lines changed

lua/telescope/_extensions/file_browser/actions.lua

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,17 @@ local os_sep = Path.path.sep
5757
fb_actions.create = function(prompt_bufnr)
5858
local current_picker = action_state.get_current_picker(prompt_bufnr)
5959
local finder = current_picker.finder
60-
local file = fb_utils.get_valid_path("Insert the file name: ", finder.path .. os_sep)
60+
local quiet = finder.quiet
61+
local file = fb_utils.get_valid_path("Insert the file name: ", finder.path .. os_sep, { quiet = quiet })
62+
-- local success
6163
if file then
6264
if not fb_utils.is_dir(file.filename) then
6365
file:touch { parents = true }
6466
else
6567
Path:new(file.filename:sub(1, -2)):mkdir { parents = true }
6668
end
6769
current_picker:refresh(finder, { reset_prompt = true, multi = current_picker._multi })
68-
fb_utils.tele_notify(string.format("\n%s created!", file.filename))
70+
fb_utils.action_cmp_msg(string.format("%s created!", file.filename), quiet)
6971
end
7072
end
7173

@@ -152,6 +154,7 @@ fb_actions.rename = function(prompt_bufnr)
152154
local current_picker = action_state.get_current_picker(prompt_bufnr)
153155
local selections = fb_utils.get_selected_files(prompt_bufnr, false)
154156
local parent_dir = Path:new(current_picker.finder.path):parent()
157+
local quiet = current_picker.finder.quiet
155158

156159
if not vim.tbl_isempty(selections) then
157160
batch_rename(prompt_bufnr, selections)
@@ -168,7 +171,7 @@ fb_actions.rename = function(prompt_bufnr)
168171
return
169172
end
170173

171-
local new_path = fb_utils.get_valid_path("Insert a new name: ", old_path:absolute())
174+
local new_path = fb_utils.get_valid_path("Insert a new name: ", old_path:absolute(), { quiet = quiet })
172175
if new_path then
173176
-- rename changes old_name in place
174177
local old_name = old_path:absolute()
@@ -184,7 +187,7 @@ fb_actions.rename = function(prompt_bufnr)
184187
current_picker._multi:drop(entry)
185188
end
186189
current_picker:refresh(current_picker.finder)
187-
fb_utils.tele_notify(string.format("\n%s renamed to %s!", old_name, new_path.filename))
190+
fb_utils.action_cmp_msg(string.format("%s renamed to %s!", old_name, new_path.filename), quiet)
188191
end
189192
end
190193
end
@@ -215,7 +218,7 @@ fb_actions.move = function(prompt_bufnr)
215218
file:rename {
216219
new_name = new_path.filename,
217220
}
218-
fb_utils.tele_notify(string.format("%s has been moved!", filename))
221+
fb_utils.action_cmp_msg(string.format("%s has been moved!", filename), finder.quiet, #selections)
219222
end
220223
end
221224

@@ -229,6 +232,7 @@ end
229232
fb_actions.copy = function(prompt_bufnr)
230233
local current_picker = action_state.get_current_picker(prompt_bufnr)
231234
local finder = current_picker.finder
235+
local quiet = finder.quiet
232236
if finder.files ~= nil and finder.files == false then
233237
fb_utils.tele_notify("Copying files in folder browser mode not supported.", log_levels.WARN)
234238
return
@@ -252,15 +256,19 @@ fb_actions.copy = function(prompt_bufnr)
252256
if file:parent():absolute() == finder.path then
253257
local absolute_path = file:absolute()
254258
fb_utils.tele_notify "Copying existing file or folder within original directory."
255-
destination = fb_utils.get_valid_path("Please provide a new file or folder name: ", absolute_path)
259+
destination = fb_utils.get_valid_path(
260+
"Please provide a new file or folder name: ",
261+
absolute_path,
262+
{ quiet = quiet }
263+
)
256264
end
257265
if destination then
258266
file:copy {
259267
destination = destination,
260268
recursive = true,
261269
parents = true,
262270
}
263-
fb_utils.tele_notify(string.format("%s has been copied!", filename))
271+
fb_utils.action_cmp_msg(string.format("%s has been copied!", filename), quiet, #selections)
264272
end
265273
end
266274

@@ -272,6 +280,7 @@ end
272280
---@param prompt_bufnr number: The prompt bufnr
273281
fb_actions.remove = function(prompt_bufnr)
274282
local current_picker = action_state.get_current_picker(prompt_bufnr)
283+
local quiet = current_picker.finder.quiet
275284
local selections = fb_utils.get_selected_files(prompt_bufnr, true)
276285
if vim.tbl_isempty(selections) then
277286
fb_utils.tele_notify "Nothing currently selected to be removed."
@@ -282,30 +291,27 @@ fb_actions.remove = function(prompt_bufnr)
282291
return sel:absolute()
283292
end, selections)
284293

285-
fb_utils.tele_notify "Following files/folders are going to be deleted:"
294+
-- BUG: printing below completely messes with the y/n 'Operation aborted' & '.. has been removed!' printing
295+
fb_utils.tele_notify "Following files/folders will be remove:"
286296
for _, file in ipairs(filenames) do
287297
fb_utils.tele_notify(" - " .. file)
288298
end
289299

290-
vim.ui.input({ prompt = "[telescope] Remove selected files [y/N]: " }, function(input)
291-
if input and input:lower() == "y" then
292-
vim.notify "\n"
293-
for _, p in ipairs(selections) do
294-
local is_dir = p:is_dir()
295-
p:rm { recursive = is_dir }
296-
-- clean up opened buffers
297-
if not is_dir then
298-
fb_utils.delete_buf(p:absolute())
299-
else
300-
fb_utils.delete_dir_buf(p:absolute())
301-
end
302-
fb_utils.tele_notify(string.format("%s has been removed!", p:absolute()))
300+
local ans = fb_utils.get_answer_yes("Remove selected files", false, { quiet = quiet })
301+
if ans then
302+
for _, p in ipairs(selections) do
303+
local is_dir = p:is_dir()
304+
p:rm { recursive = is_dir }
305+
-- clean up opened buffers
306+
if not is_dir then
307+
fb_utils.delete_buf(p:absolute())
308+
else
309+
fb_utils.delete_dir_buf(p:absolute())
303310
end
304-
current_picker:refresh(current_picker.finder)
305-
else
306-
fb_utils.tele_notify "\nRemoving files aborted!"
311+
fb_utils.action_cmp_msg(string.format("%s has been removed!", p:absolute()), quiet, #selections)
307312
end
308-
end)
313+
current_picker:refresh(current_picker.finder)
314+
end
309315
end
310316

311317
--- Toggle hidden files or folders for |fb_picker.file_browser|.

lua/telescope/_extensions/file_browser/finders.lua

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,19 @@ end
110110

111111
--- Returns a finder that combines |fb_finders.browse_files| and |fb_finders.browse_folders| into a unified finder.
112112
---@param opts table: options to pass to the picker
113-
---@field path string: root dir to file_browse from (default: vim.loop.cwd())
114-
---@field cwd string: root dir (default: vim.loop.cwd())
115-
---@field cwd_to_path bool: folder browser follows `path` of file browser
116-
---@field files boolean: start in file (true) or folder (false) browser (default: true)
113+
---@field path string: dir to browse files from from, `vim.fn.expanded` automatically (default: vim.loop.cwd())
114+
---@field cwd string: dir to browse folders from, `vim.fn.expanded` automatically (default: vim.loop.cwd())
115+
---@field cwd_to_path boolean: whether folder browser is launched from `path` rather than `cwd` (default: false)
117116
---@field grouped boolean: group initial sorting by directories and then files; uses plenary.scandir (default: false)
118-
---@field depth number: file tree depth to display (default: 1)
119-
---@field dir_icon string: change the icon for a directory. (default: )
117+
---@field files boolean: start in file (true) or folder (false) browser (default: true)
118+
---@field add_dirs boolean: whether the file browser shows folders (default: true)
119+
---@field depth number: file tree depth to display, `false` for unlimited depth (default: 1)
120+
---@field dir_icon string: change the icon for a directory (default: )
120121
---@field hidden boolean: determines whether to show hidden files or not (default: false)
121122
---@field respect_gitignore boolean: induces slow-down w/ plenary finder (default: false, true if `fd` available)
123+
---@field browse_files function: custom override for the file browser (default: |fb_finders.browse_files|)
124+
---@field browse_folders function: custom override for the folder browser (default: |fb_finders.browse_folders|)
125+
---@field quiet bool: suppress action completion messages (default: false)
122126
fb_finders.finder = function(opts)
123127
opts = opts or {}
124128
-- cache entries such that multi selections are maintained across {file, folder}_browsers
@@ -134,6 +138,7 @@ fb_finders.finder = function(opts)
134138
respect_gitignore = vim.F.if_nil(opts.respect_gitignore, has_fd),
135139
files = vim.F.if_nil(opts.files, true), -- file or folders mode
136140
grouped = vim.F.if_nil(opts.grouped, false),
141+
quiet = vim.F.if_nil(opts.quiet, false),
137142
-- ensure we forward make_entry opts adequately
138143
entry_maker = vim.F.if_nil(opts.entry_maker, function(local_opts)
139144
return fb_make_entry(vim.tbl_extend("force", opts, local_opts))

lua/telescope/_extensions/file_browser/picker.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ local fb_picker = {}
5454
---@field respect_gitignore boolean: induces slow-down w/ plenary finder (default: false, true if `fd` available)
5555
---@field browse_files function: custom override for the file browser (default: |fb_finders.browse_files|)
5656
---@field browse_folders function: custom override for the folder browser (default: |fb_finders.browse_folders|)
57+
---@field quiet bool: suppress action completion messages (default: false)
5758
fb_picker.file_browser = function(opts)
5859
opts = opts or {}
5960

@@ -63,6 +64,7 @@ fb_picker.file_browser = function(opts)
6364
opts.cwd = opts.cwd and vim.fn.expand(opts.cwd) or cwd
6465
opts.path = opts.path and vim.fn.expand(opts.path) or opts.cwd
6566
opts.files = vim.F.if_nil(opts.files, true)
67+
opts.quiet = vim.F.if_nil(opts.quiet, false)
6668
pickers.new(opts, {
6769
prompt_title = opts.files and "File Browser" or "Folder Browser",
6870
results_title = opts.files and Path:new(opts.path):make_relative(cwd) .. os_sep or "Results",

lua/telescope/_extensions/file_browser/utils.lua

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -156,37 +156,80 @@ end
156156

157157
-- Move this and above functions into telescope core?
158158
fb_utils.tele_notify = function(msg, log_level, opts)
159-
while msg:match "^\n" do
160-
msg = msg:sub(2, #msg)
161-
vim.notify "\n"
162-
end
159+
opts = opts or {}
160+
opts.title = "telescope"
163161
vim.notify(add_tele_prefix(msg), log_level, opts)
164-
while msg:match "\n$" do
165-
msg = msg:sub(1, #msg - 1)
166-
vim.notify "\n"
167-
end
168162
end
169163

170-
fb_utils.get_valid_path = function(prompt, default, check_exist)
171-
check_exist = vim.F.if_nil(check_exist, true)
164+
---@param opts table: options to pass to the path getter
165+
---@field check_exist boolean: check if path already exists
166+
---@field quiet boolean: suppress 'Operation aborted' message
167+
fb_utils.get_valid_path = function(prompt, default, opts)
168+
opts = opts or {}
169+
opts.check_exist = vim.F.if_nil(opts.check_exist, true)
170+
opts.quiet = vim.F.if_nil(opts.quiet, false)
171+
172172
local path
173173
vim.ui.input({ prompt = add_tele_prefix(prompt), default = default }, function(input)
174174
input = input and input:match "^%s*(.-)%s*$" or nil
175-
if not input then
176-
fb_utils.tele_notify("\nOperation aborted!", vim.log.levels.WARN)
175+
if not input and not opts.quiet then
176+
-- BUG: if default is completely deleted, msg prints on the same line
177+
fb_utils.tele_notify("Operation aborted!", vim.log.levels.WARN)
177178
return
178179
elseif input == "" then
179-
fb_utils.tele_notify("\nPlease provide valid path input!", vim.log.levels.WARN)
180+
fb_utils.clear_cmd()
181+
fb_utils.tele_notify("Please provide valid path input!", vim.log.levels.WARN)
180182
return
181183
end
182184

183185
path = Path:new(input)
184-
if check_exist and path:exists() then
185-
fb_utils.tele_notify(string.format("\n%s already exists! Skipping.", path.filename), vim.log.levels.WARN)
186+
if opts.check_exist and path:exists() then
187+
fb_utils.clear_cmd()
188+
fb_utils.tele_notify(string.format("%s already exists! Skipping.", path.filename), vim.log.levels.WARN)
186189
path = nil
187190
end
188191
end)
189192
return path
190193
end
191194

195+
---@param opts table: options to pass to the path getter
196+
---@field quiet boolean: suppress 'Operation aborted' message
197+
fb_utils.get_answer_yes = function(prompt, default_yes, opts)
198+
opts = opts or {}
199+
opts.quiet = vim.F.if_nil(opts.quiet, false)
200+
default_yes = vim.F.if_nil(default_yes, true)
201+
prompt = add_tele_prefix(prompt) .. (default_yes and " [Y/n]:" or " [y/N]:")
202+
203+
local answer
204+
vim.ui.input({ prompt = prompt, default = " " }, function(input)
205+
input = input and input:match "^%s*(.-)%s*$" or nil
206+
answer = input and input:lower() == "y"
207+
if not input and not opts.quiet then
208+
fb_utils.tele_notify("Operation aborted!", vim.log.levels.WARN)
209+
elseif not answer then
210+
fb_utils.clear_cmd()
211+
fb_utils.tele_notify("Operation aborted!", vim.log.levels.WARN)
212+
end
213+
end)
214+
return answer
215+
end
216+
217+
fb_utils.clear_cmd = function()
218+
-- barely works and not even always
219+
-- also tried `echon ''`
220+
vim.api.nvim_command "norm :esc<CR>"
221+
end
222+
223+
fb_utils.action_cmp_msg = function(msg, quiet, count)
224+
quiet = vim.F.if_nil(quiet, false)
225+
count = vim.F.if_nil(count, 1)
226+
227+
if not quiet then
228+
if count == 1 then -- don't clear for multi file move/remove
229+
fb_utils.clear_cmd()
230+
end
231+
fb_utils.tele_notify(msg)
232+
end
233+
end
234+
192235
return fb_utils

0 commit comments

Comments
 (0)