From fa114e43a50c9fa67bed1e1038431c39fbd8ee7d Mon Sep 17 00:00:00 2001 From: cychester <40726077+cyc4188@users.noreply.github.com> Date: Fri, 15 Sep 2023 16:15:21 +0800 Subject: [PATCH] Feat: yank (#110) * fix: incorrect icons * feat: yank path to clipboard with y * update: readme * update: clipboard lib --- README.md | 1 + go.mod | 1 + go.sum | 3 ++- walk.go | 25 +++++++++++++++++++++---- 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 38c4cc4..0f321c0 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/go.mod b/go.mod index 4f1a82d..42471b0 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index e6a72e9..b7893f5 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/walk.go b/walk.go index e45476b..8be5524 100644 --- a/walk.go +++ b/walk.go @@ -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" @@ -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() { @@ -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 { @@ -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() @@ -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. @@ -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, @@ -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()