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
@@ -3,9 +3,11 @@
|
||||
package file_manager
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
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) {
|
||||
if got := truncate_to_width("abcdef", 3); got != "ab\u2026" {
|
||||
t.Fatalf("unexpected truncation %q", got)
|
||||
|
||||
Reference in new issue
Block a user