in plc4go/tools/plc4xbrowser/ui/ui.go [113:210]
func buildCommandArea(newPrimitive func(text string) tview.Primitive, application *tview.Application) tview.Primitive {
commandAreaHeader := newPrimitive("Commands")
commandArea := tview.NewGrid().
SetRows(3, 0, 3).
SetColumns(0).
AddItem(commandAreaHeader, 0, 0, 1, 1, 0, 0, false)
{
enteredCommandsView := tview.NewTextView().
SetDynamicColors(true).
SetRegions(true).
SetWordWrap(true).
SetChangedFunc(func() {
application.Draw()
})
commandOutput = enteredCommandsView
commandOutputClear = func() {
enteredCommandsView.SetText("")
}
commandArea.AddItem(enteredCommandsView, 1, 0, 1, 1, 0, 0, false)
commandInputField := tview.NewInputField().
SetLabel("$").
SetFieldWidth(30)
application.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyCtrlC:
commandInputField.SetText("")
application.SetFocus(commandInputField)
return nil
case tcell.KeyCtrlD:
// TODO: maybe add a modal here
application.Stop()
return nil
}
return event
})
commandInputField.
SetDoneFunc(func(key tcell.Key) {
commandText := commandInputField.GetText()
if commandText == "quit" {
// TODO: maybe add a modal here
application.Stop()
return
}
commandsExecuted++
go func() {
commandHistoryShortcut, _ := regexp.Compile("^[0-9]$")
if commandHistoryShortcut.MatchString(commandText) {
atoi, _ := strconv.Atoi(commandHistoryShortcut.FindString(commandText))
if atoi < len(config.History.Last10Commands) {
commandText = config.History.Last10Commands[atoi]
} else {
_, _ = fmt.Fprintf(enteredCommandsView, "[#ff0000]%s %s[white]\n", time.Now().Format("04:05"), errors.Errorf("No such elements %d in command history", atoi))
return
}
}
_, _ = fmt.Fprintf(enteredCommandsView, "%s [\"%d\"]%s[\"\"]\n", time.Now().Format("04:05"), commandsExecuted, commandText)
if err := Execute(commandText); err != nil {
_, _ = fmt.Fprintf(enteredCommandsView, "[#ff0000]%s %s[white]\n", time.Now().Format("04:05"), err)
return
}
application.QueueUpdateDraw(func() {
commandInputField.SetText("")
})
}()
})
commandInputField.SetAutocompleteFunc(rootCommand.Completions)
enteredCommandsView.SetDoneFunc(func(key tcell.Key) {
currentSelection := enteredCommandsView.GetHighlights()
if key == tcell.KeyEnter {
if len(currentSelection) > 0 {
enteredCommandsView.Highlight()
} else {
enteredCommandsView.Highlight("0").ScrollToHighlight()
}
if len(currentSelection) == 1 {
commandInputField.SetText(enteredCommandsView.GetRegionText(currentSelection[0]))
application.SetFocus(commandInputField)
}
} else if len(currentSelection) > 0 {
index, _ := strconv.Atoi(currentSelection[0])
if key == tcell.KeyTab {
index = (index + 1) % commandsExecuted
} else if key == tcell.KeyBacktab {
index = (index - 1 + commandsExecuted) % commandsExecuted
} else {
return
}
enteredCommandsView.Highlight(strconv.Itoa(index)).ScrollToHighlight()
}
})
commandArea.AddItem(commandInputField, 2, 0, 1, 1, 0, 0, true)
}
return commandArea
}