From b6f39bd1bacd77e545fff772911b812f79949bed Mon Sep 17 00:00:00 2001 From: bloeys Date: Fri, 22 Jul 2022 19:58:20 +0400 Subject: [PATCH] Use Esc[ bytes for ansi detection+move ansi to its own package --- ansi.go => ansi/ansi.go | 11 ++++++----- main.go | 23 ++++++++++++----------- 2 files changed, 18 insertions(+), 16 deletions(-) rename ansi.go => ansi/ansi.go (97%) diff --git a/ansi.go b/ansi/ansi.go similarity index 97% rename from ansi.go rename to ansi/ansi.go index aa3694e..4fc878e 100755 --- a/ansi.go +++ b/ansi/ansi.go @@ -1,4 +1,4 @@ -package main +package ansi import ( "bytes" @@ -105,12 +105,13 @@ const ( ) var ( - // represents the string: \x1b - AnsiEscBytes = []byte{'\\', 'x', '1', 'b'} + AnsiEscByte = byte('\x1b') + // AnsiEscStringBytes = []byte{'\\', 'x', '1', 'b'} // represents the string: \x1b - // represents the string: \x1b[ - AnsiCSIBytes = []byte{'\\', 'x', '1', 'b', '['} + AnsiCSIBytes = []byte{'\x1b', '['} AnsiCSIBytesLen = len(AnsiCSIBytes) + // AnsiCSIStringBytes = []byte{'\\', 'x', '1', 'b', '['} // represents the string: \x1b[ + // AnsiCSIStringBytesLen = len(AnsiCSIStringBytes) ) type AnsiCodeOptions int64 diff --git a/main.go b/main.go index bbcb7ab..8502ce7 100755 --- a/main.go +++ b/main.go @@ -20,6 +20,7 @@ import ( "github.com/bloeys/nmage/renderer/rend3dgl" "github.com/bloeys/nmage/timing" nmageimgui "github.com/bloeys/nmage/ui/imgui" + "github.com/bloeys/nterm/ansi" "github.com/bloeys/nterm/assert" "github.com/bloeys/nterm/consts" "github.com/bloeys/nterm/glyphs" @@ -271,7 +272,6 @@ var sepLinePos = gglm.NewVec3(0, 0, 0) func (p *program) MainUpdate() { - // Return if input.KeyClicked(sdl.K_RETURN) || input.KeyClicked(sdl.K_KP_ENTER) { p.WriteToCmdBuf([]rune{'\n'}) p.HandleReturn() @@ -387,7 +387,7 @@ func (p *program) DrawTextAnsiCodes(bs []byte, pos gglm.Vec3) gglm.Vec3 { for { - index, code := NextAnsiCode(bs) + index, code := ansi.NextAnsiCode(bs) if index == -1 { draw(bytesToRunes(bs)) break @@ -398,8 +398,8 @@ func (p *program) DrawTextAnsiCodes(bs []byte, pos gglm.Vec3) gglm.Vec3 { draw(before) //Apply code - info := InfoFromAnsiCode(code) - if info.Options&AnsiCodeOptions_ColorFg != 0 { + info := ansi.InfoFromAnsiCode(code) + if info.Options&ansi.AnsiCodeOptions_ColorFg != 0 { if info.Info1.X() == -1 { currColor = p.Settings.DefaultColor @@ -521,9 +521,15 @@ func (p *program) HandleReturn() { p.cmdBufLen = 0 p.cursorCharIndex = 0 + // @PERF + cmdStr := string(cmdRunes) + cmdBytes := []byte(cmdStr) + p.WriteToTextBuf(cmdBytes) + if p.activeCmd != nil { - _, err := p.activeCmd.Stdin.Write([]byte(string(cmdRunes))) + // println("Wrote:", string(cmdBytes)) + _, err := p.activeCmd.Stdin.Write(cmdBytes) if err != nil { p.WriteToTextBuf([]byte(fmt.Sprintf("Writing to stdin pipe of '%s' failed. Error: %s\n", p.activeCmd.C.Path, err.Error()))) p.ClearActiveCmd() @@ -533,12 +539,7 @@ func (p *program) HandleReturn() { return } - // @PERF - p.WriteToTextBuf([]byte(string(cmdRunes))) - - cmdStr := strings.TrimSpace(string(cmdRunes)) - cmdSplit := strings.Split(cmdStr, " ") - + cmdSplit := strings.Split(strings.TrimSpace(cmdStr), " ") cmdName := cmdSplit[0] var args []string if len(cmdSplit) >= 2 {