From 757bdd6967ae580e48b67ad59a56c0b89f70d9ca Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 16 Aug 2024 19:51:20 +0200 Subject: [PATCH] Update --- go.mod | 6 +++++- go.sum | 12 ++++++++++-- main.go | 56 +++++++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 56 insertions(+), 18 deletions(-) diff --git a/go.mod b/go.mod index 757f498..67cd216 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/charmbracelet/bubbles v0.18.0 github.com/charmbracelet/bubbletea v0.27.0 github.com/charmbracelet/lipgloss v0.12.1 + golang.design/x/clipboard v0.7.0 ) require ( @@ -25,7 +26,10 @@ require ( github.com/muesli/termenv v0.15.2 // indirect github.com/rivo/uniseg v0.4.7 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect + golang.org/x/exp/shiny v0.0.0-20240808152545-0cdaa3abc0fa // indirect + golang.org/x/image v0.14.0 // indirect + golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a // indirect golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.24.0 // indirect - golang.org/x/text v0.3.8 // indirect + golang.org/x/text v0.14.0 // indirect ) diff --git a/go.sum b/go.sum index 4e3f0e0..40eecf7 100644 --- a/go.sum +++ b/go.sum @@ -37,13 +37,21 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= +golang.design/x/clipboard v0.7.0 h1:4Je8M/ys9AJumVnl8m+rZnIvstSnYj1fvzqYrU3TXvo= +golang.design/x/clipboard v0.7.0/go.mod h1:PQIvqYO9GP29yINEfsEn5zSQKAz3UgXmZKzDA6dnq2E= golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= +golang.org/x/exp/shiny v0.0.0-20240808152545-0cdaa3abc0fa h1:Gl6It7AL4mZR4FMt/t+e9k3XzLmoopI886quj98OXGA= +golang.org/x/exp/shiny v0.0.0-20240808152545-0cdaa3abc0fa/go.mod h1:3F+MieQB7dRYLTmnncoFbb1crS5lfQoTfDgQy6K4N0o= +golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4= +golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE= +golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a h1:sYbmY3FwUWCBTodZL1S3JUuOvaW6kM2o+clDzzDNBWg= +golang.org/x/mobile v0.0.0-20231127183840-76ac6878050a/go.mod h1:Ede7gF0KGoHlj822RtphAHK1jLdrcuRBZg0sF1Q+SPc= golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg= golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= -golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= diff --git a/main.go b/main.go index 9c92ac8..ce14963 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,12 @@ package main import ( + "encoding/base64" "fmt" "os" + "golang.design/x/clipboard" + "github.com/charmbracelet/bubbles/help" "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/textarea" @@ -11,10 +14,6 @@ import ( "github.com/charmbracelet/lipgloss" ) -const ( - helpHeight = 5 -) - var ( cursorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("212")) @@ -40,7 +39,7 @@ var ( ) type keymap = struct { - next, prev, add, remove, quit key.Binding + next, copy, clear, quit key.Binding } func newTextarea() textarea.Model { @@ -59,6 +58,8 @@ func newTextarea() textarea.Model { t.KeyMap.DeleteWordBackward.SetEnabled(false) t.KeyMap.LineNext = key.NewBinding(key.WithKeys("down")) t.KeyMap.LinePrevious = key.NewBinding(key.WithKeys("up")) + t.CharLimit = (1024 * 8) * 8 + t.MaxHeight = 1024 * 8 t.Blur() return t } @@ -86,6 +87,12 @@ func newModel() model { key.WithKeys("esc", "ctrl+c"), key.WithHelp("esc", "quit"), ), + copy: key.NewBinding( + key.WithKeys("ctrl+x"), + ), + clear: key.NewBinding( + key.WithKeys("ctrl+r"), + ), }, } m.decoded.Focus() @@ -114,6 +121,15 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { cmds = append(cmds, m.encoded.Focus()) m.decoded.Blur() } + case key.Matches(msg, m.keymap.copy): + if m.encoded.Focused() { + clipboard.Write(clipboard.FmtText, []byte(m.encoded.Value())) + } else if m.decoded.Focused() { + clipboard.Write(clipboard.FmtText, []byte(m.decoded.Value())) + } + case key.Matches(msg, m.keymap.clear): + m.encoded.SetValue("") + m.decoded.SetValue("") } case tea.WindowSizeMsg: m.height = msg.Height @@ -122,42 +138,52 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.sizeInputs() - // Update all textareas + if m.encoded.Focused() { + decoded, err := base64.StdEncoding.DecodeString(m.encoded.Value()) + if err != nil { + // log.Printf("failed to decode: %v", err) + } else { + m.decoded.SetValue(string(decoded)) + } + } else if m.decoded.Focused() { + encoded := base64.StdEncoding.EncodeToString([]byte(m.decoded.Value())) + m.encoded.SetValue(string(encoded)) + } + encoded, cmd := m.encoded.Update(msg) cmds = append(cmds, cmd) m.encoded = encoded decoded, cmd := m.decoded.Update(msg) - cmds = append(cmds, cmd) + cmds = append(cmds, cmd) m.decoded = decoded return m, tea.Batch(cmds...) } func (m *model) sizeInputs() { - m.encoded.SetHeight(m.height-5) - m.decoded.SetHeight(m.height-5) - m.encoded.SetWidth(m.width/2-5) - m.decoded.SetCursor(m.width/2-5) + m.encoded.SetHeight(m.height - 5) + m.decoded.SetHeight(m.height - 5) + m.encoded.SetWidth(m.width/2 - 5) + m.decoded.SetWidth(m.width/2 - 5) } func (m model) View() string { help := m.help.ShortHelpView([]key.Binding{ m.keymap.next, - m.keymap.prev, - m.keymap.add, - m.keymap.remove, + m.keymap.copy, m.keymap.quit, }) var views []string - views = append(views, m.encoded.View()) views = append(views, m.decoded.View()) + views = append(views, m.encoded.View()) return lipgloss.JoinHorizontal(lipgloss.Top, views...) + "\n" + help } func main() { + clipboard.Init() if _, err := tea.NewProgram(newModel(), tea.WithAltScreen()).Run(); err != nil { fmt.Println("Error while running program:", err) os.Exit(1)