Skip to content

Commit

Permalink
feat(plc4xbrowser): display last 10 commands and save them in config
Browse files Browse the repository at this point in the history
  • Loading branch information
sruehl committed Aug 3, 2022
1 parent 6858376 commit c53f4fa
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 11 deletions.
13 changes: 12 additions & 1 deletion plc4go/tests/drivers/tests/manual_cbus_driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,29 @@ import (
"github.com/apache/plc4x/plc4go/internal/cbus"
"github.com/apache/plc4x/plc4go/internal/spi/testutils"
"github.com/apache/plc4x/plc4go/pkg/api"
"github.com/apache/plc4x/plc4go/pkg/api/config"
"github.com/apache/plc4x/plc4go/pkg/api/model"
"github.com/apache/plc4x/plc4go/pkg/api/transports"
_ "github.com/apache/plc4x/plc4go/tests/initializetest"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/require"
"os"
"testing"
"time"
)

func TestManualCBusDriver(t *testing.T) {
log.Logger = log.
With().Caller().Logger().
Output(zerolog.ConsoleWriter{Out: os.Stderr}).
Level(zerolog.TraceLevel)
config.TraceTransactionManagerWorkers = true
config.TraceTransactionManagerTransactions = true
config.TraceDefaultMessageCodecWorker = true
t.Skip()

connectionString := "c-bus://192.168.178.101?srchk=true"
connectionString := "c-bus://192.168.178.101"
driverManager := plc4go.NewPlcDriverManager()
driverManager.RegisterDriver(cbus.NewDriver())
transports.RegisterTcpTransport(driverManager)
Expand Down
8 changes: 6 additions & 2 deletions plc4go/tools/plc4xbrowser/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func init() {
Name: "help",
Description: "prints out this help",
action: func(_ Command, _ string) error {
_, _ = fmt.Fprintf(commandOutput, "Available commands\n")
_, _ = fmt.Fprintf(commandOutput, "[#0000ff]Available commands[white]\n")
rootCommand.visit(0, func(currentIndent int, command Command) {
indentString := strings.Repeat(" ", currentIndent)
_, _ = fmt.Fprintf(commandOutput, "%s [#00ff00]%s[white]: %s\n", indentString, command.Name, command.Description)
Expand Down Expand Up @@ -380,7 +380,11 @@ func (c Command) hasDirectExecution() bool {
}

func Execute(commandText string) error {
return rootCommand.Execute(commandText)
err := rootCommand.Execute(commandText)
if err == nil {
addCommand(commandText)
}
return err
}

func (c Command) Execute(commandText string) error {
Expand Down
20 changes: 19 additions & 1 deletion plc4go/tools/plc4xbrowser/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ var config Config

type Config struct {
History struct {
Last10Hosts []string `yaml:"last_hosts"`
Last10Hosts []string `yaml:"last_hosts"`
Last10Commands []string `yaml:"last_commands"`
}
LastUpdated time.Time `yaml:"last_updated"`
LogLevel string `yaml:"log_level"`
Expand Down Expand Up @@ -112,6 +113,23 @@ func addHost(host string) {
config.History.Last10Hosts = append(config.History.Last10Hosts, host)
}

func addCommand(command string) {
existingIndex := -1
for i, lastCommand := range config.History.Last10Commands {
if lastCommand == command {
existingIndex = i
break
}
}
if existingIndex >= 0 {
config.History.Last10Commands = append(config.History.Last10Commands[:existingIndex], config.History.Last10Commands[existingIndex+1:]...)
}
if len(config.History.Last10Commands) >= 10 {
config.History.Last10Commands = config.History.Last10Commands[1:]
}
config.History.Last10Commands = append(config.History.Last10Commands, command)
}

func setLevel(level zerolog.Level) {
config.LogLevel = level.String()
}
46 changes: 39 additions & 7 deletions plc4go/tools/plc4xbrowser/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ var driverAdded func(string)
var connections map[string]plc4go.PlcConnection
var connectionsChanged func()

var commandsExecuted int

var messagesReceived int
var messageOutput io.Writer

var consoleOutput io.Writer

var commandsExecuted int
var commandOutput io.Writer

func init() {
Expand All @@ -70,6 +69,13 @@ func initSubsystem() {
//With().Caller().Logger().
Output(zerolog.ConsoleWriter{Out: tview.ANSIWriter(consoleOutput)}).
Level(logLevel)

// We offset the commands executed with the last commands
commandsExecuted = len(config.History.Last10Commands)
_, _ = fmt.Fprintln(commandOutput, "[#0000ff]Last 10 commands[white]")
for i, command := range config.History.Last10Commands {
_, _ = fmt.Fprintf(commandOutput, " %d: [\"%d\"]%s[\"\"]\n", i+1, i, command)
}
}

var shutdownMutex sync.Mutex
Expand Down Expand Up @@ -188,16 +194,16 @@ func buildCommandArea(newPrimitive func(text string) tview.Primitive, applicatio
SetColumns(0).
AddItem(commandAreaHeader, 0, 0, 1, 1, 0, 0, false)
{
enteredCommands := tview.NewTextView().
enteredCommandsView := tview.NewTextView().
SetDynamicColors(true).
SetRegions(true).
SetWordWrap(true).
SetChangedFunc(func() {
application.Draw()
})
commandOutput = enteredCommands
commandOutput = enteredCommandsView

commandArea.AddItem(enteredCommands, 1, 0, 1, 1, 0, 0, false)
commandArea.AddItem(enteredCommandsView, 1, 0, 1, 1, 0, 0, false)

commandInputField := tview.NewInputField().
SetLabel("$").
Expand All @@ -211,10 +217,10 @@ func buildCommandArea(newPrimitive func(text string) tview.Primitive, applicatio
return
}
commandsExecuted++
_, _ = fmt.Fprintf(enteredCommands, "%s [\"%d\"]%s[\"\"]\n", time.Now().Format("04:05"), commandsExecuted, commandText)
_, _ = fmt.Fprintf(enteredCommandsView, "%s [\"%d\"]%s[\"\"]\n", time.Now().Format("04:05"), commandsExecuted, commandText)
go func() {
if err := Execute(commandText); err != nil {
_, _ = fmt.Fprintf(enteredCommands, "[#ff0000]%s %s[white]\n", time.Now().Format("04:05"), err)
_, _ = fmt.Fprintf(enteredCommandsView, "[#ff0000]%s %s[white]\n", time.Now().Format("04:05"), err)
return
}
application.QueueUpdateDraw(func() {
Expand All @@ -223,6 +229,32 @@ func buildCommandArea(newPrimitive func(text string) tview.Primitive, applicatio
}()
})
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
Expand Down

0 comments on commit c53f4fa

Please sign in to comment.