Use Esc[ bytes for ansi detection+move ansi to its own package

This commit is contained in:
bloeys
2022-07-22 19:58:20 +04:00
parent 0d2e747739
commit b6f39bd1ba
2 changed files with 18 additions and 16 deletions

View File

@ -1,4 +1,4 @@
package main package ansi
import ( import (
"bytes" "bytes"
@ -105,12 +105,13 @@ const (
) )
var ( var (
// represents the string: \x1b AnsiEscByte = byte('\x1b')
AnsiEscBytes = []byte{'\\', 'x', '1', 'b'} // AnsiEscStringBytes = []byte{'\\', 'x', '1', 'b'} // represents the string: \x1b
// represents the string: \x1b[ AnsiCSIBytes = []byte{'\x1b', '['}
AnsiCSIBytes = []byte{'\\', 'x', '1', 'b', '['}
AnsiCSIBytesLen = len(AnsiCSIBytes) AnsiCSIBytesLen = len(AnsiCSIBytes)
// AnsiCSIStringBytes = []byte{'\\', 'x', '1', 'b', '['} // represents the string: \x1b[
// AnsiCSIStringBytesLen = len(AnsiCSIStringBytes)
) )
type AnsiCodeOptions int64 type AnsiCodeOptions int64

23
main.go
View File

@ -20,6 +20,7 @@ import (
"github.com/bloeys/nmage/renderer/rend3dgl" "github.com/bloeys/nmage/renderer/rend3dgl"
"github.com/bloeys/nmage/timing" "github.com/bloeys/nmage/timing"
nmageimgui "github.com/bloeys/nmage/ui/imgui" nmageimgui "github.com/bloeys/nmage/ui/imgui"
"github.com/bloeys/nterm/ansi"
"github.com/bloeys/nterm/assert" "github.com/bloeys/nterm/assert"
"github.com/bloeys/nterm/consts" "github.com/bloeys/nterm/consts"
"github.com/bloeys/nterm/glyphs" "github.com/bloeys/nterm/glyphs"
@ -271,7 +272,6 @@ var sepLinePos = gglm.NewVec3(0, 0, 0)
func (p *program) MainUpdate() { func (p *program) MainUpdate() {
// Return
if input.KeyClicked(sdl.K_RETURN) || input.KeyClicked(sdl.K_KP_ENTER) { if input.KeyClicked(sdl.K_RETURN) || input.KeyClicked(sdl.K_KP_ENTER) {
p.WriteToCmdBuf([]rune{'\n'}) p.WriteToCmdBuf([]rune{'\n'})
p.HandleReturn() p.HandleReturn()
@ -387,7 +387,7 @@ func (p *program) DrawTextAnsiCodes(bs []byte, pos gglm.Vec3) gglm.Vec3 {
for { for {
index, code := NextAnsiCode(bs) index, code := ansi.NextAnsiCode(bs)
if index == -1 { if index == -1 {
draw(bytesToRunes(bs)) draw(bytesToRunes(bs))
break break
@ -398,8 +398,8 @@ func (p *program) DrawTextAnsiCodes(bs []byte, pos gglm.Vec3) gglm.Vec3 {
draw(before) draw(before)
//Apply code //Apply code
info := InfoFromAnsiCode(code) info := ansi.InfoFromAnsiCode(code)
if info.Options&AnsiCodeOptions_ColorFg != 0 { if info.Options&ansi.AnsiCodeOptions_ColorFg != 0 {
if info.Info1.X() == -1 { if info.Info1.X() == -1 {
currColor = p.Settings.DefaultColor currColor = p.Settings.DefaultColor
@ -521,9 +521,15 @@ func (p *program) HandleReturn() {
p.cmdBufLen = 0 p.cmdBufLen = 0
p.cursorCharIndex = 0 p.cursorCharIndex = 0
// @PERF
cmdStr := string(cmdRunes)
cmdBytes := []byte(cmdStr)
p.WriteToTextBuf(cmdBytes)
if p.activeCmd != nil { 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 { 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.WriteToTextBuf([]byte(fmt.Sprintf("Writing to stdin pipe of '%s' failed. Error: %s\n", p.activeCmd.C.Path, err.Error())))
p.ClearActiveCmd() p.ClearActiveCmd()
@ -533,12 +539,7 @@ func (p *program) HandleReturn() {
return return
} }
// @PERF cmdSplit := strings.Split(strings.TrimSpace(cmdStr), " ")
p.WriteToTextBuf([]byte(string(cmdRunes)))
cmdStr := strings.TrimSpace(string(cmdRunes))
cmdSplit := strings.Split(cmdStr, " ")
cmdName := cmdSplit[0] cmdName := cmdSplit[0]
var args []string var args []string
if len(cmdSplit) >= 2 { if len(cmdSplit) >= 2 {