Update
This commit is contained in:
56
main.go
56
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)
|
||||
|
Reference in New Issue
Block a user