handle ex cmds; you can quit

trunk
vilmibm 2023-06-11 20:33:05 -04:00
parent 2003843965
commit 2bc70d3377
1 changed files with 72 additions and 18 deletions

90
main.go
View File

@ -9,17 +9,19 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
type mode string
const ( const (
NormalMode = "normal" modeNormal mode = "normal"
FocusedMode = "focused" modeFocus mode = "focus"
SearchMode = "search" modeSearch mode = "search"
ExMode = "ex" modeEx mode = "ex"
) )
type UI struct { type UI struct {
Mode string Mode mode
App *tview.Application App *tview.Application
Fields []Field Fields []field
// UI things // UI things
Pages *tview.Pages Pages *tview.Pages
@ -31,29 +33,62 @@ type UI struct {
ExOutput *tview.TextView ExOutput *tview.TextView
} }
func (ui *UI) setMode(m mode) {
ui.Mode = m
switch ui.Mode {
case modeNormal:
ui.App.SetFocus(ui.TopFlex)
ui.ExInput.SetText("")
case modeEx:
ui.App.SetFocus(ui.ExInput)
}
}
func (ui *UI) handleInput(event *tcell.EventKey) *tcell.EventKey { func (ui *UI) handleInput(event *tcell.EventKey) *tcell.EventKey {
switch ui.Mode { switch ui.Mode {
case NormalMode: case modeNormal:
case ExMode: switch event.Rune() {
case FocusedMode: case ':':
case SearchMode: ui.setMode(modeEx)
return nil
}
case modeEx:
case modeFocus:
case modeSearch:
default: default:
panic("mode?") panic("mode?")
} }
return event return event
} }
type Field struct { func (ui *UI) handleExInput(key tcell.Key) {
text := ui.ExInput.GetText()
ui.setMode(modeNormal)
if key != tcell.KeyEnter {
return
}
switch text {
case "q", "quit":
ui.quit()
}
ui.ExOutput.SetText("")
fmt.Fprintf(ui.ExOutput, "did not understand '%s', sorry", text)
}
type field struct {
Name string Name string
Selected bool Selected bool
} }
func newUI() *UI { func NewUI() *UI {
app := tview.NewApplication() app := tview.NewApplication()
ui := UI{ ui := UI{
Mode: NormalMode, Mode: modeNormal,
App: app, App: app,
Fields: []Field{{"scratch", true}, {"test", false}}, Fields: []field{{"scratch", true}, {"test", false}},
Pages: tview.NewPages(), Pages: tview.NewPages(),
TopFlex: tview.NewFlex(), TopFlex: tview.NewFlex(),
Field: tview.NewBox(), Field: tview.NewBox(),
@ -68,18 +103,25 @@ func newUI() *UI {
ui.TopFlex.SetDirection(tview.FlexRow) ui.TopFlex.SetDirection(tview.FlexRow)
ui.TopFlex.AddItem(ui.Field, 0, 20, true) ui.TopFlex.AddItem(ui.Field, 0, 20, true)
ui.TopFlex.AddItem(ui.FieldBar, 0, 1, false) ui.TopFlex.AddItem(ui.FieldBar, 0, 1, false)
ui.TopFlex.AddItem(ui.BottomBar, 1, -1, false) ui.TopFlex.AddItem(ui.BottomBar, 0, 1, false)
ui.Pages.AddPage("main", ui.TopFlex, true, true) ui.Pages.AddPage("main", ui.TopFlex, true, true)
ui.BottomBar.AddPage("output", ui.ExOutput, true, true) ui.BottomBar.AddPage("output", ui.ExOutput, true, true)
ui.BottomBar.AddPage("input", ui.ExInput, false, false) ui.BottomBar.AddPage("input", ui.ExInput, true, false)
fmt.Fprintln(ui.ExOutput, "porphyry has started. :q to quit") ui.ExInput.SetLabel(":")
ui.ExInput.SetDoneFunc(ui.handleExInput)
ui.ExOutput.SetMaxLines(1)
fmt.Fprintf(ui.ExOutput, "porphyry has started. :q to quit")
ui.FieldBar.SetDirection(tview.FlexColumn) ui.FieldBar.SetDirection(tview.FlexColumn)
app.SetBeforeDrawFunc(func(_ tcell.Screen) bool { app.SetBeforeDrawFunc(func(_ tcell.Screen) bool {
// Handle field bar
ui.FieldBar.Clear() ui.FieldBar.Clear()
for _, f := range ui.Fields { for _, f := range ui.Fields {
t := tview.NewTextView().SetTextStyle(tcell.StyleDefault.Bold(f.Selected)) t := tview.NewTextView().SetTextStyle(tcell.StyleDefault.Bold(f.Selected))
t.SetMaxLines(1) t.SetMaxLines(1)
@ -87,6 +129,14 @@ func newUI() *UI {
fmt.Fprintf(t, f.Name) fmt.Fprintf(t, f.Name)
ui.FieldBar.AddItem(t, 0, 1, false) ui.FieldBar.AddItem(t, 0, 1, false)
} }
// Handle ex mode prompt
if ui.Mode == modeEx {
ui.BottomBar.SwitchToPage("input")
} else {
ui.BottomBar.SwitchToPage("output")
}
return false return false
}) })
@ -94,11 +144,15 @@ func newUI() *UI {
return &ui return &ui
} }
func (ui *UI) quit() {
ui.App.Stop()
}
func _main() error { func _main() error {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "porphyry", Use: "porphyry",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return newUI().App.Run() return NewUI().App.Run()
}, },
} }