Finished console

This commit is contained in:
Verox007 2023-12-16 23:17:23 +01:00
parent 7110f02154
commit 4a0bbfa81a
3 changed files with 73 additions and 40 deletions

8
go.mod
View File

@ -2,8 +2,6 @@ module cimeyclust.com/steel
go 1.21 go 1.21
require ( require github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203
github.com/gizak/termui v3.1.0+incompatible // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect require golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
github.com/nsf/termbox-go v1.1.1 // indirect
)

10
go.sum
View File

@ -1,6 +1,4 @@
github.com/gizak/termui v3.1.0+incompatible h1:N3CFm+j087lanTxPpHOmQs0uS3s5I9TxoAFy6DqPqv8= github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203 h1:XBBHcIb256gUJtLmY22n99HaZTz+r2Z51xUPi01m3wg=
github.com/gizak/termui v3.1.0+incompatible/go.mod h1:PkJoWUt/zacQKysNfQtcw1RW+eK2SxkieVBtl+4ovLA= github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203/go.mod h1:E1jcSv8FaEny+OP/5k9UxZVw9YFWGj7eI4KR/iOBqCg=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY=
github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo=

View File

@ -1,30 +1,22 @@
package cmd package cmd
import ( import (
"bufio"
"fmt" "fmt"
"os" "github.com/eiannone/keyboard"
"strings"
"sync" "sync"
"time" "time"
) )
type Log struct { type Log struct {
// Level enum of "info", "warning", "error" Level string
Level string
// Message to be logged
Message string Message string
Time string
// Time of the log Source string
Time string
// Source of the log
Source string
} }
type LoggerQueue struct { type LoggerQueue struct {
queue chan Log queue chan Log
currentInput string
} }
var Logger = &LoggerQueue{ var Logger = &LoggerQueue{
@ -52,35 +44,80 @@ func (l *LoggerQueue) Error(message string) {
l.log("ERROR", message, "") l.log("ERROR", message, "")
} }
func (l *LoggerQueue) processLogs() { func (l *LoggerQueue) processLogs(stopChan chan struct{}) {
for logEntry := range l.queue { ticker := time.NewTicker(100 * time.Millisecond)
// Move cursor up and clear line defer ticker.Stop()
fmt.Printf("%s [%s]: %s\n", logEntry.Time, logEntry.Level, logEntry.Message)
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{}) { func Start(wg *sync.WaitGroup, stopChan chan struct{}) {
defer wg.Done() 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 { for {
fmt.Print("> ") char, key, err := keyboard.GetKey()
input, err := reader.ReadString('\n')
if err != nil { if err != nil {
Logger.Error(fmt.Sprintf("Error while reading input: %v", err)) Logger.Error(fmt.Sprintf("Keyboard error: %v", err))
continue break
} }
input = strings.TrimSpace(input) switch {
switch input { case key == keyboard.KeyEnter:
case "stop": 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.") Logger.Info("Received stop command.")
close(stopChan) close(stopChan)
return return
default: case char != 0:
Logger.Info(fmt.Sprintf("Unknown command: %s", input)) input += string(char)
} }
Logger.currentInput = input // Update stored user input
} }
} }