fixed the updating of the view

This commit is contained in:
Krumel
2021-06-03 21:36:32 +02:00
parent 32f188f54a
commit 3285b6f1ce

View File

@ -2,7 +2,7 @@ package main
import (
"bufio"
"fmt"
// "fmt"
"io"
"log"
"time"
@ -17,6 +17,7 @@ import (
var settings *toml.Tree
var stdout_ring *ring.Ring
var srv_stdin io.WriteCloser
var new_stdout bool = false
// reload settings (and maybe other things too)
func reload() {
@ -51,36 +52,11 @@ func server_run() {
if buf.Scan() {
stdout_ring.Value = buf.Text()
stdout_ring = stdout_ring.Next()
new_stdout = true
} else {
time.Sleep(300 * time.Millisecond)
}
}
}
//refresh the screen
func refresh(g *gocui.Gui) error {
v, err := g.View("srv_log")
if err != nil {
log.Panicln(err)
}
v.Clear()
_, vH := v.Size()
r := stdout_ring
v.Overwrite = true
for i := 0; i < vH; i++ {
v.SetCursor(4, vH - i)
if r.Value != nil {
fmt.Fprint(v, r.Value)
}
r = r.Prev()
}
return nil
}
func main() {
@ -105,23 +81,50 @@ func main() {
go server_run()
go func(g *gocui.Gui) {
for {
time.Sleep(50 * time.Millisecond)
if new_stdout {
new_stdout = false
g.Update(layout)
}
}
}(g)
//run the CUI main loop
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err)
}
}
//create the CUI layout
//create or update the CUI layout
func layout(g *gocui.Gui) error {
maxX, maxY := g.Size()
if _, err := g.SetView("srv_log", 0, 0, maxX-1, int(float32(maxY) * 0.8)); err != nil {
v, err := g.SetView("srv_log", 0, 0, maxX-1, int(float32(maxY) * 0.8))
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
//dont put code in here you idiot
}
refresh(g)
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()
}
return nil
}