mirror of
https://github.com/bloeys/nterm.git
synced 2025-12-29 14:38:19 +00:00
Detect lines as they come in+sprintf in assert so it gets optimized away on release mode :)
This commit is contained in:
@ -1,11 +1,13 @@
|
|||||||
package assert
|
package assert
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/bloeys/nterm/consts"
|
"github.com/bloeys/nterm/consts"
|
||||||
)
|
)
|
||||||
|
|
||||||
func T(check bool, msg string) {
|
func T(check bool, msg string, args ...any) {
|
||||||
if consts.Mode_Debug && !check {
|
if consts.Mode_Debug && !check {
|
||||||
panic("Assert failed: " + msg)
|
panic("Assert failed: " + fmt.Sprintf(msg, args...))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
46
main.go
46
main.go
@ -52,6 +52,11 @@ type Line struct {
|
|||||||
StartIndex, EndIndex uint64
|
StartIndex, EndIndex uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *Line) Size() uint64 {
|
||||||
|
size := l.EndIndex - l.StartIndex
|
||||||
|
return size
|
||||||
|
}
|
||||||
|
|
||||||
var _ engine.Game = &program{}
|
var _ engine.Game = &program{}
|
||||||
|
|
||||||
type program struct {
|
type program struct {
|
||||||
@ -66,7 +71,11 @@ type program struct {
|
|||||||
gridMesh *meshes.Mesh
|
gridMesh *meshes.Mesh
|
||||||
gridMat *materials.Material
|
gridMat *materials.Material
|
||||||
|
|
||||||
|
CurrLine Line
|
||||||
|
CurrLineValid bool
|
||||||
Lines []Line
|
Lines []Line
|
||||||
|
LineCount uint64
|
||||||
|
|
||||||
textBuf *ring.Buffer[byte]
|
textBuf *ring.Buffer[byte]
|
||||||
textBufMutex sync.Mutex
|
textBufMutex sync.Mutex
|
||||||
|
|
||||||
@ -134,6 +143,9 @@ func main() {
|
|||||||
FontSize: 40,
|
FontSize: 40,
|
||||||
|
|
||||||
Lines: make([]Line, defaultTextBufSize),
|
Lines: make([]Line, defaultTextBufSize),
|
||||||
|
LineCount: 0,
|
||||||
|
CurrLineValid: true,
|
||||||
|
|
||||||
textBuf: ring.NewBuffer[byte](defaultTextBufSize),
|
textBuf: ring.NewBuffer[byte](defaultTextBufSize),
|
||||||
|
|
||||||
cursorCharIndex: 0,
|
cursorCharIndex: 0,
|
||||||
@ -255,6 +267,7 @@ func (p *program) Update() {
|
|||||||
func (p *program) WriteToTextBuf(text []byte) {
|
func (p *program) WriteToTextBuf(text []byte) {
|
||||||
// This is locked because running cmds are potentially writing to it same time we are
|
// This is locked because running cmds are potentially writing to it same time we are
|
||||||
p.textBufMutex.Lock()
|
p.textBufMutex.Lock()
|
||||||
|
p.ParseLines(text)
|
||||||
p.textBuf.Write(text...)
|
p.textBuf.Write(text...)
|
||||||
p.scrollPos = clamp(p.textBuf.Len-p.CellCount, 0, p.textBuf.Len-1)
|
p.scrollPos = clamp(p.textBuf.Len-p.CellCount, 0, p.textBuf.Len-1)
|
||||||
p.textBufMutex.Unlock()
|
p.textBufMutex.Unlock()
|
||||||
@ -621,7 +634,6 @@ func (p *program) HandleReturn() {
|
|||||||
|
|
||||||
// @Todo We need to parse ansi codes as data is coming in to update the drawing settings (e.g. color)
|
// @Todo We need to parse ansi codes as data is coming in to update the drawing settings (e.g. color)
|
||||||
b := buf[:readBytes]
|
b := buf[:readBytes]
|
||||||
p.ParseLines(b)
|
|
||||||
p.WriteToTextBuf(b)
|
p.WriteToTextBuf(b)
|
||||||
// println("Read:", string(buf[:readBytes]))
|
// println("Read:", string(buf[:readBytes]))
|
||||||
}
|
}
|
||||||
@ -630,8 +642,6 @@ func (p *program) HandleReturn() {
|
|||||||
//Stderr
|
//Stderr
|
||||||
go func() {
|
go func() {
|
||||||
|
|
||||||
defer p.ClearActiveCmd()
|
|
||||||
|
|
||||||
buf := make([]byte, 1024)
|
buf := make([]byte, 1024)
|
||||||
for p.activeCmd != nil {
|
for p.activeCmd != nil {
|
||||||
|
|
||||||
@ -655,8 +665,34 @@ func (p *program) HandleReturn() {
|
|||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *program) ParseLines(b []byte) {
|
func (p *program) ParseLines(bs []byte) {
|
||||||
|
|
||||||
|
for i := uint64(0); i < uint64(len(bs)); i++ {
|
||||||
|
|
||||||
|
b := bs[i]
|
||||||
|
if b == '\n' {
|
||||||
|
|
||||||
|
if p.CurrLineValid {
|
||||||
|
p.CurrLine.EndIndex = p.textBuf.WrittenElements + i
|
||||||
|
p.WriteLine(&p.CurrLine)
|
||||||
|
p.CurrLine.StartIndex = p.textBuf.WrittenElements + i
|
||||||
|
} else {
|
||||||
|
p.CurrLine.StartIndex = p.textBuf.WrittenElements + i
|
||||||
|
p.CurrLineValid = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *program) WriteLine(l *Line) {
|
||||||
|
p.Lines[p.LineCount] = *l
|
||||||
|
p.LineCount = (p.LineCount + 1) % uint64(len(p.Lines))
|
||||||
|
assert.T(l.StartIndex <= l.EndIndex, "Invalid line: %+v\n", l)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *program) IsLineValid(l *Line) bool {
|
||||||
|
isValid := p.textBuf.WrittenElements-l.StartIndex <= uint64(p.textBuf.Cap)
|
||||||
|
return isValid
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *program) ClearActiveCmd() {
|
func (p *program) ClearActiveCmd() {
|
||||||
@ -777,7 +813,7 @@ func (p *program) DrawGrid() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *program) FrameEnd() {
|
func (p *program) FrameEnd() {
|
||||||
assert.T(p.cursorCharIndex <= p.cmdBufLen, fmt.Sprintf("Cursor char index is larger than cmdBufLen! You probablly forgot to move/reset the cursor index along with the buffer length somewhere. Cursor=%d, cmdBufLen=%d\n", p.cursorCharIndex, p.cmdBufLen))
|
assert.T(p.cursorCharIndex <= p.cmdBufLen, "Cursor char index is larger than cmdBufLen! You probablly forgot to move/reset the cursor index along with the buffer length somewhere. Cursor=%d, cmdBufLen=%d\n", p.cursorCharIndex, p.cmdBufLen)
|
||||||
|
|
||||||
if p.Settings.LimitFps {
|
if p.Settings.LimitFps {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user