Skip to content

Commit

Permalink
Fix issue with a space in dir name; improve cd integration
Browse files Browse the repository at this point in the history
  • Loading branch information
antonmedv committed Dec 10, 2021
1 parent 19dcc70 commit 1010000
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ Put the next function into **~/.bashrc**:

```bash
function ll {
llama "$@" 2> /tmp/path
if [[ -d `cat /tmp/path` ]]; then
cd `cat /tmp/path`
fi
llama "$@" 2> /tmp/path && cd "$(cat /tmp/path)"
}
```

Expand Down
18 changes: 13 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func main() {
if err := p.Start(); err != nil {
die(err)
}
os.Exit(m.exitCode)
}

type model struct {
Expand All @@ -96,6 +97,7 @@ type model struct {
matchedIndexes []int // List of char found indexes.
prevName string // Base name of previous directory before "up".
findPrevName bool // On View(), set c&r to point to prevName.
exitCode int // Exit code.
}

type position struct {
Expand All @@ -119,7 +121,7 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Reset position history as c&r changes.
m.positions = make(map[string]position)
// Keep cursor at same place.
m.prevName = m.fileAtCursor().Name()
m.prevName = m.cursorFileName()
m.findPrevName = true
// Also, m.c&r no longer point to correct indexes.
m.c = 0
Expand Down Expand Up @@ -151,15 +153,17 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch keypress := msg.String(); keypress {
case "ctrl+c":
fmt.Println()
m.exitCode = 2
return m, tea.Quit

case "esc":
fmt.Println()
_, _ = fmt.Fprintln(os.Stderr, m.path)
m.exitCode = 0
return m, tea.Quit

case "enter":
newPath := filepath.Join(m.path, m.fileAtCursor().Name())
newPath := filepath.Join(m.path, m.cursorFileName())
if fi := fileInfo(newPath); fi.IsDir() {
// Enter subdirectory.
m.path = newPath
Expand All @@ -176,7 +180,7 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.status()
} else {
// Open file.
cmd := exec.Command(lookup([]string{"LLAMA_EDITOR", "EDITOR"}, "less"), filepath.Join(m.path, m.fileAtCursor().Name()))
cmd := exec.Command(lookup([]string{"LLAMA_EDITOR", "EDITOR"}, "less"), filepath.Join(m.path, m.cursorFileName()))
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
// Note: no Stderr as redirect `llama 2> /tmp/path` can be used.
Expand Down Expand Up @@ -448,8 +452,12 @@ func (m *model) saveCursorPosition() {
}
}

func (m *model) fileAtCursor() os.FileInfo {
return m.files[m.c*m.rows+m.r]
func (m *model) cursorFileName() string {
i := m.c*m.rows + m.r
if i < len(m.files) {
return m.files[i].Name()
}
return ""
}

func fileInfo(path string) os.FileInfo {
Expand Down

0 comments on commit 1010000

Please sign in to comment.