This commit is contained in:
Kovid Goyal 2026-10-13 08:21:05 +05:30
parent 8aaa161fb8
commit 163e271250
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 117 additions and 3 deletions

View file

@ -273,6 +273,9 @@ Detailed list of changes
- choose-files kitten: Fix handling of symlinks to directories in directory
selection modes and when using file filters
- choose-files kitten: Fix switching between file filters made up of multiple
patterns that share the same first pattern not updating the results
- Fix a non-zero :opt:`input_delay` painting once before delayed program output is parsed and again when it is parsed, so it could use more CPU than :opt:`input_delay` ``0``
- Linux/BSD: Detect MIME types for :doc:`open actions <open_actions>` and

View file

@ -3,6 +3,7 @@ package choose_files
import (
"fmt"
"path/filepath"
"slices"
"strings"
"github.com/kovidgoyal/kitty/tools/utils"
@ -13,14 +14,33 @@ var _ = fmt.Print
type Filter struct {
Name, Type, Pattern string
Match func(filename string) bool
// The individual filters making up a combined filter, Type and Pattern are empty for combined filters
sub_filters []Filter
}
func (f Filter) String() string {
if len(f.sub_filters) > 0 {
return strings.Join(utils.Map(Filter.String, f.sub_filters), ", ")
}
return fmt.Sprintf("%s:%s:%s", f.Type, f.Pattern, f.Name)
}
// The sorted, de-duplicated type:pattern specs of the matchers making up this filter
func (f Filter) specs() []string {
if len(f.sub_filters) == 0 {
return []string{f.Type + ":" + f.Pattern}
}
ans := utils.Map(func(sf Filter) string { return sf.Type + ":" + sf.Pattern }, f.sub_filters)
slices.Sort(ans)
return slices.Compact(ans)
}
// Whether the two filters match the same set of files, regardless of their names
func (f Filter) Equal(other Filter) bool {
return f.Type == other.Type && f.Pattern == other.Pattern
if f.Match == nil || other.Match == nil {
return f.Match == nil && other.Match == nil
}
return slices.Equal(f.specs(), other.specs())
}
func NewFilter(spec string) (*Filter, error) {
@ -63,13 +83,22 @@ func CombinedFilter(filters ...Filter) Filter {
if len(filters) == 0 {
return Filter{}
}
var flattened []Filter
for _, f := range filters {
if f.Match == nil {
return f
}
if len(f.sub_filters) > 0 {
flattened = append(flattened, f.sub_filters...)
} else {
flattened = append(flattened, f)
}
}
ans := filters[0]
matchers := utils.Map(func(f Filter) func(filename string) bool { return f.Match }, filters)
if len(flattened) == 1 {
return flattened[0]
}
ans := Filter{Name: filters[0].Name, sub_filters: flattened}
matchers := utils.Map(func(f Filter) func(filename string) bool { return f.Match }, flattened)
ans.Match = func(filename string) bool {
for _, m := range matchers {
if m(filename) {

View file

@ -243,6 +243,88 @@ func TestChooseFilesScoring(t *testing.T) {
af("glob:*:All", "x", "y", "a", "b", "c.png", "x/s", "x/1", "x/2", "x/3", "y/3", "y/4", "y/5", "x/s/m", "x/s/n")
}
func combined_filter(t *testing.T, specs ...string) Filter {
t.Helper()
filters := make([]Filter, 0, len(specs))
for _, spec := range specs {
f, err := NewFilter(spec)
if err != nil {
t.Fatal(err)
}
filters = append(filters, *f)
}
return CombinedFilter(filters...)
}
func TestChooseFilesFilterEquality(t *testing.T) {
f := func(specs ...string) Filter { return combined_filter(t, specs...) }
for _, tc := range []struct {
a, b Filter
equal bool
}{
{f("glob:*.txt:A"), f("glob:*.txt:B"), true},
{f("glob:*.txt:A"), f("mime:*.txt:A"), false},
{f("glob:*.txt:A", "glob:*.jpg:A"), f("glob:*.jpg:B", "glob:*.txt:B"), true},
{f("glob:*.txt:A", "glob:*.jpg:A"), f("glob:*.txt:A", "glob:*.png:A"), false},
{f("glob:*.txt:A", "glob:*.jpg:A"), f("glob:*.txt:A"), false},
{f("glob:*.txt:A", "glob:*.txt:A"), f("glob:*.txt:A"), true},
{f("mime:image/*:A", "glob:*.mkv:A"), f("mime:image/*:A", "mime:*.mkv:A"), false},
{f("glob:*.txt:A", "glob:*.jpg:A"), CombinedFilter(f("glob:*.jpg:A"), f("glob:*.txt:A", "glob:*.jpg:A")), true},
{f("glob:*:A"), Filter{}, true},
{f("glob:*:A"), f("glob:*.txt:A"), false},
} {
if tc.a.Equal(tc.b) != tc.equal || tc.b.Equal(tc.a) != tc.equal {
t.Errorf("expected Equal(%s, %s) to be %v", tc.a, tc.b, tc.equal)
}
}
if s := f("glob:*.txt:A", "mime:image/*:A").String(); s != "glob:*.txt:A, mime:image/*:A" {
t.Errorf("unexpected string representation of combined filter: %s", s)
}
}
func TestChooseFilesCombinedFilters(t *testing.T) {
root := t.TempDir()
for _, name := range []string{"a.txt", "b.jpg", "c.png"} {
if err := os.WriteFile(filepath.Join(root, name), []byte("data"), 0o600); err != nil {
t.Fatal(err)
}
}
first := combined_filter(t, "glob:*.txt:First", "glob:*.jpg:First")
second := combined_filter(t, "glob:*.txt:Second", "glob:*.png:Second")
var scan_err error
s := NewFileSystemScorer(root, "", first, false, func(err error, complete bool) {
if err != nil {
scan_err = err
}
})
h := Handler{state: State{
current_dir: root, current_filter: "First", filter_map: map[string]Filter{"First": first, "Second": second},
}}
h.result_manager = &ResultManager{settings: &h.state, scorer: s}
check := func(expected ...string) {
t.Helper()
s.current_worker_wait.Wait()
if scan_err != nil {
t.Fatal(scan_err)
}
results, _ := s.Results()
var actual []string
for _, item := range results.RenderedMatches(CollectionIndex{}, -1) {
actual = append(actual, item.text)
}
slices.Sort(actual)
if diff := cmp.Diff(expected, actual); diff != "" {
t.Fatalf("incorrect results for filter %s:\n%s", h.state.current_filter, diff)
}
}
s.Start()
check("a.txt", "b.jpg")
h.set_filter("Second")
check("a.txt", "c.png")
h.set_filter("First")
check("a.txt", "b.jpg")
}
func TestChooseFilesDirectorySymlinks(t *testing.T) {
root := t.TempDir()
if err := os.Mkdir(filepath.Join(root, "real-dir"), 0o700); err != nil {