package repl import ( "git.red-panda.pet/pandaware/lox-go/reporter" "github.com/charmbracelet/bubbles/textinput" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/log" ) type REPL struct { input textinput.Model history []string historySize uint } func NewREPL(historySize uint) REPL { reporter.SetLevel(reporter.LevelDebug) inputModel := textinput.New() inputModel.Focus() inputModel.Prompt = "repl> " inputModel.Placeholder = "..." return REPL{ history: make([]string, historySize), historySize: historySize, input: inputModel, } } func (r REPL) Init() tea.Cmd { return textinput.Blink } func (r REPL) handleKey(msg tea.KeyMsg) (REPL, tea.Cmd) { switch msg.Type { case tea.KeyEnter, tea.KeyCtrlC, tea.KeyEscape: return r, tea.Quit } return r, nil } func (r REPL) Update(msg tea.Msg) (tea.Model, tea.Cmd) { var cmd tea.Cmd switch m := msg.(type) { case tea.KeyMsg: r, cmd = r.handleKey(m) if cmd != nil { return r, cmd } } r.input, cmd = r.input.Update(msg) return r, nil } func (r REPL) View() string { return r.input.View() } func Run(r REPL) error { p := tea.NewProgram(r) if _, err := p.Run(); err != nil { log.Error("error in REPL", "err", err) return err } return nil }