changed from unmaintained version of gocui to newer fork; vastly improved handling of logging

This commit is contained in:
Krumel
2021-06-04 00:11:11 +02:00
parent 3285b6f1ce
commit 5e3682cb3d
3 changed files with 38 additions and 47 deletions

View File

@ -2,22 +2,20 @@ package main
import (
"bufio"
// "fmt"
"fmt"
"io"
"log"
"time"
// "math"
"container/ring"
"os/exec"
"github.com/jroimartin/gocui"
"github.com/awesome-gocui/gocui"
"github.com/pelletier/go-toml"
)
var settings *toml.Tree
var stdout_ring *ring.Ring
var srv_stdin io.WriteCloser
var new_stdout bool = false
var (
settings *toml.Tree
srv_stdin io.WriteCloser
)
// reload settings (and maybe other things too)
func reload() {
@ -30,7 +28,7 @@ func build_cmd() (string, []string) {
}
//run the server-thread
func server_run() {
func server_run(g *gocui.Gui) {
//create the command
cmd_str, cmd_args := build_cmd()
cmd := exec.Command(cmd_str, cmd_args...)
@ -50,9 +48,14 @@ func server_run() {
buf := bufio.NewScanner(stdout)
for {
if buf.Scan() {
stdout_ring.Value = buf.Text()
stdout_ring = stdout_ring.Next()
new_stdout = true
g.Update(func(g *gocui.Gui) error {
v, err := g.View("srv_log")
if err != nil {
return err
}
fmt.Fprintln(v, buf.Text())
return nil
})
} else {
time.Sleep(300 * time.Millisecond)
}
@ -63,11 +66,8 @@ func main() {
//initialize settings by "re"loading them from disk
reload()
//initialize the stdout/err ringbuffer
stdout_ring = ring.New(255)
//init the CUI
g, err := gocui.NewGui(gocui.OutputNormal)
g, err := gocui.NewGui(gocui.OutputNormal, false)
if err != nil {
log.Panicln(err)
}
@ -79,17 +79,7 @@ func main() {
log.Panicln(err)
}
go server_run()
go func(g *gocui.Gui) {
for {
time.Sleep(50 * time.Millisecond)
if new_stdout {
new_stdout = false
g.Update(layout)
}
}
}(g)
go server_run(g)
//run the CUI main loop
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
@ -101,29 +91,13 @@ func main() {
func layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
v, err := g.SetView("srv_log", 0, 0, maxX-1, int(float32(maxY) * 0.8))
v, err := g.SetView("srv_log", 0, 0, maxX-1, int(float32(maxY) * 0.8), 0)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
//dont put code in here you idiot
}
v.Clear()
vW, vH := v.Size()
r := stdout_ring.Prev()
for i := 0; i < vH; i++ {
v.SetCursor(0, vH - i - 1)
if r.Value != nil {
s := r.Value.(string)
for i := 0; i < len(s) && i < vW; i++ {
v.EditWrite([]rune(s)[i])
}
}
r = r.Prev()
//*only* put initialization code for the view here
v.Autoscroll = true
}
return nil