Skip to content

Instantly share code, notes, and snippets.

@bashbunni
Last active September 3, 2022 03:27
Show Gist options
  • Save bashbunni/b8529d304496e17d5cf3c487d52d095f to your computer and use it in GitHub Desktop.
Save bashbunni/b8529d304496e17d5cf3c487d52d095f to your computer and use it in GitHub Desktop.
catppuccin
import (
tea "github.com/charmbracelet/bubbletea"
)
func main() {
// init models, we can reset them at any time anyway
models = []tea.Model{NewInitialModel(), NewSpinnerParent()}
m := models[initialView]
p := tea.NewProgram(m)
if err := p.Start(); err != nil {
panic(err)
}
}
// I put all the globals here :shrug:
var (
models []tea.Model
// current will be used to track the current model being returned from the
// list of models
current int
EnterVal string
)
const (
initialView = iota
spinnerView
)
type errMsg error
import (
// "os"
// "os/exec"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
// you don't need these keymaps but they can be helpful for generating the help
// menu for you
type KeyMap struct {
Up key.Binding
Down key.Binding
}
var DefaultKeyMap = KeyMap{
Up: key.NewBinding(
key.WithKeys("k", "up"), // actual keybindings
key.WithHelp("↑/k", "move up"), // corresponding help text
),
Down: key.NewBinding(
key.WithKeys("j", "down"),
key.WithHelp("↓/j", "move down"),
),
}
type confirm struct {
choices []string
cursor int
selected int
}
type InitialModel struct {
textInput textinput.Model
confirm confirm
err error
}
func NewInitialModel() InitialModel {
ti := textinput.New()
ti.Placeholder = "Helix"
ti.Focus()
ti.CharLimit = 256
ti.Width = 20
con := confirm{
choices: []string{"yes", "no"},
selected: 0,
}
return InitialModel{
textInput: ti,
confirm: con,
err: nil,
}
}
func (m InitialModel) Init() tea.Cmd {
return textinput.Blink
}
func (m InitialModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC, tea.KeyEsc:
return m, tea.Quit
case tea.KeyEnter:
// save value to global so it doesn't get lost
// or you can wrap it as a tea.Msg and send it to the spinnerView to get handled
EnterVal = m.textInput.Value()
return models[spinnerView], models[spinnerView].Init()
}
case errMsg:
m.err = msg
}
m.textInput, cmd = m.textInput.Update(msg)
return m, cmd
}
func (m InitialModel) View() string {
// lipgloss will format the layout for you
return lipgloss.JoinVertical(
lipgloss.Left,
"What's the project name?",
m.textInput.View(),
"(esc to quit)",
)
}
package ui
import (
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type SpinnerParent struct {
spinner spinner.Model
}
func NewSpinnerParent() *SpinnerParent {
spin := spinner.New()
return &SpinnerParent{spinner: spin}
}
func (m SpinnerParent) Init() tea.Cmd {
return m.spinner.Tick
}
func (m SpinnerParent) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
}
}
m.spinner, cmd = m.spinner.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
func (m SpinnerParent) View() string {
return lipgloss.JoinVertical(lipgloss.Left, "hello world", m.spinner.View())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment