diff --git a/README.md b/README.md index 7ceb930..021e517 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@

Directory browser for Neovim, inspired by Ranger

-![Triptych screenshot](screenshot.jpg?raw=true "Triptych screenshot") +![Triptych screenshot](screenshot.jpg "Triptych screenshot") [![CI](https://github.com/simonmclean/triptych.nvim/actions/workflows/ci.yml/badge.svg)](https://github.com/simonmclean/triptych.nvim/actions/workflows/ci.yml) @@ -100,6 +100,7 @@ Key mappings can either be a string, or a table of strings if you want multiple add = 'a', copy = 'c', rename = 'r', + rename_from_scratch = 'R', cut = 'x', paste = 'p', quit = 'q', diff --git a/doc/triptych.nvim.txt b/doc/triptych.nvim.txt index cc6e061..9d48718 100644 --- a/doc/triptych.nvim.txt +++ b/doc/triptych.nvim.txt @@ -107,6 +107,7 @@ bindings. add = 'a', copy = 'c', rename = 'r', + rename_from_scratch = 'R', cut = 'x', paste = 'p', quit = 'q', diff --git a/lua/triptych/actions.lua b/lua/triptych/actions.lua index 71527c9..459f133 100644 --- a/lua/triptych/actions.lua +++ b/lua/triptych/actions.lua @@ -260,15 +260,17 @@ function Actions.new(State, refresh_view) refresh_view() end + ---@param from_scratch boolean ---@return nil - M.rename = function() + M.rename = function(from_scratch) local target = view.get_target_under_cursor(State) if target then local display_name = u.cond(target.is_dir, { when_true = u.trim_last_char(target.display_name), when_false = target.display_name, }) - local response = vim.fn.trim(vim.fn.input('Enter new name for "' .. display_name .. '": ')) + local input_text = from_scratch and '' or display_name + local response = vim.fn.trim(vim.fn.input('Enter new name for "' .. display_name .. '": ', input_text)) if u.is_defined(response) and response ~= target.display_name then local destination = u.path_join(target.dirname, response) rename_node_and_publish(target.path, destination) diff --git a/lua/triptych/config.lua b/lua/triptych/config.lua index 31e1d93..e93bb59 100644 --- a/lua/triptych/config.lua +++ b/lua/triptych/config.lua @@ -27,6 +27,7 @@ end ---@field add KeyMapping ---@field copy KeyMapping ---@field rename KeyMapping +---@field rename_from_scratch KeyMapping ---@field cut KeyMapping ---@field paste KeyMapping ---@field quit KeyMapping @@ -112,6 +113,7 @@ local function default_config() add = 'a', copy = 'c', rename = 'r', + rename_from_scratch = 'R', cut = 'x', paste = 'p', quit = 'q', diff --git a/lua/triptych/help.lua b/lua/triptych/help.lua index 818e403..39eeaa5 100644 --- a/lua/triptych/help.lua +++ b/lua/triptych/help.lua @@ -3,7 +3,6 @@ ---@return string[] local function help_lines() local mappings = vim.g.triptych_config.mappings - local lines = {} local left_col_length = 0 -- Used for padding and alignment -- Update padding/alignment @@ -13,25 +12,50 @@ local function help_lines() end end - for key, value in pairs(mappings) do - local display_value = value + ---@type { key: string, action: string }[] + local mapping_pairs = {} + + for action, keys in pairs(mappings) do + local keys_display_value + if type(keys) == 'table' then + keys_display_value = table.concat(keys, ', ') + else + keys_display_value = keys + end + table.insert(mapping_pairs, { + action = action, + key = keys_display_value, + }) + end + + table.sort(mapping_pairs, function(a, b) + return a.action < b.action + end) + + ---@type string[] + local lines = {} + + for _, pair in ipairs(mapping_pairs) do + local keys_str = pair.key local padding = '' - if type(value) == 'table' then - display_value = table.concat(value, ', ') + if type(pair.key) == 'table' then + keys_str = table.concat(pair.key, ', ') end - if string.len(display_value) < left_col_length then - local diff = left_col_length - string.len(display_value) + if string.len(pair.action) < left_col_length then + local diff = left_col_length - string.len(pair.action) padding = string.rep(' ', diff) end - local line = '[' .. display_value .. ']' .. padding .. ' : ' .. key + local line = string.format('%s%s : %s', pair.action, padding, keys_str) table.insert(lines, line) end - table.sort(lines) - table.insert(lines, 1, 'Triptych key bindings') table.insert(lines, 2, '') + for index, line in ipairs(lines) do + lines[index] = ' ' .. line + end + return lines end diff --git a/lua/triptych/mappings.lua b/lua/triptych/mappings.lua index 91e11eb..ed83b6e 100644 --- a/lua/triptych/mappings.lua +++ b/lua/triptych/mappings.lua @@ -41,7 +41,12 @@ function Mappings.new(State, actions, refresh_fn) map('n', mappings.add, actions.add_file_or_dir, { desc = 'add file or directory' }) map('n', mappings.copy, actions.toggle_copy, { desc = 'copy' }) map('v', mappings.copy, actions.bulk_toggle_copy, { desc = 'copy selection' }) - map('n', mappings.rename, actions.rename, { desc = 'rename' }) + map('n', mappings.rename, function() + actions.rename(false) + end, { desc = 'rename' }) + map('n', mappings.rename_from_scratch, function() + actions.rename(true) + end, { desc = 'rename from scratch' }) map('n', mappings.cut, actions.toggle_cut, { desc = 'cut' }) map('v', mappings.cut, actions.bulk_toggle_cut, { desc = 'cut selection' }) map('n', mappings.paste, actions.paste, { desc = 'paste' }) diff --git a/tests/specs/config_spec.lua b/tests/specs/config_spec.lua index ddc3df3..465c6c7 100644 --- a/tests/specs/config_spec.lua +++ b/tests/specs/config_spec.lua @@ -21,6 +21,7 @@ local function expected_default_config() add = 'a', copy = 'c', rename = 'r', + rename_from_scratch = 'R', cut = 'x', paste = 'p', quit = 'q', diff --git a/tests/specs/help_spec.lua b/tests/specs/help_spec.lua index a0a5e4b..efb94c2 100644 --- a/tests/specs/help_spec.lua +++ b/tests/specs/help_spec.lua @@ -9,25 +9,26 @@ describe('help_lines', { local result = help.help_lines() assert.same({ - 'Triptych key bindings', - '', - '[-] : open_hsplit', - '[.] : jump_to_cwd', - '[] : open_tab', - '[.] : toggle_hidden', - '[cd] : cd', - '[a] : add', - '[c] : copy', - '[d] : delete', - '[g?] : show_help', - '[h] : nav_left', - '[l, ] : nav_right', - '[p] : paste', - '[q] : quit', - '[r] : rename', - '[x] : cut', - '[z] : toggle_collapse_dirs', - '[|] : open_vsplit', + ' Triptych key bindings', + ' ', + ' add : a', + ' cd : cd', + ' copy : c', + ' cut : x', + ' delete : d', + ' jump_to_cwd : .', + ' nav_left : h', + ' nav_right : l, ', + ' open_hsplit : -', + ' open_tab : ', + ' open_vsplit : |', + ' paste : p', + ' quit : q', + ' rename : r', + ' rename_from_scratch : R', + ' show_help : g?', + ' toggle_collapse_dirs : z', + ' toggle_hidden : .', }, result) end), }) diff --git a/tests/specs/ui_spec.lua b/tests/specs/ui_spec.lua index a38a66a..33e398e 100644 --- a/tests/specs/ui_spec.lua +++ b/tests/specs/ui_spec.lua @@ -465,11 +465,11 @@ describe('Triptych UI', { } open_triptych(function() - u.press_keys 'r' + u.press_keys 'R' u.press_keys 'renamed_dir' u.on_primary_window_updated(function() u.press_keys 'j' - u.press_keys 'r' + u.press_keys 'R' u.press_keys 'renamed_file.lua' u.on_primary_window_updated(function() local state = u.get_state() @@ -507,11 +507,11 @@ describe('Triptych UI', { } open_triptych(function() - u.press_keys 'r' + u.press_keys 'R' u.press_keys 'renamed_dir' u.on_primary_window_updated(function() u.press_keys 'j' - u.press_keys 'r' + u.press_keys 'R' u.press_keys 'renamed_file.lua' end) u.on_events({