2022-07-01 12:20:04 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/help"
|
|
|
|
"github.com/charmbracelet/bubbles/key"
|
|
|
|
"github.com/charmbracelet/bubbles/textarea"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/charmbracelet/lipgloss"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
initialInputs = 2
|
|
|
|
maxInputs = 6
|
|
|
|
minInputs = 1
|
2022-08-18 15:49:36 -04:00
|
|
|
helpHeight = 5
|
2022-07-01 12:20:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-07-05 14:31:32 -04:00
|
|
|
cursorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("212"))
|
|
|
|
|
2022-07-01 12:20:04 -04:00
|
|
|
cursorLineStyle = lipgloss.NewStyle().
|
|
|
|
Background(lipgloss.Color("57")).
|
|
|
|
Foreground(lipgloss.Color("230"))
|
|
|
|
|
|
|
|
placeholderStyle = lipgloss.NewStyle().
|
|
|
|
Foreground(lipgloss.Color("238"))
|
|
|
|
|
2022-10-03 12:06:18 -04:00
|
|
|
endOfBufferStyle = lipgloss.NewStyle().
|
|
|
|
Foreground(lipgloss.Color("235"))
|
|
|
|
|
2022-07-01 12:20:04 -04:00
|
|
|
focusedPlaceholderStyle = lipgloss.NewStyle().
|
|
|
|
Foreground(lipgloss.Color("99"))
|
2022-07-05 14:31:32 -04:00
|
|
|
|
|
|
|
focusedBorderStyle = lipgloss.NewStyle().
|
|
|
|
Border(lipgloss.RoundedBorder()).
|
|
|
|
BorderForeground(lipgloss.Color("238"))
|
|
|
|
|
|
|
|
blurredBorderStyle = lipgloss.NewStyle().
|
|
|
|
Border(lipgloss.HiddenBorder())
|
2022-07-01 12:20:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type keymap = struct {
|
|
|
|
next, prev, add, remove, quit key.Binding
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTextarea() textarea.Model {
|
|
|
|
t := textarea.New()
|
|
|
|
t.Prompt = ""
|
|
|
|
t.Placeholder = "Type something"
|
|
|
|
t.ShowLineNumbers = true
|
2022-07-05 14:31:32 -04:00
|
|
|
t.Cursor.Style = cursorStyle
|
2022-07-01 12:20:04 -04:00
|
|
|
t.FocusedStyle.Placeholder = focusedPlaceholderStyle
|
|
|
|
t.BlurredStyle.Placeholder = placeholderStyle
|
|
|
|
t.FocusedStyle.CursorLine = cursorLineStyle
|
2022-07-05 14:31:32 -04:00
|
|
|
t.FocusedStyle.Base = focusedBorderStyle
|
|
|
|
t.BlurredStyle.Base = blurredBorderStyle
|
2022-10-03 12:06:18 -04:00
|
|
|
t.FocusedStyle.EndOfBuffer = endOfBufferStyle
|
|
|
|
t.BlurredStyle.EndOfBuffer = endOfBufferStyle
|
2022-07-01 12:20:04 -04:00
|
|
|
t.KeyMap.DeleteWordBackward.SetEnabled(false)
|
|
|
|
t.KeyMap.LineNext = key.NewBinding(key.WithKeys("down"))
|
|
|
|
t.KeyMap.LinePrevious = key.NewBinding(key.WithKeys("up"))
|
|
|
|
t.Blur()
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
type model struct {
|
|
|
|
width int
|
2022-08-18 15:49:36 -04:00
|
|
|
height int
|
2022-07-01 12:20:04 -04:00
|
|
|
keymap keymap
|
|
|
|
help help.Model
|
|
|
|
inputs []textarea.Model
|
|
|
|
focus int
|
|
|
|
}
|
|
|
|
|
|
|
|
func newModel() model {
|
|
|
|
m := model{
|
|
|
|
inputs: make([]textarea.Model, initialInputs),
|
|
|
|
help: help.New(),
|
|
|
|
keymap: keymap{
|
|
|
|
next: key.NewBinding(
|
|
|
|
key.WithKeys("tab"),
|
|
|
|
key.WithHelp("tab", "next"),
|
|
|
|
),
|
|
|
|
prev: key.NewBinding(
|
|
|
|
key.WithKeys("shift+tab"),
|
|
|
|
key.WithHelp("shift+tab", "prev"),
|
|
|
|
),
|
|
|
|
add: key.NewBinding(
|
|
|
|
key.WithKeys("ctrl+n"),
|
|
|
|
key.WithHelp("ctrl+n", "add an editor"),
|
|
|
|
),
|
|
|
|
remove: key.NewBinding(
|
|
|
|
key.WithKeys("ctrl+w"),
|
|
|
|
key.WithHelp("ctrl+w", "remove an editor"),
|
|
|
|
),
|
|
|
|
quit: key.NewBinding(
|
|
|
|
key.WithKeys("esc", "ctrl+c"),
|
|
|
|
key.WithHelp("esc", "quit"),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for i := 0; i < initialInputs; i++ {
|
|
|
|
m.inputs[i] = newTextarea()
|
|
|
|
}
|
|
|
|
m.inputs[m.focus].Focus()
|
|
|
|
m.updateKeybindings()
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) Init() tea.Cmd {
|
|
|
|
return textarea.Blink
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
|
|
var cmds []tea.Cmd
|
|
|
|
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
case tea.KeyMsg:
|
|
|
|
switch {
|
|
|
|
case key.Matches(msg, m.keymap.quit):
|
|
|
|
for i := range m.inputs {
|
|
|
|
m.inputs[i].Blur()
|
|
|
|
}
|
|
|
|
return m, tea.Quit
|
|
|
|
case key.Matches(msg, m.keymap.next):
|
|
|
|
m.inputs[m.focus].Blur()
|
|
|
|
m.focus++
|
|
|
|
if m.focus > len(m.inputs)-1 {
|
|
|
|
m.focus = 0
|
|
|
|
}
|
|
|
|
cmd := m.inputs[m.focus].Focus()
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
case key.Matches(msg, m.keymap.prev):
|
|
|
|
m.inputs[m.focus].Blur()
|
|
|
|
m.focus--
|
|
|
|
if m.focus < 0 {
|
|
|
|
m.focus = len(m.inputs) - 1
|
|
|
|
}
|
|
|
|
cmd := m.inputs[m.focus].Focus()
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
case key.Matches(msg, m.keymap.add):
|
|
|
|
m.inputs = append(m.inputs, newTextarea())
|
|
|
|
case key.Matches(msg, m.keymap.remove):
|
|
|
|
m.inputs = m.inputs[:len(m.inputs)-1]
|
|
|
|
if m.focus > len(m.inputs)-1 {
|
|
|
|
m.focus = len(m.inputs) - 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case tea.WindowSizeMsg:
|
2022-08-18 15:49:36 -04:00
|
|
|
m.height = msg.Height
|
2022-07-01 12:20:04 -04:00
|
|
|
m.width = msg.Width
|
|
|
|
}
|
|
|
|
|
|
|
|
m.updateKeybindings()
|
|
|
|
m.sizeInputs()
|
|
|
|
|
|
|
|
// Update all textareas
|
|
|
|
for i := range m.inputs {
|
|
|
|
newModel, cmd := m.inputs[i].Update(msg)
|
|
|
|
m.inputs[i] = newModel
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, tea.Batch(cmds...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *model) sizeInputs() {
|
|
|
|
for i := range m.inputs {
|
|
|
|
m.inputs[i].SetWidth(m.width / len(m.inputs))
|
2022-08-18 15:49:36 -04:00
|
|
|
m.inputs[i].SetHeight(m.height - helpHeight)
|
2022-07-01 12:20:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *model) updateKeybindings() {
|
|
|
|
m.keymap.add.SetEnabled(len(m.inputs) < maxInputs)
|
|
|
|
m.keymap.remove.SetEnabled(len(m.inputs) > minInputs)
|
|
|
|
}
|
|
|
|
|
|
|
|
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.quit,
|
|
|
|
})
|
|
|
|
|
|
|
|
var views []string
|
|
|
|
for i := range m.inputs {
|
|
|
|
views = append(views, m.inputs[i].View())
|
|
|
|
}
|
|
|
|
|
|
|
|
return lipgloss.JoinHorizontal(lipgloss.Top, views...) + "\n\n" + help
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2022-10-07 17:56:12 -04:00
|
|
|
if _, err := tea.NewProgram(newModel(), tea.WithAltScreen()).Run(); err != nil {
|
2022-07-01 12:20:04 -04:00
|
|
|
fmt.Println("Error while running program:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|