feat(file-manager): filter the listing and open files faster
The panel could only be browsed entry by entry, which is slower than cd and ls in a large directory. Typing / now filters the listing with the same fuzzy matcher as the command palette, typing : lists every action with its description from a single catalog, and o/t open the selection in a new tab or OS window. Sizes are read once when the directory is listed so that sort_by size does not need to stat the entries again.
This commit is contained in:
3 files changed
+569
-59
No files matched your search
+418
-58
@@ -14,6 +14,7 @@ import (
|
|||||||
|
|
||||||
"github.com/kovidgoyal/kitty/tools/cli"
|
"github.com/kovidgoyal/kitty/tools/cli"
|
||||||
"github.com/kovidgoyal/kitty/tools/config"
|
"github.com/kovidgoyal/kitty/tools/config"
|
||||||
|
"github.com/kovidgoyal/kitty/tools/fzf"
|
||||||
"github.com/kovidgoyal/kitty/tools/tui"
|
"github.com/kovidgoyal/kitty/tools/tui"
|
||||||
"github.com/kovidgoyal/kitty/tools/tui/loop"
|
"github.com/kovidgoyal/kitty/tools/tui/loop"
|
||||||
"github.com/kovidgoyal/kitty/tools/wcswidth"
|
"github.com/kovidgoyal/kitty/tools/wcswidth"
|
||||||
@@ -27,6 +28,7 @@ type Entry struct {
|
|||||||
Name string
|
Name string
|
||||||
IsDir bool
|
IsDir bool
|
||||||
ModTime int64
|
ModTime int64
|
||||||
|
Size int64
|
||||||
}
|
}
|
||||||
|
|
||||||
type input_purpose int
|
type input_purpose int
|
||||||
@@ -36,6 +38,7 @@ const (
|
|||||||
input_create_file
|
input_create_file
|
||||||
input_create_dir
|
input_create_dir
|
||||||
input_rename
|
input_rename
|
||||||
|
input_search
|
||||||
)
|
)
|
||||||
|
|
||||||
// Pure helpers, kept free of terminal state so that they can be unit tested
|
// Pure helpers, kept free of terminal state so that they can be unit tested
|
||||||
@@ -51,8 +54,15 @@ func sort_entries(entries []Entry, sort_by string) {
|
|||||||
if a.IsDir != b.IsDir {
|
if a.IsDir != b.IsDir {
|
||||||
return a.IsDir
|
return a.IsDir
|
||||||
}
|
}
|
||||||
if sort_by == "mtime" && a.ModTime != b.ModTime {
|
switch sort_by {
|
||||||
return a.ModTime > b.ModTime
|
case "mtime":
|
||||||
|
if a.ModTime != b.ModTime {
|
||||||
|
return a.ModTime > b.ModTime
|
||||||
|
}
|
||||||
|
case "size":
|
||||||
|
if a.Size != b.Size {
|
||||||
|
return a.Size > b.Size
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return strings.ToLower(a.Name) < strings.ToLower(b.Name)
|
return strings.ToLower(a.Name) < strings.ToLower(b.Name)
|
||||||
})
|
})
|
||||||
@@ -72,6 +82,7 @@ func read_dir(dir string, show_hidden bool, sort_by string) ([]Entry, error) {
|
|||||||
e := Entry{Name: name, IsDir: item.IsDir()}
|
e := Entry{Name: name, IsDir: item.IsDir()}
|
||||||
if info, ierr := item.Info(); ierr == nil {
|
if info, ierr := item.Info(); ierr == nil {
|
||||||
e.ModTime = info.ModTime().Unix()
|
e.ModTime = info.ModTime().Unix()
|
||||||
|
e.Size = info.Size()
|
||||||
}
|
}
|
||||||
entries = append(entries, e)
|
entries = append(entries, e)
|
||||||
}
|
}
|
||||||
@@ -191,14 +202,20 @@ type Handler struct {
|
|||||||
screen_size loop.ScreenSize
|
screen_size loop.ScreenSize
|
||||||
conf *Config
|
conf *Config
|
||||||
cwd string
|
cwd string
|
||||||
|
all_entries []Entry
|
||||||
entries []Entry
|
entries []Entry
|
||||||
selected int
|
selected int
|
||||||
scroll int
|
scroll int
|
||||||
show_hidden bool
|
show_hidden bool
|
||||||
|
search_query string
|
||||||
input_purpose input_purpose
|
input_purpose input_purpose
|
||||||
text_buffer string
|
text_buffer string
|
||||||
rename_source string
|
rename_source string
|
||||||
confirm_name string
|
confirm_name string
|
||||||
|
palette_open bool
|
||||||
|
palette_query string
|
||||||
|
palette []action_info
|
||||||
|
palette_selected int
|
||||||
message string
|
message string
|
||||||
result_action string
|
result_action string
|
||||||
result_path string
|
result_path string
|
||||||
@@ -232,9 +249,15 @@ func (h *Handler) load(dir string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
h.cwd = dir
|
h.cwd = dir
|
||||||
|
h.all_entries = entries
|
||||||
h.entries = entries
|
h.entries = entries
|
||||||
if h.selected >= len(entries) {
|
if h.search_query != "" {
|
||||||
h.selected = max(0, len(entries)-1)
|
if err = h.apply_search(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if h.selected >= len(h.entries) {
|
||||||
|
h.selected = max(0, len(h.entries)-1)
|
||||||
}
|
}
|
||||||
if h.scroll > h.selected {
|
if h.scroll > h.selected {
|
||||||
h.scroll = h.selected
|
h.scroll = h.selected
|
||||||
@@ -277,6 +300,8 @@ func (h *Handler) reload() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) change_dir(dir string) {
|
func (h *Handler) change_dir(dir string) {
|
||||||
|
// A filter belongs to the directory it was typed in, not to the next one.
|
||||||
|
h.search_query, h.text_buffer, h.input_purpose = "", "", input_none
|
||||||
if err := h.load(dir); err != nil {
|
if err := h.load(dir); err != nil {
|
||||||
h.message = err.Error()
|
h.message = err.Error()
|
||||||
h.draw_screen()
|
h.draw_screen()
|
||||||
@@ -398,6 +423,9 @@ func (h *Handler) copy_selected_path() {
|
|||||||
// activate implements the primary action: the selected directory becomes the
|
// activate implements the primary action: the selected directory becomes the
|
||||||
// working directory of the shell, the selected file is opened in the editor.
|
// working directory of the shell, the selected file is opened in the editor.
|
||||||
func (h *Handler) activate() {
|
func (h *Handler) activate() {
|
||||||
|
if h.input_purpose == input_search {
|
||||||
|
h.stop_search()
|
||||||
|
}
|
||||||
e, ok := h.selected_entry()
|
e, ok := h.selected_entry()
|
||||||
if !ok {
|
if !ok {
|
||||||
h.lp.Beep()
|
h.lp.Beep()
|
||||||
@@ -411,15 +439,24 @@ func (h *Handler) activate() {
|
|||||||
h.open_file(path)
|
h.open_file(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
// edit_args is the client side of kitty's edit protocol. The terminal opens the
|
// edit_args_in is the client side of kitty's edit protocol. The terminal opens
|
||||||
// editor in a new tab, which gives the file a whole window even when the panel
|
// the editor in a new tab or OS window, which gives the file a whole window even
|
||||||
// is docked. The tab closes when the editor exits and the panel is redrawn in
|
// when the panel is docked. The window closes when the editor exits and the
|
||||||
// place, instead of handing the command to the shell and disappearing.
|
// panel is redrawn in place, instead of handing the command to the shell and
|
||||||
|
// disappearing.
|
||||||
|
func edit_args_in(path, window_type string) []string {
|
||||||
|
return []string{"edit-in-kitty", "--type=" + window_type, path}
|
||||||
|
}
|
||||||
|
|
||||||
func edit_args(path string) []string {
|
func edit_args(path string) []string {
|
||||||
return []string{"edit-in-kitty", "--type=tab", path}
|
return edit_args_in(path, "tab")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) open_file(path string) {
|
func (h *Handler) open_file(path string) {
|
||||||
|
h.open_file_in(path, "tab")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) open_file_in(path, window_type string) {
|
||||||
exe, err := os.Executable()
|
exe, err := os.Executable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.message = err.Error()
|
h.message = err.Error()
|
||||||
@@ -429,7 +466,7 @@ func (h *Handler) open_file(path string) {
|
|||||||
// The loop must give up the tty while edit-in-kitty talks to the terminal,
|
// The loop must give up the tty while edit-in-kitty talks to the terminal,
|
||||||
// otherwise both processes compete for input and escape code responses.
|
// otherwise both processes compete for input and escape code responses.
|
||||||
err = h.lp.SuspendAndRun(func() error {
|
err = h.lp.SuspendAndRun(func() error {
|
||||||
cmd := exec.Command(exe, edit_args(path)...)
|
cmd := exec.Command(exe, edit_args_in(path, window_type)...)
|
||||||
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
|
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
|
||||||
return cmd.Run()
|
return cmd.Run()
|
||||||
})
|
})
|
||||||
@@ -473,53 +510,346 @@ func (h *Handler) change_shell_dir() {
|
|||||||
h.change_shell_dir_to(path)
|
h.change_shell_dir_to(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) perform_action(name string) {
|
type action_info struct {
|
||||||
switch name {
|
Name string
|
||||||
case "quit":
|
Description string
|
||||||
h.lp.Quit(0)
|
}
|
||||||
case "move_up":
|
|
||||||
h.move_selection(-1)
|
// action_catalog is the single source of truth shared by the in-panel palette
|
||||||
case "move_down":
|
// and the documentation: every action accepted by perform_action has exactly
|
||||||
h.move_selection(1)
|
// one description here, and test_action_catalog enforces that.
|
||||||
case "page_up":
|
var action_catalog = []action_info{
|
||||||
|
{"quit", "Close the panel without changing the shell directory"},
|
||||||
|
{"move_up", "Select the previous entry"},
|
||||||
|
{"move_down", "Select the next entry"},
|
||||||
|
{"page_up", "Move the selection up by one page"},
|
||||||
|
{"page_down", "Move the selection down by one page"},
|
||||||
|
{"home", "Select the first entry"},
|
||||||
|
{"end", "Select the last entry"},
|
||||||
|
{"activate", "Change the shell directory or open the selected file"},
|
||||||
|
{"enter_dir", "Browse into the selected directory"},
|
||||||
|
{"parent_dir", "Browse into the parent directory"},
|
||||||
|
{"cd_here", "Move the shell to the directory shown in the panel"},
|
||||||
|
{"toggle_hidden", "Show or hide entries whose name starts with a dot"},
|
||||||
|
{"create_file", "Create a file in the current directory"},
|
||||||
|
{"create_dir", "Create a directory in the current directory"},
|
||||||
|
{"rename", "Rename the selected entry"},
|
||||||
|
{"delete", "Delete the selected entry after confirmation"},
|
||||||
|
{"copy_path", "Copy the path of the selection to the clipboard"},
|
||||||
|
{"refresh", "Reload the current directory"},
|
||||||
|
{"search", "Filter the listing with a fuzzy query"},
|
||||||
|
{"palette", "List and run the actions of the panel"},
|
||||||
|
{"open_tab", "Open the selected file in a new tab"},
|
||||||
|
{"open_window", "Open the selected file in a new OS window"},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Actions that need the panel state are plain functions of the handler, so the
|
||||||
|
// palette and the configurable shortcuts go through the same code path.
|
||||||
|
var action_handlers = map[string]func(*Handler){
|
||||||
|
"quit": func(h *Handler) { h.lp.Quit(0) },
|
||||||
|
"move_up": func(h *Handler) { h.move_selection(-1) },
|
||||||
|
"move_down": func(h *Handler) { h.move_selection(1) },
|
||||||
|
"page_up": func(h *Handler) {
|
||||||
h.move_selection(-max(1, int(h.screen_size.HeightCells)-3))
|
h.move_selection(-max(1, int(h.screen_size.HeightCells)-3))
|
||||||
case "page_down":
|
},
|
||||||
|
"page_down": func(h *Handler) {
|
||||||
h.move_selection(max(1, int(h.screen_size.HeightCells)-3))
|
h.move_selection(max(1, int(h.screen_size.HeightCells)-3))
|
||||||
case "home":
|
},
|
||||||
|
"home": func(h *Handler) {
|
||||||
h.selected = 0
|
h.selected = 0
|
||||||
h.draw_screen()
|
h.draw_screen()
|
||||||
case "end":
|
},
|
||||||
|
"end": func(h *Handler) {
|
||||||
if len(h.entries) > 0 {
|
if len(h.entries) > 0 {
|
||||||
h.selected = len(h.entries) - 1
|
h.selected = len(h.entries) - 1
|
||||||
}
|
}
|
||||||
h.draw_screen()
|
h.draw_screen()
|
||||||
case "parent_dir":
|
},
|
||||||
h.change_dir(parent_of(h.cwd))
|
"parent_dir": func(h *Handler) { h.change_dir(parent_of(h.cwd)) },
|
||||||
case "activate":
|
"activate": func(h *Handler) { h.activate() },
|
||||||
|
"enter_dir": func(h *Handler) { h.enter_dir() },
|
||||||
|
"cd_here": func(h *Handler) { h.change_shell_dir() },
|
||||||
|
"toggle_hidden": func(h *Handler) { h.show_hidden = !h.show_hidden; h.reload() },
|
||||||
|
"create_file": func(h *Handler) { h.start_input(input_create_file) },
|
||||||
|
"create_dir": func(h *Handler) { h.start_input(input_create_dir) },
|
||||||
|
"rename": func(h *Handler) { h.start_rename() },
|
||||||
|
"delete": func(h *Handler) { h.start_delete() },
|
||||||
|
"copy_path": func(h *Handler) { h.copy_selected_path() },
|
||||||
|
"refresh": func(h *Handler) { h.reload() },
|
||||||
|
"search": func(h *Handler) { h.start_search() },
|
||||||
|
"palette": func(h *Handler) { h.open_palette() },
|
||||||
|
"open_tab": func(h *Handler) { h.open_selected("tab") },
|
||||||
|
"open_window": func(h *Handler) { h.open_selected("window") },
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) perform_action(name string) {
|
||||||
|
if fn, ok := action_handlers[name]; ok {
|
||||||
|
fn(h)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// filter_entries keeps the entries matching query, using the same fuzzy matcher
|
||||||
|
// as the command palette so that scoring stays consistent across kittens. An
|
||||||
|
// empty query keeps the listing untouched.
|
||||||
|
func filter_entries(entries []Entry, query string) ([]Entry, error) {
|
||||||
|
if query == "" {
|
||||||
|
return entries, nil
|
||||||
|
}
|
||||||
|
names := make([]string, len(entries))
|
||||||
|
for i, e := range entries {
|
||||||
|
names[i] = e.Name
|
||||||
|
if e.IsDir {
|
||||||
|
// Matching the trailing slash lets a query such as "src/" only
|
||||||
|
// select directories.
|
||||||
|
names[i] += "/"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m := fzf.NewFuzzyMatcher(fzf.PATH_SCHEME)
|
||||||
|
results, err := m.Score(names, query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
type scored_entry struct {
|
||||||
|
entry Entry
|
||||||
|
score uint
|
||||||
|
}
|
||||||
|
matches := make([]scored_entry, 0, len(entries))
|
||||||
|
for i, r := range results {
|
||||||
|
if r.Score > 0 {
|
||||||
|
matches = append(matches, scored_entry{entries[i], r.Score})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.SliceStable(matches, func(i, j int) bool {
|
||||||
|
if matches[i].score != matches[j].score {
|
||||||
|
return matches[i].score > matches[j].score
|
||||||
|
}
|
||||||
|
return strings.ToLower(matches[i].entry.Name) < strings.ToLower(matches[j].entry.Name)
|
||||||
|
})
|
||||||
|
ans := make([]Entry, len(matches))
|
||||||
|
for i, m := range matches {
|
||||||
|
ans[i] = m.entry
|
||||||
|
}
|
||||||
|
return ans, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// filter_actions keeps the actions whose name or description matches query, most
|
||||||
|
// relevant first. An empty query lists the whole catalog.
|
||||||
|
func filter_actions(query string) ([]action_info, error) {
|
||||||
|
if query == "" {
|
||||||
|
return action_catalog, nil
|
||||||
|
}
|
||||||
|
texts := make([]string, len(action_catalog))
|
||||||
|
for i, a := range action_catalog {
|
||||||
|
texts[i] = a.Name + " " + a.Description
|
||||||
|
}
|
||||||
|
m := fzf.NewFuzzyMatcher(fzf.DEFAULT_SCHEME)
|
||||||
|
results, err := m.Score(texts, query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
type scored_action struct {
|
||||||
|
action action_info
|
||||||
|
score uint
|
||||||
|
}
|
||||||
|
matches := make([]scored_action, 0, len(action_catalog))
|
||||||
|
for i, r := range results {
|
||||||
|
if r.Score > 0 {
|
||||||
|
matches = append(matches, scored_action{action_catalog[i], r.Score})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sort.SliceStable(matches, func(i, j int) bool {
|
||||||
|
if matches[i].score != matches[j].score {
|
||||||
|
return matches[i].score > matches[j].score
|
||||||
|
}
|
||||||
|
return matches[i].action.Name < matches[j].action.Name
|
||||||
|
})
|
||||||
|
ans := make([]action_info, len(matches))
|
||||||
|
for i, m := range matches {
|
||||||
|
ans[i] = m.action
|
||||||
|
}
|
||||||
|
return ans, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) start_search() {
|
||||||
|
h.input_purpose = input_search
|
||||||
|
h.text_buffer, h.search_query = "", ""
|
||||||
|
h.entries = h.all_entries
|
||||||
|
h.selected, h.scroll, h.message = 0, 0, ""
|
||||||
|
h.draw_screen()
|
||||||
|
}
|
||||||
|
|
||||||
|
// apply_search filters the listing without redrawing, so that it can also be
|
||||||
|
// used from load() while the panel is being rebuilt.
|
||||||
|
func (h *Handler) apply_search() error {
|
||||||
|
entries, err := filter_entries(h.all_entries, h.search_query)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
h.entries = entries
|
||||||
|
h.selected = max(0, min(h.selected, len(h.entries)-1))
|
||||||
|
h.scroll = 0
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// stop_search clears the filter and restores the full listing, keeping the
|
||||||
|
// selection on the entry the user was looking at when it still exists.
|
||||||
|
func (h *Handler) stop_search() {
|
||||||
|
name := ""
|
||||||
|
if e, ok := h.selected_entry(); ok {
|
||||||
|
name = e.Name
|
||||||
|
}
|
||||||
|
h.input_purpose, h.text_buffer, h.search_query = input_none, "", ""
|
||||||
|
h.entries = h.all_entries
|
||||||
|
h.selected, h.scroll = 0, 0
|
||||||
|
h.select_name(name)
|
||||||
|
if h.selected >= len(h.entries) {
|
||||||
|
h.selected = max(0, len(h.entries)-1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) handle_search_key(ev *loop.KeyEvent) error {
|
||||||
|
switch {
|
||||||
|
case ev.MatchesPressOrRepeat("escape"):
|
||||||
|
ev.Handled = true
|
||||||
|
h.stop_search()
|
||||||
|
h.draw_screen()
|
||||||
|
case ev.MatchesPressOrRepeat("enter"):
|
||||||
|
ev.Handled = true
|
||||||
h.activate()
|
h.activate()
|
||||||
case "enter_dir":
|
case ev.MatchesPressOrRepeat("up"):
|
||||||
|
ev.Handled = true
|
||||||
|
h.move_selection(-1)
|
||||||
|
case ev.MatchesPressOrRepeat("down"):
|
||||||
|
ev.Handled = true
|
||||||
|
h.move_selection(1)
|
||||||
|
case ev.MatchesPressOrRepeat("backspace"):
|
||||||
|
ev.Handled = true
|
||||||
|
if h.text_buffer != "" {
|
||||||
|
g := wcswidth.SplitIntoGraphemes(h.text_buffer)
|
||||||
|
h.text_buffer = strings.Join(g[:len(g)-1], "")
|
||||||
|
}
|
||||||
|
h.search_query = h.text_buffer
|
||||||
|
if err := h.apply_search(); err != nil {
|
||||||
|
h.message = err.Error()
|
||||||
|
}
|
||||||
|
h.draw_screen()
|
||||||
|
case ev.MatchesPressOrRepeat("tab"), ev.MatchesPressOrRepeat("shift+tab"):
|
||||||
|
// Tabs cannot be part of a query here, they move the selection.
|
||||||
|
ev.Handled = true
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) open_palette() {
|
||||||
|
h.palette_open = true
|
||||||
|
h.palette_query = ""
|
||||||
|
h.palette = action_catalog
|
||||||
|
h.palette_selected = 0
|
||||||
|
h.message = ""
|
||||||
|
h.draw_screen()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) close_palette() {
|
||||||
|
h.palette_open = false
|
||||||
|
h.palette_query = ""
|
||||||
|
h.palette = nil
|
||||||
|
h.palette_selected = 0
|
||||||
|
h.draw_screen()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) update_palette() {
|
||||||
|
actions, err := filter_actions(h.palette_query)
|
||||||
|
if err != nil {
|
||||||
|
h.message = err.Error()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h.palette = actions
|
||||||
|
h.palette_selected = max(0, min(h.palette_selected, len(h.palette)-1))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) run_palette_action() {
|
||||||
|
if h.palette_selected < 0 || h.palette_selected >= len(h.palette) {
|
||||||
|
h.close_palette()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
name := h.palette[h.palette_selected].Name
|
||||||
|
h.palette_open, h.palette_query, h.palette = false, "", nil
|
||||||
|
h.palette_selected = 0
|
||||||
|
h.perform_action(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *Handler) handle_palette_key(ev *loop.KeyEvent) error {
|
||||||
|
switch {
|
||||||
|
case ev.MatchesPressOrRepeat("escape"):
|
||||||
|
ev.Handled = true
|
||||||
|
h.close_palette()
|
||||||
|
case ev.MatchesPressOrRepeat("enter"):
|
||||||
|
ev.Handled = true
|
||||||
|
h.run_palette_action()
|
||||||
|
case ev.MatchesPressOrRepeat("up"):
|
||||||
|
ev.Handled = true
|
||||||
|
h.palette_selected = max(0, h.palette_selected-1)
|
||||||
|
h.draw_screen()
|
||||||
|
case ev.MatchesPressOrRepeat("down"):
|
||||||
|
ev.Handled = true
|
||||||
|
h.palette_selected = min(max(0, len(h.palette)-1), h.palette_selected+1)
|
||||||
|
h.draw_screen()
|
||||||
|
case ev.MatchesPressOrRepeat("backspace"):
|
||||||
|
ev.Handled = true
|
||||||
|
if h.palette_query != "" {
|
||||||
|
g := wcswidth.SplitIntoGraphemes(h.palette_query)
|
||||||
|
h.palette_query = strings.Join(g[:len(g)-1], "")
|
||||||
|
h.update_palette()
|
||||||
|
h.draw_screen()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// open_selected opens the selected file in the requested window type. The
|
||||||
|
// window type is a launch type, so "tab" and "window" both work.
|
||||||
|
func (h *Handler) open_selected(window_type string) {
|
||||||
|
e, ok := h.selected_entry()
|
||||||
|
if !ok {
|
||||||
|
h.lp.Beep()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if e.IsDir {
|
||||||
h.enter_dir()
|
h.enter_dir()
|
||||||
case "cd_here":
|
return
|
||||||
h.change_shell_dir()
|
}
|
||||||
case "toggle_hidden":
|
h.open_file_in(entry_path(h.cwd, e.Name), window_type)
|
||||||
h.show_hidden = !h.show_hidden
|
}
|
||||||
h.reload()
|
|
||||||
case "create_file":
|
func (h *Handler) draw_palette_list(width, list_start, list_height int) {
|
||||||
h.start_input(input_create_file)
|
if len(h.palette) == 0 {
|
||||||
case "create_dir":
|
return
|
||||||
h.start_input(input_create_dir)
|
}
|
||||||
case "rename":
|
offset := 0
|
||||||
h.start_rename()
|
if h.palette_selected >= list_height {
|
||||||
case "delete":
|
offset = h.palette_selected - list_height + 1
|
||||||
h.start_delete()
|
}
|
||||||
case "copy_path":
|
for row := 0; row < list_height; row++ {
|
||||||
h.copy_selected_path()
|
idx := offset + row
|
||||||
case "refresh":
|
if idx >= len(h.palette) {
|
||||||
h.reload()
|
break
|
||||||
|
}
|
||||||
|
a := h.palette[idx]
|
||||||
|
text := truncate_to_width(fmt.Sprintf("%-18s %s", a.Name, a.Description), width-2)
|
||||||
|
h.lp.MoveCursorTo(1, list_start+row)
|
||||||
|
if idx == h.palette_selected {
|
||||||
|
text += strings.Repeat(" ", max(0, width-wcswidth.Stringwidth(text)))
|
||||||
|
h.lp.QueueWriteString(h.lp.SprintStyled("reverse", text))
|
||||||
|
} else {
|
||||||
|
h.lp.QueueWriteString(" " + text)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) handle_input_key(ev *loop.KeyEvent) error {
|
func (h *Handler) handle_input_key(ev *loop.KeyEvent) error {
|
||||||
|
if h.input_purpose == input_search {
|
||||||
|
return h.handle_search_key(ev)
|
||||||
|
}
|
||||||
switch {
|
switch {
|
||||||
case ev.MatchesPressOrRepeat("escape"):
|
case ev.MatchesPressOrRepeat("escape"):
|
||||||
ev.Handled = true
|
ev.Handled = true
|
||||||
@@ -558,6 +888,9 @@ func (h *Handler) on_key_event(ev *loop.KeyEvent) error {
|
|||||||
if h.input_purpose != input_none {
|
if h.input_purpose != input_none {
|
||||||
return h.handle_input_key(ev)
|
return h.handle_input_key(ev)
|
||||||
}
|
}
|
||||||
|
if h.palette_open {
|
||||||
|
return h.handle_palette_key(ev)
|
||||||
|
}
|
||||||
if h.confirm_name != "" {
|
if h.confirm_name != "" {
|
||||||
return h.handle_confirm_key(ev)
|
return h.handle_confirm_key(ev)
|
||||||
}
|
}
|
||||||
@@ -611,8 +944,20 @@ func (h *Handler) on_text(text string, from_key_event bool, in_bracketed_paste b
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if h.palette_open {
|
||||||
|
h.palette_query += text
|
||||||
|
h.update_palette()
|
||||||
|
h.draw_screen()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
if h.input_purpose != input_none {
|
if h.input_purpose != input_none {
|
||||||
h.text_buffer += text
|
h.text_buffer += text
|
||||||
|
if h.input_purpose == input_search {
|
||||||
|
h.search_query = h.text_buffer
|
||||||
|
if err := h.apply_search(); err != nil {
|
||||||
|
h.message = err.Error()
|
||||||
|
}
|
||||||
|
}
|
||||||
h.draw_screen()
|
h.draw_screen()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@@ -680,6 +1025,8 @@ func (h *Handler) input_prompt() string {
|
|||||||
return "New directory:"
|
return "New directory:"
|
||||||
case input_rename:
|
case input_rename:
|
||||||
return fmt.Sprintf("Rename %s to:", h.rename_source)
|
return fmt.Sprintf("Rename %s to:", h.rename_source)
|
||||||
|
case input_search:
|
||||||
|
return "Search:"
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@@ -688,6 +1035,9 @@ func (h *Handler) status_text() string {
|
|||||||
if h.message != "" {
|
if h.message != "" {
|
||||||
return " " + truncate_to_width(h.message, max(1, int(h.screen_size.WidthCells)-1))
|
return " " + truncate_to_width(h.message, max(1, int(h.screen_size.WidthCells)-1))
|
||||||
}
|
}
|
||||||
|
if h.search_query != "" {
|
||||||
|
return fmt.Sprintf(" /%s %d matches", h.search_query, len(h.entries))
|
||||||
|
}
|
||||||
if len(h.entries) == 0 {
|
if len(h.entries) == 0 {
|
||||||
return " Empty directory"
|
return " Empty directory"
|
||||||
}
|
}
|
||||||
@@ -736,20 +1086,24 @@ func (h *Handler) draw_screen() {
|
|||||||
}
|
}
|
||||||
h.scroll = max(0, min(h.scroll, max(0, len(h.entries)-list_height)))
|
h.scroll = max(0, min(h.scroll, max(0, len(h.entries)-list_height)))
|
||||||
|
|
||||||
for row := 0; row < list_height; row++ {
|
if h.palette_open {
|
||||||
idx := h.scroll + row
|
h.draw_palette_list(width, list_start, list_height)
|
||||||
if idx >= len(h.entries) {
|
} else {
|
||||||
break
|
for row := 0; row < list_height; row++ {
|
||||||
}
|
idx := h.scroll + row
|
||||||
e := h.entries[idx]
|
if idx >= len(h.entries) {
|
||||||
h.lp.MoveCursorTo(1, list_start+row)
|
break
|
||||||
switch {
|
}
|
||||||
case idx == h.selected:
|
e := h.entries[idx]
|
||||||
h.lp.QueueWriteString(h.lp.SprintStyled("reverse", h.padded_entry_text(e, width)))
|
h.lp.MoveCursorTo(1, list_start+row)
|
||||||
case e.IsDir:
|
switch {
|
||||||
h.lp.QueueWriteString(h.lp.SprintStyled("fg=bright-blue", h.entry_text(e, width)))
|
case idx == h.selected:
|
||||||
default:
|
h.lp.QueueWriteString(h.lp.SprintStyled("reverse", h.padded_entry_text(e, width)))
|
||||||
h.lp.QueueWriteString(h.entry_text(e, width))
|
case e.IsDir:
|
||||||
|
h.lp.QueueWriteString(h.lp.SprintStyled("fg=bright-blue", h.entry_text(e, width)))
|
||||||
|
default:
|
||||||
|
h.lp.QueueWriteString(h.entry_text(e, width))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -758,6 +1112,12 @@ func (h *Handler) draw_screen() {
|
|||||||
|
|
||||||
h.lp.MoveCursorTo(1, height)
|
h.lp.MoveCursorTo(1, height)
|
||||||
switch {
|
switch {
|
||||||
|
case h.palette_open:
|
||||||
|
prompt := "Actions: "
|
||||||
|
h.lp.SetCursorVisible(true)
|
||||||
|
h.lp.QueueWriteString(h.lp.SprintStyled("fg=bright-yellow", prompt))
|
||||||
|
h.lp.QueueWriteString(h.palette_query)
|
||||||
|
h.lp.MoveCursorTo(1+wcswidth.Stringwidth(prompt+h.palette_query), height)
|
||||||
case h.input_purpose != input_none:
|
case h.input_purpose != input_none:
|
||||||
prompt := h.input_prompt() + " "
|
prompt := h.input_prompt() + " "
|
||||||
h.lp.QueueWriteString(h.lp.SprintStyled("fg=bright-yellow", prompt))
|
h.lp.QueueWriteString(h.lp.SprintStyled("fg=bright-yellow", prompt))
|
||||||
@@ -769,7 +1129,7 @@ func (h *Handler) draw_screen() {
|
|||||||
h.lp.QueueWriteString(h.lp.SprintStyled("bold fg=bright-red", fmt.Sprintf("Delete %s? [y/N] ", h.confirm_name)))
|
h.lp.QueueWriteString(h.lp.SprintStyled("bold fg=bright-red", fmt.Sprintf("Delete %s? [y/N] ", h.confirm_name)))
|
||||||
default:
|
default:
|
||||||
h.lp.SetCursorVisible(false)
|
h.lp.SetCursorVisible(false)
|
||||||
hints := "Enter: cd/open ->: enter <-: up .: hidden n: new file r: rename d: delete y: copy q: quit"
|
hints := "Enter: cd/open /: search :: actions ->: enter <-: up .: hidden n: new file r: rename d: delete y: copy q: quit"
|
||||||
h.lp.QueueWriteString(h.lp.SprintStyled("dim", truncate_to_width(hints, width)))
|
h.lp.QueueWriteString(h.lp.SprintStyled("dim", truncate_to_width(hints, width)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,7 +41,8 @@ opt(
|
|||||||
long_text="""
|
long_text="""
|
||||||
How to order entries. :code:`name` lists directories first, then files, both
|
How to order entries. :code:`name` lists directories first, then files, both
|
||||||
sorted by name ignoring case. :code:`mtime` lists directories first, then files
|
sorted by name ignoring case. :code:`mtime` lists directories first, then files
|
||||||
sorted by last modification time, most recent first.
|
sorted by last modification time, most recent first. :code:`size` lists
|
||||||
|
directories first, then files sorted by size, largest first.
|
||||||
""",
|
""",
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -127,6 +128,7 @@ map('Change shell directory to the directory being browsed', 'cd_here --allow-fa
|
|||||||
map('Toggle display of hidden entries', 'toggle_hidden --allow-fallback=shifted,ascii . toggle_hidden')
|
map('Toggle display of hidden entries', 'toggle_hidden --allow-fallback=shifted,ascii . toggle_hidden')
|
||||||
map('Create a new file', 'create_file --allow-fallback=shifted,ascii n create_file')
|
map('Create a new file', 'create_file --allow-fallback=shifted,ascii n create_file')
|
||||||
map('Create a new directory', 'create_dir shift+n create_dir')
|
map('Create a new directory', 'create_dir shift+n create_dir')
|
||||||
|
map('Create a new directory', 'create_dir --allow-fallback=shifted,ascii a create_dir')
|
||||||
map('Rename the selected entry', 'rename --allow-fallback=shifted,ascii r rename')
|
map('Rename the selected entry', 'rename --allow-fallback=shifted,ascii r rename')
|
||||||
map(
|
map(
|
||||||
'Delete the selected entry',
|
'Delete the selected entry',
|
||||||
@@ -138,6 +140,26 @@ confirmation. Directories are never deleted recursively.
|
|||||||
)
|
)
|
||||||
map('Copy the path of the selection to the clipboard', 'copy_path --allow-fallback=shifted,ascii y copy_path')
|
map('Copy the path of the selection to the clipboard', 'copy_path --allow-fallback=shifted,ascii y copy_path')
|
||||||
map('Reload the current directory', 'refresh --allow-fallback=shifted,ascii ctrl+r refresh')
|
map('Reload the current directory', 'refresh --allow-fallback=shifted,ascii ctrl+r refresh')
|
||||||
|
map(
|
||||||
|
'Filter the listing with a fuzzy query',
|
||||||
|
'search --allow-fallback=shifted,ascii / search',
|
||||||
|
long_text="""
|
||||||
|
Filters the listing as text is typed, using the same fuzzy matcher as the
|
||||||
|
command palette. :kbd:`Entrée` opens the selection, :kbd:`Échap` clears the
|
||||||
|
query and restores the full listing.
|
||||||
|
""",
|
||||||
|
)
|
||||||
|
map(
|
||||||
|
'List and run the actions of the panel',
|
||||||
|
'palette --allow-fallback=shifted,ascii : palette',
|
||||||
|
long_text="""
|
||||||
|
Lists every action of the panel with its description, filtered by a fuzzy query,
|
||||||
|
and runs the selected one with :kbd:`Entrée`. The same actions can be bound to
|
||||||
|
dedicated shortcuts in :file:`file-manager.conf`.
|
||||||
|
""",
|
||||||
|
)
|
||||||
|
map('Open the selected file in a new tab', 'open_tab --allow-fallback=shifted,ascii o open_tab')
|
||||||
|
map('Open the selected file in a new OS window', 'open_window --allow-fallback=shifted,ascii t open_window')
|
||||||
|
|
||||||
egr()
|
egr()
|
||||||
|
|
||||||
|
|||||||
@@ -3,9 +3,11 @@
|
|||||||
package file_manager
|
package file_manager
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestSortEntriesDirectoriesFirstThenByName(t *testing.T) {
|
func TestSortEntriesDirectoriesFirstThenByName(t *testing.T) {
|
||||||
@@ -213,6 +215,132 @@ func TestResultPayload(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSortEntriesBySize(t *testing.T) {
|
||||||
|
entries := []Entry{
|
||||||
|
{Name: "small.txt", Size: 10},
|
||||||
|
{Name: "large.txt", Size: 300},
|
||||||
|
{Name: "medium.txt", Size: 20},
|
||||||
|
{Name: "dir", IsDir: true, Size: 1},
|
||||||
|
}
|
||||||
|
sort_entries(entries, "size")
|
||||||
|
expected := []string{"dir", "large.txt", "medium.txt", "small.txt"}
|
||||||
|
for i, name := range expected {
|
||||||
|
if entries[i].Name != name {
|
||||||
|
t.Fatalf("wrong order at %d: wanted %s got %s", i, name, entries[i].Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilterEntries(t *testing.T) {
|
||||||
|
entries := []Entry{
|
||||||
|
{Name: "alpha.txt"},
|
||||||
|
{Name: "beta.txt"},
|
||||||
|
{Name: "zeta.md"},
|
||||||
|
{Name: "src", IsDir: true},
|
||||||
|
}
|
||||||
|
all, err := filter_entries(entries, "")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(all) != len(entries) {
|
||||||
|
t.Fatalf("an empty query must keep every entry, got %#v", all)
|
||||||
|
}
|
||||||
|
|
||||||
|
matches, err := filter_entries(entries, "bt")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(matches) != 1 || matches[0].Name != "beta.txt" {
|
||||||
|
t.Fatalf("unexpected matches for \"bt\": %#v", matches)
|
||||||
|
}
|
||||||
|
|
||||||
|
dirs, err := filter_entries(entries, "src/")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(dirs) != 1 || !dirs[0].IsDir {
|
||||||
|
t.Fatalf("a trailing slash must only match directories, got %#v", dirs)
|
||||||
|
}
|
||||||
|
|
||||||
|
none, err := filter_entries(entries, "zzz")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(none) != 0 {
|
||||||
|
t.Fatalf("a query that matches nothing must return nothing, got %#v", none)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilterEntriesLargeDirectory(t *testing.T) {
|
||||||
|
entries := make([]Entry, 10_000)
|
||||||
|
for i := range entries {
|
||||||
|
entries[i] = Entry{Name: fmt.Sprintf("file-%05d.txt", i)}
|
||||||
|
}
|
||||||
|
start := time.Now()
|
||||||
|
matches, err := filter_entries(entries, "file-099")
|
||||||
|
elapsed := time.Since(start)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(matches) == 0 {
|
||||||
|
t.Fatal("expected matches in a large directory")
|
||||||
|
}
|
||||||
|
if elapsed > 100*time.Millisecond {
|
||||||
|
t.Fatalf("filtering 10000 entries took %s, the limit is 100ms", elapsed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFilterActions(t *testing.T) {
|
||||||
|
all, err := filter_actions("")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(all) != len(action_catalog) {
|
||||||
|
t.Fatalf("an empty query must list the whole catalog, got %d of %d", len(all), len(action_catalog))
|
||||||
|
}
|
||||||
|
matches, err := filter_actions("delete")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(matches) == 0 || matches[0].Name != "delete" {
|
||||||
|
t.Fatalf("the palette must rank the delete action first, got %#v", matches)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestActionCatalogMatchesHandlers(t *testing.T) {
|
||||||
|
seen := make(map[string]bool, len(action_catalog))
|
||||||
|
for _, a := range action_catalog {
|
||||||
|
if a.Name == "" || a.Description == "" {
|
||||||
|
t.Fatalf("actions need a name and a description: %#v", a)
|
||||||
|
}
|
||||||
|
if seen[a.Name] {
|
||||||
|
t.Fatalf("duplicate action %q in the catalog", a.Name)
|
||||||
|
}
|
||||||
|
seen[a.Name] = true
|
||||||
|
if _, ok := action_handlers[a.Name]; !ok {
|
||||||
|
t.Fatalf("action %q is described but has no handler", a.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for name := range action_handlers {
|
||||||
|
if !seen[name] {
|
||||||
|
t.Fatalf("action %q has a handler but no description in the palette", name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestEditArgsInWindow(t *testing.T) {
|
||||||
|
got := edit_args_in("/tmp/a.py", "window")
|
||||||
|
want := []string{"edit-in-kitty", "--type=window", "/tmp/a.py"}
|
||||||
|
if len(got) != len(want) {
|
||||||
|
t.Fatalf("expected %#v, got %#v", want, got)
|
||||||
|
}
|
||||||
|
for i := range want {
|
||||||
|
if got[i] != want[i] {
|
||||||
|
t.Fatalf("expected %#v, got %#v", want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestTruncateToWidth(t *testing.T) {
|
func TestTruncateToWidth(t *testing.T) {
|
||||||
if got := truncate_to_width("abcdef", 3); got != "ab\u2026" {
|
if got := truncate_to_width("abcdef", 3); got != "ab\u2026" {
|
||||||
t.Fatalf("unexpected truncation %q", got)
|
t.Fatalf("unexpected truncation %q", got)
|
||||||
|
|||||||
Reference in new issue
Block a user