Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(filepicker): add GetFileUnderCursor #704

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
36 changes: 36 additions & 0 deletions filepicker/filepicker.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package filepicker

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -451,6 +452,41 @@ func (m Model) DidSelectDisabledFile(msg tea.Msg) (bool, string) {
return false, ""
}

func (m Model) GetFileUnderCursor() (string, error) {
if len(m.files) == 0 {
return "", errors.New("no files")
}

if m.selected < 0 || m.selected >= len(m.files) {
return "", errors.New("invalid selection index")
}

f := m.files[m.selected]
info, err := f.Info()
if err != nil {
return "", err
}
isSymlink := info.Mode()&os.ModeSymlink != 0
isDir := f.IsDir()

if isSymlink {
symlinkPath, _ := filepath.EvalSymlinks(filepath.Join(m.CurrentDirectory, f.Name()))
info, err := os.Stat(symlinkPath)
if err != nil {
return "", err
}
if info.IsDir() {
isDir = true
}
}

if isDir {
return "", errors.New("cannot select a directory")
}

return filepath.Join(m.CurrentDirectory, f.Name()), nil
}

func (m Model) didSelectFile(msg tea.Msg) (bool, string) {
if len(m.files) == 0 {
return false, ""
Expand Down