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 (
"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

23
main.go
View File

@ -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 {