1package ansiterm
2
3func (ap *AnsiParser) collectParam() error {
4	currChar := ap.context.currentChar
5	ap.logf("collectParam %#x", currChar)
6	ap.context.paramBuffer = append(ap.context.paramBuffer, currChar)
7	return nil
8}
9
10func (ap *AnsiParser) collectInter() error {
11	currChar := ap.context.currentChar
12	ap.logf("collectInter %#x", currChar)
13	ap.context.paramBuffer = append(ap.context.interBuffer, currChar)
14	return nil
15}
16
17func (ap *AnsiParser) escDispatch() error {
18	cmd, _ := parseCmd(*ap.context)
19	intermeds := ap.context.interBuffer
20	ap.logf("escDispatch currentChar: %#x", ap.context.currentChar)
21	ap.logf("escDispatch: %v(%v)", cmd, intermeds)
22
23	switch cmd {
24	case "D": // IND
25		return ap.eventHandler.IND()
26	case "E": // NEL, equivalent to CRLF
27		err := ap.eventHandler.Execute(ANSI_CARRIAGE_RETURN)
28		if err == nil {
29			err = ap.eventHandler.Execute(ANSI_LINE_FEED)
30		}
31		return err
32	case "M": // RI
33		return ap.eventHandler.RI()
34	}
35
36	return nil
37}
38
39func (ap *AnsiParser) csiDispatch() error {
40	cmd, _ := parseCmd(*ap.context)
41	params, _ := parseParams(ap.context.paramBuffer)
42	ap.logf("Parsed params: %v with length: %d", params, len(params))
43
44	ap.logf("csiDispatch: %v(%v)", cmd, params)
45
46	switch cmd {
47	case "@":
48		return ap.eventHandler.ICH(getInt(params, 1))
49	case "A":
50		return ap.eventHandler.CUU(getInt(params, 1))
51	case "B":
52		return ap.eventHandler.CUD(getInt(params, 1))
53	case "C":
54		return ap.eventHandler.CUF(getInt(params, 1))
55	case "D":
56		return ap.eventHandler.CUB(getInt(params, 1))
57	case "E":
58		return ap.eventHandler.CNL(getInt(params, 1))
59	case "F":
60		return ap.eventHandler.CPL(getInt(params, 1))
61	case "G":
62		return ap.eventHandler.CHA(getInt(params, 1))
63	case "H":
64		ints := getInts(params, 2, 1)
65		x, y := ints[0], ints[1]
66		return ap.eventHandler.CUP(x, y)
67	case "J":
68		param := getEraseParam(params)
69		return ap.eventHandler.ED(param)
70	case "K":
71		param := getEraseParam(params)
72		return ap.eventHandler.EL(param)
73	case "L":
74		return ap.eventHandler.IL(getInt(params, 1))
75	case "M":
76		return ap.eventHandler.DL(getInt(params, 1))
77	case "P":
78		return ap.eventHandler.DCH(getInt(params, 1))
79	case "S":
80		return ap.eventHandler.SU(getInt(params, 1))
81	case "T":
82		return ap.eventHandler.SD(getInt(params, 1))
83	case "c":
84		return ap.eventHandler.DA(params)
85	case "d":
86		return ap.eventHandler.VPA(getInt(params, 1))
87	case "f":
88		ints := getInts(params, 2, 1)
89		x, y := ints[0], ints[1]
90		return ap.eventHandler.HVP(x, y)
91	case "h":
92		return ap.hDispatch(params)
93	case "l":
94		return ap.lDispatch(params)
95	case "m":
96		return ap.eventHandler.SGR(getInts(params, 1, 0))
97	case "r":
98		ints := getInts(params, 2, 1)
99		top, bottom := ints[0], ints[1]
100		return ap.eventHandler.DECSTBM(top, bottom)
101	default:
102		ap.logf("ERROR: Unsupported CSI command: '%s', with full context:  %v", cmd, ap.context)
103		return nil
104	}
105
106}
107
108func (ap *AnsiParser) print() error {
109	return ap.eventHandler.Print(ap.context.currentChar)
110}
111
112func (ap *AnsiParser) clear() error {
113	ap.context = &ansiContext{}
114	return nil
115}
116
117func (ap *AnsiParser) execute() error {
118	return ap.eventHandler.Execute(ap.context.currentChar)
119}
120