2015-04-16 17:01:37 -04:00
|
|
|
package ansiterm
|
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
type stateID int
|
2015-04-16 17:01:37 -04:00
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
type state interface {
|
2015-04-16 17:01:37 -04:00
|
|
|
Enter() error
|
|
|
|
Exit() error
|
2016-04-25 18:55:12 -04:00
|
|
|
Handle(byte) (state, error)
|
2015-04-16 17:01:37 -04:00
|
|
|
Name() string
|
2016-04-25 18:55:12 -04:00
|
|
|
Transition(state) error
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
type baseState struct {
|
2015-04-16 17:01:37 -04:00
|
|
|
name string
|
|
|
|
parser *AnsiParser
|
|
|
|
}
|
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
func (base baseState) Enter() error {
|
2015-04-16 17:01:37 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
func (base baseState) Exit() error {
|
2015-04-16 17:01:37 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
func (base baseState) Handle(b byte) (s state, e error) {
|
2015-04-16 17:01:37 -04:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case b == CSI_ENTRY:
|
2016-04-25 18:55:12 -04:00
|
|
|
return base.parser.csiEntry, nil
|
2015-04-16 17:01:37 -04:00
|
|
|
case b == DCS_ENTRY:
|
2016-04-25 18:55:12 -04:00
|
|
|
return base.parser.dcsEntry, nil
|
2015-04-16 17:01:37 -04:00
|
|
|
case b == ANSI_ESCAPE_PRIMARY:
|
2016-04-25 18:55:12 -04:00
|
|
|
return base.parser.escape, nil
|
2015-04-16 17:01:37 -04:00
|
|
|
case b == OSC_STRING:
|
2016-04-25 18:55:12 -04:00
|
|
|
return base.parser.oscString, nil
|
|
|
|
case sliceContains(toGroundBytes, b):
|
|
|
|
return base.parser.ground, nil
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
func (base baseState) Name() string {
|
2015-04-16 17:01:37 -04:00
|
|
|
return base.name
|
|
|
|
}
|
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
func (base baseState) Transition(s state) error {
|
|
|
|
if s == base.parser.ground {
|
2015-04-16 17:01:37 -04:00
|
|
|
execBytes := []byte{0x18}
|
|
|
|
execBytes = append(execBytes, 0x1A)
|
|
|
|
execBytes = append(execBytes, getByteRange(0x80, 0x8F)...)
|
|
|
|
execBytes = append(execBytes, getByteRange(0x91, 0x97)...)
|
|
|
|
execBytes = append(execBytes, 0x99)
|
|
|
|
execBytes = append(execBytes, 0x9A)
|
|
|
|
|
|
|
|
if sliceContains(execBytes, base.parser.context.currentChar) {
|
|
|
|
return base.parser.execute()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
type dcsEntryState struct {
|
|
|
|
baseState
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
type errorState struct {
|
|
|
|
baseState
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|