Skip to content
Myr

main.go

This file

Path

examples/split-editors/main.go

235 lines · 5.0 KiB · Go

Last changed by

Merge pull request #1118 from charmbracelet/v2-expAyman Bagabasbefore adoptiond1c8c97

History of this file

Take it

1package main23import (4	"fmt"5	"os"67	"charm.land/bubbles/v2/help"8	"charm.land/bubbles/v2/key"9	"charm.land/bubbles/v2/textarea"10	tea "charm.land/bubbletea/v2"11	"charm.land/lipgloss/v2"12)1314const (15	initialInputs = 216	maxInputs     = 617	minInputs     = 118	helpHeight    = 519)2021var (22	cursorColor = lipgloss.Color("212")2324	cursorLineStyle = lipgloss.NewStyle().25			Background(lipgloss.Color("57")).26			Foreground(lipgloss.Color("230"))2728	placeholderStyle = lipgloss.NewStyle().29				Foreground(lipgloss.Color("238"))3031	focusedPlaceholderStyle = lipgloss.NewStyle().32				Foreground(lipgloss.Color("99"))3334	focusedBorderStyle = lipgloss.NewStyle().35				Border(lipgloss.RoundedBorder()).36				BorderForeground(lipgloss.Color("238"))3738	blurredBorderStyle = lipgloss.NewStyle().39				Border(lipgloss.HiddenBorder())4041	endOfBufferStyle = lipgloss.NewStyle().42				Foreground(lipgloss.Color("235"))43)4445type keymap = struct {46	next, prev, add, remove, quit key.Binding47}4849func newTextarea() textarea.Model {50	t := textarea.New()51	t.Prompt = ""52	t.Placeholder = "Type something"53	t.ShowLineNumbers = true54	t.SetVirtualCursor(true)5556	s := t.Styles()57	s.Cursor.Color = cursorColor58	s.Focused.Placeholder = focusedPlaceholderStyle59	s.Blurred.Placeholder = placeholderStyle60	s.Focused.CursorLine = cursorLineStyle61	s.Focused.CursorLineNumber = cursorLineStyle62	s.Focused.Base = focusedBorderStyle63	s.Blurred.Base = blurredBorderStyle64	s.Focused.EndOfBuffer = endOfBufferStyle65	s.Blurred.EndOfBuffer = endOfBufferStyle66	t.SetStyles(s)6768	t.KeyMap.DeleteWordBackward.SetEnabled(false)69	t.KeyMap.LineNext = key.NewBinding(key.WithKeys("down"))70	t.KeyMap.LinePrevious = key.NewBinding(key.WithKeys("up"))71	t.Blur()72	return t73}7475type model struct {76	width  int77	height int78	keymap keymap79	help   help.Model80	inputs []textarea.Model81	focus  int82}8384func newModel() model {85	m := model{86		inputs: make([]textarea.Model, initialInputs),87		help:   help.New(),88		keymap: keymap{89			next: key.NewBinding(90				key.WithKeys("tab"),91				key.WithHelp("tab", "next"),92			),93			prev: key.NewBinding(94				key.WithKeys("shift+tab"),95				key.WithHelp("shift+tab", "prev"),96			),97			add: key.NewBinding(98				key.WithKeys("ctrl+n"),99				key.WithHelp("ctrl+n", "add an editor"),100			),101			remove: key.NewBinding(102				key.WithKeys("ctrl+w"),103				key.WithHelp("ctrl+w", "remove an editor"),104			),105			quit: key.NewBinding(106				key.WithKeys("esc", "ctrl+c"),107				key.WithHelp("esc", "quit"),108			),109		},110	}111	for i := range initialInputs {112		m.inputs[i] = newTextarea()113	}114	m.inputs[m.focus].Focus()115	m.updateKeybindings()116	return m117}118119func (m model) Init() tea.Cmd {120	return textarea.Blink121}122123func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {124	var cmds []tea.Cmd125126	switch msg := msg.(type) {127	case tea.KeyPressMsg:128		switch {129		case key.Matches(msg, m.keymap.quit):130			for i := range m.inputs {131				m.inputs[i].Blur()132			}133			return m, tea.Quit134		case key.Matches(msg, m.keymap.next):135			m.inputs[m.focus].Blur()136			m.focus++137			if m.focus > len(m.inputs)-1 {138				m.focus = 0139			}140			cmd := m.inputs[m.focus].Focus()141			cmds = append(cmds, cmd)142		case key.Matches(msg, m.keymap.prev):143			m.inputs[m.focus].Blur()144			m.focus--145			if m.focus < 0 {146				m.focus = len(m.inputs) - 1147			}148			cmd := m.inputs[m.focus].Focus()149			cmds = append(cmds, cmd)150		case key.Matches(msg, m.keymap.add):151			m.inputs = append(m.inputs, newTextarea())152		case key.Matches(msg, m.keymap.remove):153			m.inputs = m.inputs[:len(m.inputs)-1]154			if m.focus > len(m.inputs)-1 {155				m.focus = len(m.inputs) - 1156			}157		}158	case tea.WindowSizeMsg:159		m.height = msg.Height160		m.width = msg.Width161	}162163	m.updateKeybindings()164	m.sizeInputs()165166	// Update all textareas167	for i := range m.inputs {168		newModel, cmd := m.inputs[i].Update(msg)169		m.inputs[i] = newModel170		cmds = append(cmds, cmd)171	}172173	return m, tea.Batch(cmds...)174}175176func (m *model) sizeInputs() {177	for i := range m.inputs {178		m.inputs[i].SetWidth(m.width / len(m.inputs))179		m.inputs[i].SetHeight(m.height - helpHeight)180	}181}182183func (m *model) updateKeybindings() {184	m.keymap.add.SetEnabled(len(m.inputs) < maxInputs)185	m.keymap.remove.SetEnabled(len(m.inputs) > minInputs)186}187188func (m model) inputViews() []string {189	var views []string190	for i := range m.inputs {191		views = append(views, m.inputs[i].View())192	}193	return views194}195196func (m model) View() tea.View {197	help := m.help.ShortHelpView([]key.Binding{198		m.keymap.next,199		m.keymap.prev,200		m.keymap.add,201		m.keymap.remove,202		m.keymap.quit,203	})204205	v := tea.NewView(lipgloss.JoinHorizontal(lipgloss.Top, m.inputViews()...) + "\n\n" + help)206	v.AltScreen = true207	return v208}209210func (m model) Cursor() *tea.Cursor {211	focusedInput := m.inputs[m.focus]212	if focusedInput.VirtualCursor() {213		return nil214	}215216	views := m.inputViews()217	c := focusedInput.Cursor()218219	// Find textrea offset to position real cursor.220	//221	// To do this we calculate the width of all textareas to the left of222	// the focused one.223	for i := range m.focus {224		c.X += lipgloss.Width(views[i])225	}226227	return c228}229230func main() {231	if _, err := tea.NewProgram(newModel()).Run(); err != nil {232		fmt.Println("Error while running program:", err)233		os.Exit(1)234	}235}