mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-11-18 14:18:16 +00:00
choose-files: Handle symlinks to directories
Recognize the existing directory-symlink marker when scanning, filtering and deciding which results can be selected. Resolve symlinks before file filters and cover directory selection and filtered navigation.
This commit is contained in:
parent
b78a6aa30f
commit
38ed84edf5
4 changed files with 91 additions and 9 deletions
|
|
@ -270,6 +270,9 @@ Detailed list of changes
|
|||
|
||||
- choose-files kitten: Various edge case crashes and deadlocks fixed
|
||||
|
||||
- choose-files kitten: Fix handling of symlinks to directories in directory
|
||||
selection modes and when using file filters
|
||||
|
||||
- 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
|
||||
|
|
|
|||
|
|
@ -400,7 +400,8 @@ func (s *State) CanSelect(r *ResultItem) bool {
|
|||
if s.mode == SELECT_ANY {
|
||||
return true
|
||||
}
|
||||
return utils.IfElse(s.OnlyDirs(), r.ftype.IsDir(), !r.ftype.IsDir())
|
||||
is_dir := is_directory(r.ftype)
|
||||
return utils.IfElse(s.OnlyDirs(), is_dir, !is_dir)
|
||||
}
|
||||
|
||||
func (h *Handler) toggle_selection_at(idx CollectionIndex) bool {
|
||||
|
|
|
|||
|
|
@ -164,6 +164,10 @@ type sortable_dir_entry struct {
|
|||
|
||||
const SymlinkToDir = 1
|
||||
|
||||
func is_directory(ftype fs.FileMode) bool {
|
||||
return ftype.IsDir() || ftype&SymlinkToDir != 0
|
||||
}
|
||||
|
||||
// lowercase a string into a pre-existing byte buffer with speedups for ASCII
|
||||
func as_lower(s string, output []byte) int {
|
||||
limit := min(len(s), len(output))
|
||||
|
|
@ -301,7 +305,12 @@ func (fss *FileSystemScanner) worker() {
|
|||
for i, e := range entries {
|
||||
name := e.Name()
|
||||
ftype := e.Type()
|
||||
is_dir := ftype&fs.ModeDir != 0
|
||||
if ftype&fs.ModeSymlink != 0 {
|
||||
if st, serr := os.Stat(dir + name); serr == nil && st.IsDir() {
|
||||
ftype |= SymlinkToDir
|
||||
}
|
||||
}
|
||||
is_dir := is_directory(ftype)
|
||||
if !is_dir {
|
||||
switch name {
|
||||
case ".ignore":
|
||||
|
|
@ -321,11 +330,6 @@ func (fss *FileSystemScanner) worker() {
|
|||
continue
|
||||
}
|
||||
arena[i].name = name
|
||||
if ftype&fs.ModeSymlink != 0 {
|
||||
if st, serr := os.Stat(dir + arena[i].name); serr == nil && st.IsDir() {
|
||||
ftype |= SymlinkToDir
|
||||
}
|
||||
}
|
||||
arena[i].ftype = ftype
|
||||
if is_dir {
|
||||
arena[i].buf[0] = '0'
|
||||
|
|
@ -550,7 +554,7 @@ func (fss *FileSystemScorer) worker(on_results chan bool, worker_wait *sync.Wait
|
|||
if fss.only_dirs {
|
||||
rp = make([]*ResultItem, 0, len(results))
|
||||
for i, r := range results {
|
||||
if r.ftype.IsDir() {
|
||||
if is_directory(r.ftype) {
|
||||
rp = append(rp, &results[i])
|
||||
}
|
||||
}
|
||||
|
|
@ -563,7 +567,7 @@ func (fss *FileSystemScorer) worker(on_results chan bool, worker_wait *sync.Wait
|
|||
} else {
|
||||
rp = make([]*ResultItem, 0, len(results))
|
||||
for i, r := range results {
|
||||
if r.ftype.IsDir() || fss.filter.Match(filepath.Base(r.text)) {
|
||||
if is_directory(r.ftype) || fss.filter.Match(filepath.Base(r.text)) {
|
||||
rp = append(rp, &results[i])
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ import (
|
|||
"io/fs"
|
||||
"math/rand"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
|
@ -241,6 +243,78 @@ 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 TestChooseFilesDirectorySymlinks(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := os.Mkdir(filepath.Join(root, "real-dir"), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, name := range []string{"file.txt", "real-dir/nested.txt"} {
|
||||
if err := os.WriteFile(filepath.Join(root, name), []byte("data"), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for name, target := range map[string]string{"linked-dir": "real-dir", "linked-file.txt": "file.txt"} {
|
||||
if err := os.Symlink(target, filepath.Join(root, name)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
filter, err := NewFilter("glob:*.txt:Text files")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
mode Mode
|
||||
filter Filter
|
||||
want, selected []string
|
||||
}{
|
||||
{"directories", SELECT_SINGLE_DIR, Filter{},
|
||||
[]string{"linked-dir", "real-dir"}, []string{"linked-dir", "real-dir"}},
|
||||
{"file filter", SELECT_SINGLE_FILE, *filter,
|
||||
[]string{"file.txt", "linked-dir", "linked-file.txt", "real-dir", "real-dir/nested.txt"},
|
||||
[]string{"file.txt", "linked-file.txt", "real-dir/nested.txt"}},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
done := make(chan struct{})
|
||||
var scan_err error
|
||||
s := NewFileSystemScorer(root, "", tc.filter, tc.mode.OnlyDirs(), func(err error, complete bool) {
|
||||
if err != nil {
|
||||
scan_err = err
|
||||
}
|
||||
if complete {
|
||||
close(done)
|
||||
}
|
||||
})
|
||||
s.Start()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("file scan did not finish")
|
||||
}
|
||||
if scan_err != nil {
|
||||
t.Fatal(scan_err)
|
||||
}
|
||||
results, _ := s.Results()
|
||||
state := State{mode: tc.mode}
|
||||
var actual, selected []string
|
||||
for _, item := range results.RenderedMatches(CollectionIndex{}, -1) {
|
||||
actual = append(actual, item.text)
|
||||
if state.CanSelect(item) {
|
||||
selected = append(selected, item.text)
|
||||
}
|
||||
}
|
||||
slices.Sort(actual)
|
||||
slices.Sort(selected)
|
||||
if diff := cmp.Diff(tc.want, actual); diff != "" {
|
||||
t.Errorf("wrong choices:\n%s", diff)
|
||||
}
|
||||
if diff := cmp.Diff(tc.selected, selected); diff != "" {
|
||||
t.Errorf("wrong selectable choices:\n%s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSortedResults(t *testing.T) {
|
||||
r := NewSortedResults()
|
||||
idx := CollectionIndex{}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue