Skip to content
Myr

main.go

This file

Path

examples/capability/main.go

76 lines · 1.5 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/textinput"8	tea "charm.land/bubbletea/v2"9	"charm.land/lipgloss/v2"10)1112type model struct {13	input textinput.Model14	width int15}1617var _ tea.Model = model{}1819// Init implements tea.Model.20func (m model) Init() tea.Cmd {21	return m.input.Focus()22}2324// Update implements tea.Model.25func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {26	var cmd tea.Cmd27	switch msg := msg.(type) {28	case tea.WindowSizeMsg:29		m.width = msg.Width30	case tea.KeyPressMsg:31		switch msg.String() {32		case "ctrl+c", "esc":33			return m, tea.Quit34		case "enter":35			input := m.input.Value()36			m.input.Reset()37			return m, tea.RequestCapability(input)38		}39	case tea.CapabilityMsg:40		return m, tea.Printf("Got capability: %s", msg)41	}42	m.input, cmd = m.input.Update(msg)43	return m, cmd44}4546// View implements tea.Model.47func (m model) View() tea.View {48	w := min(m.width, 60)4950	instructions := lipgloss.NewStyle().51		Width(w).52		Render("Query for terminal capabilities. You can enter things like 'TN', 'RGB', 'cols', and so on. This will not work in all terminals and multiplexers.")5354	return tea.NewView("\n" + instructions + "\n\n" +55		m.input.View() +56		"\n\nPress enter to request capability, or ctrl+c to quit.")57}5859func main() {60	m := model{}61	m.input = textinput.New()62	m.input.Placeholder = "Enter capability name to request"63	m.input.Focus()6465	if _, err := tea.NewProgram(m).Run(); err != nil {66		fmt.Fprintln(os.Stderr, "Uh oh:", err)67		os.Exit(1)68	}69}7071func min(a, b int) int {72	if a < b {73		return a74	}75	return b76}