From 4a0bbfa81a45ea5dcaff15121680d148d7b39d72 Mon Sep 17 00:00:00 2001 From: Verox007 Date: Sat, 16 Dec 2023 23:17:23 +0100 Subject: [PATCH] Finished console --- go.mod | 8 ++-- go.sum | 10 ++--- pkg/cmd/console.go | 95 ++++++++++++++++++++++++++++++++-------------- 3 files changed, 73 insertions(+), 40 deletions(-) diff --git a/go.mod b/go.mod index 791020c..04db1ad 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,6 @@ module cimeyclust.com/steel go 1.21 -require ( - github.com/gizak/termui v3.1.0+incompatible // indirect - github.com/mattn/go-runewidth v0.0.9 // indirect - github.com/nsf/termbox-go v1.1.1 // indirect -) +require github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203 + +require golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect diff --git a/go.sum b/go.sum index c0279e1..24996dc 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,4 @@ -github.com/gizak/termui v3.1.0+incompatible h1:N3CFm+j087lanTxPpHOmQs0uS3s5I9TxoAFy6DqPqv8= -github.com/gizak/termui v3.1.0+incompatible/go.mod h1:PkJoWUt/zacQKysNfQtcw1RW+eK2SxkieVBtl+4ovLA= -github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= -github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= -github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY= -github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo= +github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203 h1:XBBHcIb256gUJtLmY22n99HaZTz+r2Z51xUPi01m3wg= +github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203/go.mod h1:E1jcSv8FaEny+OP/5k9UxZVw9YFWGj7eI4KR/iOBqCg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/pkg/cmd/console.go b/pkg/cmd/console.go index 4141d8d..54a46e5 100644 --- a/pkg/cmd/console.go +++ b/pkg/cmd/console.go @@ -1,30 +1,22 @@ package cmd import ( - "bufio" "fmt" - "os" - "strings" + "github.com/eiannone/keyboard" "sync" "time" ) type Log struct { - // Level enum of "info", "warning", "error" - Level string - - // Message to be logged + Level string Message string - - // Time of the log - Time string - - // Source of the log - Source string + Time string + Source string } type LoggerQueue struct { - queue chan Log + queue chan Log + currentInput string } var Logger = &LoggerQueue{ @@ -52,35 +44,80 @@ func (l *LoggerQueue) Error(message string) { l.log("ERROR", message, "") } -func (l *LoggerQueue) processLogs() { - for logEntry := range l.queue { - // Move cursor up and clear line - fmt.Printf("%s [%s]: %s\n", logEntry.Time, logEntry.Level, logEntry.Message) +func (l *LoggerQueue) processLogs(stopChan chan struct{}) { + ticker := time.NewTicker(100 * time.Millisecond) + defer ticker.Stop() + + for { + select { + case logEntry := <-l.queue: + displayLog(logEntry) + case <-stopChan: + fmt.Print("\033[2K\r") + println("Stopping") + return + case <-ticker.C: + refreshInputDisplay(l.currentInput) + } } } +func displayLog(logEntry Log) { + fmt.Printf("\033[2K\r%s [%s]: %s\n", logEntry.Time, logEntry.Level, logEntry.Message) + fmt.Print("> ") +} + +func refreshInputDisplay(input string) { + fmt.Print("\033[2K\r> " + input) +} + func Start(wg *sync.WaitGroup, stopChan chan struct{}) { defer wg.Done() - go Logger.processLogs() + if err := keyboard.Open(); err != nil { + panic(err) + } + defer func() { + go keyboard.Close() + }() - reader := bufio.NewReader(os.Stdin) + go Logger.processLogs(stopChan) + + input := "" for { - fmt.Print("> ") - input, err := reader.ReadString('\n') + char, key, err := keyboard.GetKey() if err != nil { - Logger.Error(fmt.Sprintf("Error while reading input: %v", err)) - continue + Logger.Error(fmt.Sprintf("Keyboard error: %v", err)) + break } - input = strings.TrimSpace(input) - switch input { - case "stop": + switch { + case key == keyboard.KeyEnter: + switch input { + case "": + // Do nothing + case "stop": + Logger.Info("Received stop command.") + close(stopChan) + return + default: + Logger.Info(fmt.Sprintf("Unknown command: %s", input)) + + } + input = "" + case key == keyboard.KeyBackspace || key == keyboard.KeyBackspace2: + if len(input) > 0 { + input = input[:len(input)-1] + } + case key == keyboard.KeySpace: + input += " " + case key == keyboard.KeyCtrlC: Logger.Info("Received stop command.") close(stopChan) return - default: - Logger.Info(fmt.Sprintf("Unknown command: %s", input)) + case char != 0: + input += string(char) } + Logger.currentInput = input // Update stored user input } }