This commit is contained in:
2024-08-16 19:51:20 +02:00
parent e3459c783c
commit 757bdd6967
3 changed files with 56 additions and 18 deletions

6
go.mod
View File

@@ -6,6 +6,7 @@ require (
github.com/charmbracelet/bubbles v0.18.0 github.com/charmbracelet/bubbles v0.18.0
github.com/charmbracelet/bubbletea v0.27.0 github.com/charmbracelet/bubbletea v0.27.0
github.com/charmbracelet/lipgloss v0.12.1 github.com/charmbracelet/lipgloss v0.12.1
golang.design/x/clipboard v0.7.0
) )
require ( require (
@@ -25,7 +26,10 @@ require (
github.com/muesli/termenv v0.15.2 // indirect github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect github.com/rivo/uniseg v0.4.7 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // 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/sync v0.8.0 // indirect
golang.org/x/sys v0.24.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
) )

12
go.sum
View File

@@ -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/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 h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= 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 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= 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 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= 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.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.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 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 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.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=

54
main.go
View File

@@ -1,9 +1,12 @@
package main package main
import ( import (
"encoding/base64"
"fmt" "fmt"
"os" "os"
"golang.design/x/clipboard"
"github.com/charmbracelet/bubbles/help" "github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textarea" "github.com/charmbracelet/bubbles/textarea"
@@ -11,10 +14,6 @@ import (
"github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss"
) )
const (
helpHeight = 5
)
var ( var (
cursorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("212")) cursorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("212"))
@@ -40,7 +39,7 @@ var (
) )
type keymap = struct { type keymap = struct {
next, prev, add, remove, quit key.Binding next, copy, clear, quit key.Binding
} }
func newTextarea() textarea.Model { func newTextarea() textarea.Model {
@@ -59,6 +58,8 @@ func newTextarea() textarea.Model {
t.KeyMap.DeleteWordBackward.SetEnabled(false) t.KeyMap.DeleteWordBackward.SetEnabled(false)
t.KeyMap.LineNext = key.NewBinding(key.WithKeys("down")) t.KeyMap.LineNext = key.NewBinding(key.WithKeys("down"))
t.KeyMap.LinePrevious = key.NewBinding(key.WithKeys("up")) t.KeyMap.LinePrevious = key.NewBinding(key.WithKeys("up"))
t.CharLimit = (1024 * 8) * 8
t.MaxHeight = 1024 * 8
t.Blur() t.Blur()
return t return t
} }
@@ -86,6 +87,12 @@ func newModel() model {
key.WithKeys("esc", "ctrl+c"), key.WithKeys("esc", "ctrl+c"),
key.WithHelp("esc", "quit"), key.WithHelp("esc", "quit"),
), ),
copy: key.NewBinding(
key.WithKeys("ctrl+x"),
),
clear: key.NewBinding(
key.WithKeys("ctrl+r"),
),
}, },
} }
m.decoded.Focus() m.decoded.Focus()
@@ -114,6 +121,15 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds = append(cmds, m.encoded.Focus()) cmds = append(cmds, m.encoded.Focus())
m.decoded.Blur() 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: case tea.WindowSizeMsg:
m.height = msg.Height m.height = msg.Height
@@ -122,7 +138,18 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.sizeInputs() 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) encoded, cmd := m.encoded.Update(msg)
cmds = append(cmds, cmd) cmds = append(cmds, cmd)
m.encoded = encoded m.encoded = encoded
@@ -135,29 +162,28 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
} }
func (m *model) sizeInputs() { func (m *model) sizeInputs() {
m.encoded.SetHeight(m.height-5) m.encoded.SetHeight(m.height - 5)
m.decoded.SetHeight(m.height-5) m.decoded.SetHeight(m.height - 5)
m.encoded.SetWidth(m.width/2-5) m.encoded.SetWidth(m.width/2 - 5)
m.decoded.SetCursor(m.width/2-5) m.decoded.SetWidth(m.width/2 - 5)
} }
func (m model) View() string { func (m model) View() string {
help := m.help.ShortHelpView([]key.Binding{ help := m.help.ShortHelpView([]key.Binding{
m.keymap.next, m.keymap.next,
m.keymap.prev, m.keymap.copy,
m.keymap.add,
m.keymap.remove,
m.keymap.quit, m.keymap.quit,
}) })
var views []string var views []string
views = append(views, m.encoded.View())
views = append(views, m.decoded.View()) views = append(views, m.decoded.View())
views = append(views, m.encoded.View())
return lipgloss.JoinHorizontal(lipgloss.Top, views...) + "\n" + help return lipgloss.JoinHorizontal(lipgloss.Top, views...) + "\n" + help
} }
func main() { func main() {
clipboard.Init()
if _, err := tea.NewProgram(newModel(), tea.WithAltScreen()).Run(); err != nil { if _, err := tea.NewProgram(newModel(), tea.WithAltScreen()).Run(); err != nil {
fmt.Println("Error while running program:", err) fmt.Println("Error while running program:", err)
os.Exit(1) os.Exit(1)