feat: improve UX with vim.notify (#1763)
* fix(notify): don't report request on new line * ref(notify): update message format * ref(msgs): always quote values + decrease duplication * fix(ci): undefined variables * ref(actions): temporary silent actions.__index errors * cleanup * revert: panic effort, we continue to use error for those Co-authored-by: Simon Hauser <Simon-Hauser@outlook.de>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
local conf = require("telescope.config").values
|
||||
local Path = require "plenary.path"
|
||||
local utils = require "telescope.utils"
|
||||
|
||||
local uv = vim.loop
|
||||
|
||||
@@ -111,10 +112,10 @@ end
|
||||
---@return string: the next history item
|
||||
function histories.History:get_next(line, picker)
|
||||
if not self.enabled then
|
||||
print(
|
||||
"You are cycling to next the history item but history is disabled.",
|
||||
"Read ':help telescope.defaults.history'"
|
||||
)
|
||||
utils.notify("History:get_next", {
|
||||
msg = "You are cycling to next the history item but history is disabled. Read ':help telescope.defaults.history'",
|
||||
level = "WARN",
|
||||
})
|
||||
return false
|
||||
end
|
||||
if self._pre_get then
|
||||
@@ -136,10 +137,10 @@ end
|
||||
---@return string: the previous history item
|
||||
function histories.History:get_prev(line, picker)
|
||||
if not self.enabled then
|
||||
print(
|
||||
"You are cycling to previous the history item but history is disabled.",
|
||||
"Read ':help telescope.defaults.history'"
|
||||
)
|
||||
utils.notify("History:get_prev", {
|
||||
msg = "You are cycling to next the history item but history is disabled. Read ':help telescope.defaults.history'",
|
||||
level = "WARN",
|
||||
})
|
||||
return false
|
||||
end
|
||||
if self._pre_get then
|
||||
|
||||
@@ -275,7 +275,7 @@ end
|
||||
actions.edit_command_line = function(prompt_bufnr)
|
||||
local selection = action_state.get_selected_entry()
|
||||
if selection == nil then
|
||||
print "[telescope] Nothing currently selected"
|
||||
utils.__warn_no_selection "actions.edit_command_line"
|
||||
return
|
||||
end
|
||||
actions.close(prompt_bufnr)
|
||||
@@ -285,7 +285,7 @@ end
|
||||
actions.set_command_line = function(prompt_bufnr)
|
||||
local selection = action_state.get_selected_entry()
|
||||
if selection == nil then
|
||||
print "[telescope] Nothing currently selected"
|
||||
utils.__warn_no_selection "actions.set_command_line"
|
||||
return
|
||||
end
|
||||
actions.close(prompt_bufnr)
|
||||
@@ -296,7 +296,7 @@ end
|
||||
actions.edit_search_line = function(prompt_bufnr)
|
||||
local selection = action_state.get_selected_entry()
|
||||
if selection == nil then
|
||||
print "[telescope] Nothing currently selected"
|
||||
utils.__warn_no_selection "actions.edit_search_line"
|
||||
return
|
||||
end
|
||||
actions.close(prompt_bufnr)
|
||||
@@ -306,7 +306,7 @@ end
|
||||
actions.set_search_line = function(prompt_bufnr)
|
||||
local selection = action_state.get_selected_entry()
|
||||
if selection == nil then
|
||||
print "[telescope] Nothing currently selected"
|
||||
utils.__warn_no_selection "actions.set_search_line"
|
||||
return
|
||||
end
|
||||
actions.close(prompt_bufnr)
|
||||
@@ -338,7 +338,7 @@ end
|
||||
actions.paste_register = function(prompt_bufnr)
|
||||
local selection = action_state.get_selected_entry()
|
||||
if selection == nil then
|
||||
print "[telescope] Nothing currently selected"
|
||||
utils.__warn_no_selection "actions.paste_register"
|
||||
return
|
||||
end
|
||||
|
||||
@@ -346,7 +346,6 @@ actions.paste_register = function(prompt_bufnr)
|
||||
|
||||
-- ensure that the buffer can be written to
|
||||
if vim.api.nvim_buf_get_option(vim.api.nvim_get_current_buf(), "modifiable") then
|
||||
print "Paste!"
|
||||
vim.api.nvim_paste(selection.content, true, -1)
|
||||
end
|
||||
end
|
||||
@@ -370,7 +369,7 @@ end
|
||||
actions.insert_value = function(prompt_bufnr)
|
||||
local selection = action_state.get_selected_entry()
|
||||
if selection == nil then
|
||||
print "[telescope] Nothing currently selected"
|
||||
utils.__warn_no_selection "actions.insert_value"
|
||||
return
|
||||
end
|
||||
|
||||
@@ -388,11 +387,17 @@ actions.git_create_branch = function(prompt_bufnr)
|
||||
local new_branch = action_state.get_current_line()
|
||||
|
||||
if new_branch == "" then
|
||||
print "Please enter the name of the new branch to create"
|
||||
utils.notify("actions.git_create_branch", {
|
||||
msg = "Missing the new branch name",
|
||||
level = "ERROR",
|
||||
})
|
||||
else
|
||||
local confirmation = vim.fn.input(string.format('Create new branch "%s"? [y/n]: ', new_branch))
|
||||
local confirmation = vim.fn.input(string.format("Create new branch '%s'? [y/n]: ", new_branch))
|
||||
if string.len(confirmation) == 0 or string.sub(string.lower(confirmation), 0, 1) ~= "y" then
|
||||
print(string.format('Didn\'t create branch "%s"', new_branch))
|
||||
utils.notify("actions.git_create_branch", {
|
||||
msg = string.format("fail to create branch: '%s'", new_branch),
|
||||
level = "ERROR",
|
||||
})
|
||||
return
|
||||
end
|
||||
|
||||
@@ -400,11 +405,19 @@ actions.git_create_branch = function(prompt_bufnr)
|
||||
|
||||
local _, ret, stderr = utils.get_os_command_output({ "git", "checkout", "-b", new_branch }, cwd)
|
||||
if ret == 0 then
|
||||
print(string.format("Switched to a new branch: %s", new_branch))
|
||||
utils.notify("actions.git_create_branch", {
|
||||
msg = string.format("Switched to a new branch: %s", new_branch),
|
||||
level = "INFO",
|
||||
})
|
||||
else
|
||||
print(
|
||||
string.format('Error when creating new branch: %s Git returned "%s"', new_branch, table.concat(stderr, " "))
|
||||
)
|
||||
utils.notify("actions.git_create_branch", {
|
||||
msg = string.format(
|
||||
"Error when creating new branch: '%s' Git returned '%s'",
|
||||
new_branch,
|
||||
table.concat(stderr, " ")
|
||||
),
|
||||
level = "INFO",
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -414,15 +427,21 @@ end
|
||||
actions.git_apply_stash = function(prompt_bufnr)
|
||||
local selection = action_state.get_selected_entry()
|
||||
if selection == nil then
|
||||
print "[telescope] Nothing currently selected"
|
||||
utils.__warn_no_selection "actions.git_apply_stash"
|
||||
return
|
||||
end
|
||||
actions.close(prompt_bufnr)
|
||||
local _, ret, stderr = utils.get_os_command_output { "git", "stash", "apply", "--index", selection.value }
|
||||
if ret == 0 then
|
||||
print("applied: " .. selection.value)
|
||||
utils.notify("actions.git_apply_stash", {
|
||||
msg = string.format("applied: '%s' ", selection.value),
|
||||
level = "INFO",
|
||||
})
|
||||
else
|
||||
print(string.format('Error when applying: %s. Git returned: "%s"', selection.value, table.concat(stderr, " ")))
|
||||
utils.notify("actions.git_apply_stash", {
|
||||
msg = string.format("Error when applying: %s. Git returned: '%s'", selection.value, table.concat(stderr, " ")),
|
||||
level = "ERROR",
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
@@ -432,15 +451,25 @@ actions.git_checkout = function(prompt_bufnr)
|
||||
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
|
||||
local selection = action_state.get_selected_entry()
|
||||
if selection == nil then
|
||||
print "[telescope] Nothing currently selected"
|
||||
utils.__warn_no_selection "actions.git_checkout"
|
||||
return
|
||||
end
|
||||
actions.close(prompt_bufnr)
|
||||
local _, ret, stderr = utils.get_os_command_output({ "git", "checkout", selection.value }, cwd)
|
||||
if ret == 0 then
|
||||
print("Checked out: " .. selection.value)
|
||||
utils.notify("actions.git_checkout", {
|
||||
msg = string.format("Checked out: ", selection.value),
|
||||
level = "INFO",
|
||||
})
|
||||
else
|
||||
print(string.format('Error when checking out: %s. Git returned: "%s"', selection.value, table.concat(stderr, " ")))
|
||||
utils.notify("actions.git_checkout", {
|
||||
msg = string.format(
|
||||
"Error when checking out: %s. Git returned: '%s'",
|
||||
selection.value,
|
||||
table.concat(stderr, " ")
|
||||
),
|
||||
level = "ERORR",
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
@@ -452,7 +481,7 @@ actions.git_switch_branch = function(prompt_bufnr)
|
||||
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
|
||||
local selection = action_state.get_selected_entry()
|
||||
if selection == nil then
|
||||
print "[telescope] Nothing currently selected"
|
||||
utils.__warn_no_selection "actions.git_switch_branch"
|
||||
return
|
||||
end
|
||||
actions.close(prompt_bufnr)
|
||||
@@ -463,9 +492,19 @@ actions.git_switch_branch = function(prompt_bufnr)
|
||||
end
|
||||
local _, ret, stderr = utils.get_os_command_output({ "git", "switch", branch }, cwd)
|
||||
if ret == 0 then
|
||||
print("Switched to: " .. branch)
|
||||
utils.notify("actions.git_switch_branch", {
|
||||
msg = string.format("Switched to: '%s'", branch),
|
||||
level = "INFO",
|
||||
})
|
||||
else
|
||||
print(string.format('Error when switching to: %s. Git returned: "%s"', selection.value, table.concat(stderr, " ")))
|
||||
utils.notify("actions.git_switch_branch", {
|
||||
msg = string.format(
|
||||
"Error when switching to: %s. Git returned: '%s'",
|
||||
selection.value,
|
||||
table.concat(stderr, " ")
|
||||
),
|
||||
level = "ERORR",
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
@@ -474,7 +513,7 @@ local function make_git_branch_action(opts)
|
||||
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
|
||||
local selection = action_state.get_selected_entry()
|
||||
if selection == nil then
|
||||
print "[telescope] Nothing currently selected"
|
||||
utils.__warn_no_selection(opts.action_name)
|
||||
return
|
||||
end
|
||||
|
||||
@@ -489,9 +528,15 @@ local function make_git_branch_action(opts)
|
||||
actions.close(prompt_bufnr)
|
||||
local _, ret, stderr = utils.get_os_command_output(opts.command(selection.value), cwd)
|
||||
if ret == 0 then
|
||||
print(string.format(opts.success_message, selection.value))
|
||||
utils.notify(opts.action_name, {
|
||||
msg = string.format(opts.success_message, selection.value),
|
||||
level = "INFO",
|
||||
})
|
||||
else
|
||||
print(string.format(opts.error_message, selection.value, table.concat(stderr, " ")))
|
||||
utils.notify(opts.action_name, {
|
||||
msg = string.format(opts.error_message, selection.value, table.concat(stderr, " ")),
|
||||
level = "ERROR",
|
||||
})
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -500,8 +545,9 @@ end
|
||||
---@param prompt_bufnr number: The prompt bufnr
|
||||
actions.git_track_branch = make_git_branch_action {
|
||||
should_confirm = false,
|
||||
action_name = "actions.git_track_branch",
|
||||
success_message = "Tracking branch: %s",
|
||||
error_message = 'Error when tracking branch: %s. Git returned: "%s"',
|
||||
error_message = "Error when tracking branch: %s. Git returned: '%s'",
|
||||
command = function(branch_name)
|
||||
return { "git", "checkout", "--track", branch_name }
|
||||
end,
|
||||
@@ -511,9 +557,10 @@ actions.git_track_branch = make_git_branch_action {
|
||||
---@param prompt_bufnr number: The prompt bufnr
|
||||
actions.git_delete_branch = make_git_branch_action {
|
||||
should_confirm = true,
|
||||
action_name = "actions.git_delete_branch",
|
||||
confirmation_question = "Do you really wanna delete branch %s? [Y/n] ",
|
||||
success_message = "Deleted branch: %s",
|
||||
error_message = 'Error when deleting branch: %s. Git returned: "%s"',
|
||||
error_message = "Error when deleting branch: %s. Git returned: '%s'",
|
||||
command = function(branch_name)
|
||||
return { "git", "branch", "-D", branch_name }
|
||||
end,
|
||||
@@ -523,9 +570,10 @@ actions.git_delete_branch = make_git_branch_action {
|
||||
---@param prompt_bufnr number: The prompt bufnr
|
||||
actions.git_merge_branch = make_git_branch_action {
|
||||
should_confirm = true,
|
||||
action_name = "actions.git_merge_branch",
|
||||
confirmation_question = "Do you really wanna merge branch %s? [Y/n] ",
|
||||
success_message = "Merged branch: %s",
|
||||
error_message = 'Error when merging branch: %s. Git returned: "%s"',
|
||||
error_message = "Error when merging branch: %s. Git returned: '%s'",
|
||||
command = function(branch_name)
|
||||
return { "git", "merge", branch_name }
|
||||
end,
|
||||
@@ -535,9 +583,10 @@ actions.git_merge_branch = make_git_branch_action {
|
||||
---@param prompt_bufnr number: The prompt bufnr
|
||||
actions.git_rebase_branch = make_git_branch_action {
|
||||
should_confirm = true,
|
||||
action_name = "actions.git_rebase_branch",
|
||||
confirmation_question = "Do you really wanna rebase branch %s? [Y/n] ",
|
||||
success_message = "Rebased branch: %s",
|
||||
error_message = 'Error when rebasing branch: %s. Git returned: "%s"',
|
||||
error_message = "Error when rebasing branch: %s. Git returned: '%s'",
|
||||
command = function(branch_name)
|
||||
return { "git", "rebase", branch_name }
|
||||
end,
|
||||
@@ -547,7 +596,7 @@ local git_reset_branch = function(prompt_bufnr, mode)
|
||||
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
|
||||
local selection = action_state.get_selected_entry()
|
||||
if selection == nil then
|
||||
print "[telescope] Nothing currently selected"
|
||||
utils.__warn_no_selection "actions.git_reset_branch"
|
||||
return
|
||||
end
|
||||
|
||||
@@ -559,9 +608,15 @@ local git_reset_branch = function(prompt_bufnr, mode)
|
||||
actions.close(prompt_bufnr)
|
||||
local _, ret, stderr = utils.get_os_command_output({ "git", "reset", mode, selection.value }, cwd)
|
||||
if ret == 0 then
|
||||
print("Reset to: " .. selection.value)
|
||||
utils.notify("actions.git_rebase_branch", {
|
||||
msg = string.format("Reset to: '%s'", selection.value),
|
||||
level = "INFO",
|
||||
})
|
||||
else
|
||||
print(string.format('Error when resetting to: %s. Git returned: "%s"', selection.value, table.concat(stderr, " ")))
|
||||
utils.notify("actions.git_rebase_branch", {
|
||||
msg = string.format("Rest to: %s. Git returned: '%s'", selection.value, table.concat(stderr, " ")),
|
||||
level = "ERROR",
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
@@ -587,7 +642,8 @@ actions.git_checkout_current_buffer = function(prompt_bufnr)
|
||||
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
|
||||
local selection = action_state.get_selected_entry()
|
||||
if selection == nil then
|
||||
print "[telescope] Nothing currently selected"
|
||||
utils.__warn_no_selection "actions.git_checkout_current_buffer"
|
||||
|
||||
return
|
||||
end
|
||||
actions.close(prompt_bufnr)
|
||||
@@ -600,7 +656,7 @@ actions.git_staging_toggle = function(prompt_bufnr)
|
||||
local cwd = action_state.get_current_picker(prompt_bufnr).cwd
|
||||
local selection = action_state.get_selected_entry()
|
||||
if selection == nil then
|
||||
print "[telescope] Nothing currently selected"
|
||||
utils.__warn_no_selection "actions.git_staging_toggle"
|
||||
return
|
||||
end
|
||||
if selection.status:sub(2) == " " then
|
||||
@@ -707,7 +763,7 @@ end
|
||||
|
||||
local smart_send = function(prompt_bufnr, mode, target)
|
||||
local picker = action_state.get_current_picker(prompt_bufnr)
|
||||
if table.getn(picker:get_multi_selection()) > 0 then
|
||||
if #picker:get_multi_selection() > 0 then
|
||||
send_selected_to_qf(prompt_bufnr, mode, target)
|
||||
else
|
||||
send_all_to_qf(prompt_bufnr, mode, target)
|
||||
@@ -744,7 +800,11 @@ actions.complete_tag = function(prompt_bufnr)
|
||||
local delimiter = current_picker.sorter._delimiter
|
||||
|
||||
if not tags then
|
||||
print "No tag pre-filtering set for this picker"
|
||||
utils.notify("actions.complete_tag", {
|
||||
msg = "No tag pre-filtering set for this picker",
|
||||
level = "ERROR",
|
||||
})
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
@@ -771,7 +831,10 @@ actions.complete_tag = function(prompt_bufnr)
|
||||
end
|
||||
|
||||
if vim.tbl_isempty(filtered_tags) then
|
||||
print "No matches found"
|
||||
utils.notify("complete_tag", {
|
||||
msg = "No matches found",
|
||||
level = "INFO",
|
||||
})
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@ local a = vim.api
|
||||
local log = require "telescope.log"
|
||||
local Path = require "plenary.path"
|
||||
local state = require "telescope.state"
|
||||
local utils = require "telescope.utils"
|
||||
|
||||
local action_state = require "telescope.actions.state"
|
||||
|
||||
@@ -88,7 +89,10 @@ action_set.edit = function(prompt_bufnr, command)
|
||||
local entry = action_state.get_selected_entry()
|
||||
|
||||
if not entry then
|
||||
print "[telescope] Nothing currently selected"
|
||||
utils.notify("actions.set.edit", {
|
||||
msg = "Nothing currently selected",
|
||||
level = "WARN",
|
||||
})
|
||||
return
|
||||
end
|
||||
|
||||
@@ -105,7 +109,10 @@ action_set.edit = function(prompt_bufnr, command)
|
||||
-- to put stuff into `filename`
|
||||
local value = entry.value
|
||||
if not value then
|
||||
print "Could not do anything with blank line..."
|
||||
utils.notify("actions.set.edit", {
|
||||
msg = "Could not do anything with blank line...",
|
||||
level = "WARN",
|
||||
})
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user