restructure
This commit is contained in:
parent
edcbfde351
commit
eebaadc16e
21 changed files with 937 additions and 626 deletions
71
repl/main.go
Normal file
71
repl/main.go
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue