Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<p align="center">Directory browser for Neovim, inspired by <a href="https://github.com/ranger/ranger">Ranger</a></p>

![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)

Expand Down Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions doc/triptych.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ bindings.
add = 'a',
copy = 'c',
rename = 'r',
rename_from_scratch = 'R',
cut = 'x',
paste = 'p',
quit = 'q',
Expand Down
6 changes: 4 additions & 2 deletions lua/triptych/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lua/triptych/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -112,6 +113,7 @@ local function default_config()
add = 'a',
copy = 'c',
rename = 'r',
rename_from_scratch = 'R',
cut = 'x',
paste = 'p',
quit = 'q',
Expand Down
44 changes: 34 additions & 10 deletions lua/triptych/help.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
7 changes: 6 additions & 1 deletion lua/triptych/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
Expand Down
1 change: 1 addition & 0 deletions tests/specs/config_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
39 changes: 20 additions & 19 deletions tests/specs/help_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@ describe('help_lines', {
local result = help.help_lines()

assert.same({
'Triptych key bindings',
'',
'[-] : open_hsplit',
'[.] : jump_to_cwd',
'[<C-t>] : open_tab',
'[<leader>.] : toggle_hidden',
'[<leader>cd] : cd',
'[a] : add',
'[c] : copy',
'[d] : delete',
'[g?] : show_help',
'[h] : nav_left',
'[l, <CR>] : nav_right',
'[p] : paste',
'[q] : quit',
'[r] : rename',
'[x] : cut',
'[z] : toggle_collapse_dirs',
'[|] : open_vsplit',
' Triptych key bindings',
' ',
' add : a',
' cd : <leader>cd',
' copy : c',
' cut : x',
' delete : d',
' jump_to_cwd : .',
' nav_left : h',
' nav_right : l, <CR>',
' open_hsplit : -',
' open_tab : <C-t>',
' open_vsplit : |',
' paste : p',
' quit : q',
' rename : r',
' rename_from_scratch : R',
' show_help : g?',
' toggle_collapse_dirs : z',
' toggle_hidden : <leader>.',
}, result)
end),
})
8 changes: 4 additions & 4 deletions tests/specs/ui_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -465,11 +465,11 @@ describe('Triptych UI', {
}

open_triptych(function()
u.press_keys 'r'
u.press_keys 'R'
u.press_keys 'renamed_dir<cr>'
u.on_primary_window_updated(function()
u.press_keys 'j'
u.press_keys 'r'
u.press_keys 'R'
u.press_keys 'renamed_file.lua<cr>'
u.on_primary_window_updated(function()
local state = u.get_state()
Expand Down Expand Up @@ -507,11 +507,11 @@ describe('Triptych UI', {
}

open_triptych(function()
u.press_keys 'r'
u.press_keys 'R'
u.press_keys 'renamed_dir<cr>'
u.on_primary_window_updated(function()
u.press_keys 'j'
u.press_keys 'r'
u.press_keys 'R'
u.press_keys 'renamed_file.lua<cr>'
end)
u.on_events({
Expand Down
Loading