Detect lines as they come in+sprintf in assert so it gets optimized away on release mode :)

This commit is contained in:
bloeys
2022-07-24 05:06:07 +04:00
parent 46f958c935
commit bea69429f9
2 changed files with 47 additions and 9 deletions

View File

@ -1,11 +1,13 @@
package assert
import (
"fmt"
"github.com/bloeys/nterm/consts"
)
func T(check bool, msg string) {
func T(check bool, msg string, args ...any) {
if consts.Mode_Debug && !check {
panic("Assert failed: " + msg)
panic("Assert failed: " + fmt.Sprintf(msg, args...))
}
}

46
main.go
View File

@ -52,6 +52,11 @@ type Line struct {
StartIndex, EndIndex uint64
}
func (l *Line) Size() uint64 {
size := l.EndIndex - l.StartIndex
return size
}
var _ engine.Game = &program{}
type program struct {
@ -66,7 +71,11 @@ type program struct {
gridMesh *meshes.Mesh
gridMat *materials.Material
CurrLine Line
CurrLineValid bool
Lines []Line
LineCount uint64
textBuf *ring.Buffer[byte]
textBufMutex sync.Mutex
@ -134,6 +143,9 @@ func main() {
FontSize: 40,
Lines: make([]Line, defaultTextBufSize),
LineCount: 0,
CurrLineValid: true,
textBuf: ring.NewBuffer[byte](defaultTextBufSize),
cursorCharIndex: 0,
@ -255,6 +267,7 @@ func (p *program) Update() {
func (p *program) WriteToTextBuf(text []byte) {
// This is locked because running cmds are potentially writing to it same time we are
p.textBufMutex.Lock()
p.ParseLines(text)
p.textBuf.Write(text...)
p.scrollPos = clamp(p.textBuf.Len-p.CellCount, 0, p.textBuf.Len-1)
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)
b := buf[:readBytes]
p.ParseLines(b)
p.WriteToTextBuf(b)
// println("Read:", string(buf[:readBytes]))
}
@ -630,8 +642,6 @@ func (p *program) HandleReturn() {
//Stderr
go func() {
defer p.ClearActiveCmd()
buf := make([]byte, 1024)
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() {
@ -777,7 +813,7 @@ func (p *program) DrawGrid() {
}
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 {