Skip to content

Commit

Permalink
Feat: yank (antonmedv#110)
Browse files Browse the repository at this point in the history
* fix: incorrect icons

* feat: yank path to clipboard with y

* update: readme

* update: clipboard lib
  • Loading branch information
cyc4188 authored and ardnew committed Mar 26, 2024
1 parent 142cd4b commit fa114e4
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Now use `lk` command to start walking.
| `Ctrl+c` | Exit without cd |
| `/` | Fuzzy search |
| `dd` | Delete file or dir |
| `y` | yank current dir |

The `EDITOR` or `WALK_EDITOR` environment variable used for opening files from
the walk.
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/antonmedv/walk
go 1.17

require (
github.com/antonmedv/clipboard v1.0.1
github.com/charmbracelet/bubbles v0.16.1
github.com/charmbracelet/bubbletea v0.24.2
github.com/charmbracelet/lipgloss v0.8.0
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/antonmedv/clipboard v1.0.1 h1:z9rRBhSKt4lDb6uNcMykUmNbspk/6v07JeiTaOfYYOY=
github.com/antonmedv/clipboard v1.0.1/go.mod h1:3jcOUCdraVHehZaOsMaJZoE92MxURt5fovC1gDAiZ2s=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/bubbles v0.16.1 h1:6uzpAAaT9ZqKssntbvZMlksWHruQLNxg49H5WdeuYSY=
Expand Down
25 changes: 21 additions & 4 deletions walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"
"unicode/utf8"

"github.com/antonmedv/clipboard"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
Expand Down Expand Up @@ -63,6 +64,7 @@ var (
keyPreview = key.NewBinding(key.WithKeys(" "))
keyDelete = key.NewBinding(key.WithKeys("d"))
keyUndo = key.NewBinding(key.WithKeys("u"))
keyYank = key.NewBinding(key.WithKeys("y"))
)

func main() {
Expand Down Expand Up @@ -127,6 +129,7 @@ type model struct {
previewContent string // Content of preview.
deleteCurrentFile bool // Whether to delete current file.
toBeDeleted []toDelete // Map of files to be deleted.
yankSuccess bool // Show yank info
}

type position struct {
Expand Down Expand Up @@ -352,9 +355,16 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.previewContent = ""
return m, nil
}

case key.Matches(msg, keyYank):
// copy path to clipboard
clipboard.WriteAll(m.path)
m.yankSuccess = true
return m, nil
} // End of switch statement for key presses.

m.deleteCurrentFile = false
m.yankSuccess = false
m.updateOffset()
m.saveCursorPosition()

Expand Down Expand Up @@ -471,14 +481,14 @@ func (m *Model) View() string {
if barLen > outputWidth {
location = location[min(barLen-outputWidth, len(location)):]
}
bar := bar.Render(location) + search.Render(filter)
barStr := bar.Render(location) + search.Render(filter)

main := bar + "\n" + Join(output, "\n")
main := barStr + "\n" + Join(output, "\n")

if m.err != nil {
main = bar + "\n" + warning.Render(m.err.Error())
main = barStr + "\n" + warning.Render(m.err.Error())
} else if len(m.files) == 0 {
main = bar + "\n" + warning.Render("No files")
main = barStr + "\n" + warning.Render("No files")
}

// Delete bar.
Expand All @@ -489,6 +499,12 @@ func (m *Model) View() string {
main += "\n" + danger.Render(deleteBar)
}

// Yank success.
if m.yankSuccess {
yankBar := fmt.Sprintf("yanked path to clipboard: %v", m.path)
main += "\n" + bar.Render(yankBar)
}

if m.previewMode {
return lipgloss.JoinHorizontal(
lipgloss.Top,
Expand Down Expand Up @@ -881,6 +897,7 @@ func usage() {
put(" Ctrl+c\tExit without cd")
put(" /\tFuzzy search")
put(" dd\tDelete file or dir")
put(" y\tYank current directory path to clipboard")
put("\n Flags:\n")
put(" --icons\tdisplay icons")
_ = w.Flush()
Expand Down

0 comments on commit fa114e4

Please sign in to comment.