handle ex cmds; you can quit
parent
2003843965
commit
2bc70d3377
90
main.go
90
main.go
|
@ -9,17 +9,19 @@ import (
|
|||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type mode string
|
||||
|
||||
const (
|
||||
NormalMode = "normal"
|
||||
FocusedMode = "focused"
|
||||
SearchMode = "search"
|
||||
ExMode = "ex"
|
||||
modeNormal mode = "normal"
|
||||
modeFocus mode = "focus"
|
||||
modeSearch mode = "search"
|
||||
modeEx mode = "ex"
|
||||
)
|
||||
|
||||
type UI struct {
|
||||
Mode string
|
||||
Mode mode
|
||||
App *tview.Application
|
||||
Fields []Field
|
||||
Fields []field
|
||||
|
||||
// UI things
|
||||
Pages *tview.Pages
|
||||
|
@ -31,29 +33,62 @@ type UI struct {
|
|||
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 {
|
||||
switch ui.Mode {
|
||||
case NormalMode:
|
||||
case ExMode:
|
||||
case FocusedMode:
|
||||
case SearchMode:
|
||||
case modeNormal:
|
||||
switch event.Rune() {
|
||||
case ':':
|
||||
ui.setMode(modeEx)
|
||||
return nil
|
||||
}
|
||||
case modeEx:
|
||||
case modeFocus:
|
||||
case modeSearch:
|
||||
default:
|
||||
panic("mode?")
|
||||
}
|
||||
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
|
||||
Selected bool
|
||||
}
|
||||
|
||||
func newUI() *UI {
|
||||
func NewUI() *UI {
|
||||
app := tview.NewApplication()
|
||||
ui := UI{
|
||||
Mode: NormalMode,
|
||||
Mode: modeNormal,
|
||||
App: app,
|
||||
Fields: []Field{{"scratch", true}, {"test", false}},
|
||||
Fields: []field{{"scratch", true}, {"test", false}},
|
||||
Pages: tview.NewPages(),
|
||||
TopFlex: tview.NewFlex(),
|
||||
Field: tview.NewBox(),
|
||||
|
@ -68,18 +103,25 @@ func newUI() *UI {
|
|||
ui.TopFlex.SetDirection(tview.FlexRow)
|
||||
ui.TopFlex.AddItem(ui.Field, 0, 20, true)
|
||||
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.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)
|
||||
|
||||
app.SetBeforeDrawFunc(func(_ tcell.Screen) bool {
|
||||
// Handle field bar
|
||||
ui.FieldBar.Clear()
|
||||
|
||||
for _, f := range ui.Fields {
|
||||
t := tview.NewTextView().SetTextStyle(tcell.StyleDefault.Bold(f.Selected))
|
||||
t.SetMaxLines(1)
|
||||
|
@ -87,6 +129,14 @@ func newUI() *UI {
|
|||
fmt.Fprintf(t, f.Name)
|
||||
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
|
||||
})
|
||||
|
||||
|
@ -94,11 +144,15 @@ func newUI() *UI {
|
|||
return &ui
|
||||
}
|
||||
|
||||
func (ui *UI) quit() {
|
||||
ui.App.Stop()
|
||||
}
|
||||
|
||||
func _main() error {
|
||||
cmd := &cobra.Command{
|
||||
Use: "porphyry",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return newUI().App.Run()
|
||||
return NewUI().App.Run()
|
||||
},
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue