Finished console
This commit is contained in:
parent
7110f02154
commit
4a0bbfa81a
8
go.mod
8
go.mod
@ -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
10
go.sum
@ -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=
|
|
||||||
|
|||||||
@ -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 of the log
|
|
||||||
Time string
|
Time string
|
||||||
|
|
||||||
// Source of the log
|
|
||||||
Source string
|
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 {
|
||||||
|
case key == keyboard.KeyEnter:
|
||||||
switch input {
|
switch input {
|
||||||
|
case "":
|
||||||
|
// Do nothing
|
||||||
case "stop":
|
case "stop":
|
||||||
Logger.Info("Received stop command.")
|
Logger.Info("Received stop command.")
|
||||||
close(stopChan)
|
close(stopChan)
|
||||||
return
|
return
|
||||||
default:
|
default:
|
||||||
Logger.Info(fmt.Sprintf("Unknown command: %s", input))
|
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
|
||||||
|
case char != 0:
|
||||||
|
input += string(char)
|
||||||
|
}
|
||||||
|
Logger.currentInput = input // Update stored user input
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user