Sixel Part 2. Ignore embedded by Windows CSI and OSC sequences.

This commit is contained in:
Roman Shevchenko 2018-12-30 17:40:28 +03:00 committed by Max Risuhin
parent 10a962746f
commit b8e66e0736
2 changed files with 51 additions and 5 deletions

View File

@ -49,11 +49,17 @@ var csiSequences = []csiMapping{
{id: '@', handler: csiInsertBlankCharactersHandler, description: "Insert Ps (Blank) Character(s) (default = 1) (ICH)"},
}
func csiHandler(pty chan rune, terminal *Terminal) error {
var final rune
type runeRange struct {
min rune
max rune
}
var csiTerminators = runeRange{0x40, 0x7e}
func loadCSI(pty chan rune) (final rune, param string, intermediate string) {
var b rune
param := ""
intermediate := ""
param = ""
intermediate = ""
CSI:
for {
b = <-pty
@ -63,12 +69,18 @@ CSI:
case b >= 0x20 && b <= 0x2F:
//intermediate? useful?
intermediate += string(b)
case b >= 0x40 && b <= 0x7e:
case b >= csiTerminators.min && b <= csiTerminators.max:
final = b
break CSI
}
}
return final, param, intermediate
}
func csiHandler(pty chan rune, terminal *Terminal) error {
final, param, intermediate := loadCSI(pty)
params := strings.Split(param, ";")
if param == "" {
params = []string{}

View File

@ -10,7 +10,19 @@ import (
"github.com/liamg/aminal/sixel"
)
type boolFormRuneFunc func(rune) bool
func swallowByFunction(pty chan rune, isTerminator boolFormRuneFunc) {
for {
b := <-pty
if isTerminator(b) {
break
}
}
}
func sixelHandler(pty chan rune, terminal *Terminal) error {
debug := ""
data := []rune{}
@ -18,16 +30,38 @@ func sixelHandler(pty chan rune, terminal *Terminal) error {
b := <-pty
if b == 0x1b { // terminated by ESC bell or ESC \
t := <-pty
if t == '[' { // Windows injected a CSI sequence, let's ignore it
final, param, _ := loadCSI(pty)
debug += "[CSI " + param + string(final) + "]"
continue
}
if t == ']' { // Windows injected an OCS sequence, let's ignore it
swallowByFunction(pty, terminal.IsOSCTerminator)
debug += "[OSC]"
continue
}
if t != 0x07 && t != 0x5c {
return fmt.Errorf("Incorrect terminator in sixel sequence: 0x%02X [%c]", t, t)
}
break
}
if b == 0x0A {
terminal.logger.Debugf("Sixel line: %s", debug)
debug = ""
} else {
debug += string(b)
}
if b >= 33 {
data = append(data, b)
}
}
if debug != "" {
terminal.logger.Debugf("Sixel last line: %s", debug)
}
six, err := sixel.ParseString(string(data))
if err != nil {
return fmt.Errorf("Failed to parse sixel data: %s", err)