1package vt
2
3import (
4 "strconv"
5 "strings"
6
7 renderer "github.com/bnema/vev-vt/core"
8)
9
10type cursorState struct {
11 row int
12 col int
13 style renderer.Style
14 originMode bool
15 insertMode bool
16 autoWrapMode bool
17 charsets [2]byte
18 shiftOut bool
19 saved bool
20}
21
22func (s *Screen) cursorReportRow() int {
23 if s.originMode {
24 return clamp(s.Row, s.cursorMinRow(), s.cursorMaxRow()) - s.cursorMinRow() + 1
25 }
26 return s.Row + 1
27}
28
29func (s *Screen) cursorMinRow() int {
30 if s.originMode {
31 return clamp(s.scrollTop, 0, s.frame.Height-1)
32 }
33 return 0
34}
35
36func (s *Screen) cursorMaxRow() int {
37 if s.originMode {
38 return clamp(s.scrollBottom, 0, s.frame.Height-1)
39 }
40 return max(s.frame.Height-1, 0)
41}
42
43func (s *Screen) addressedRow(row int) int {
44 if s.originMode {
45 return clamp(s.scrollTop+row-1, s.cursorMinRow(), s.cursorMaxRow())
46 }
47 return clamp(row-1, 0, s.frame.Height-1)
48}
49
50func (s *Screen) addressCursor(row, col int) {
51 s.Row = s.addressedRow(row)
52 s.Col = clamp(col-1, 0, s.frame.Width-1)
53}
54
55func (s *Screen) homeCursor() {
56 s.addressCursor(1, 1)
57}
58
59func (s *Screen) applyCursorStyle(params string) {
60 if strings.HasPrefix(params, ">") || !strings.HasSuffix(params, " ") {
61 return
62 }
63 styleParam := strings.TrimSuffix(params, " ")
64 style := 0
65 if styleParam != "" {
66 v, err := strconv.Atoi(styleParam)
67 if err != nil {
68 return
69 }
70 style = v
71 }
72 if style < 0 || style > 6 {
73 return
74 }
75 s.cursorStyle = style
76 s.cursorStyleSet = true
77}
78
79func (s *Screen) saveCursor() {
80 s.savedCursor = cursorState{
81 row: s.Row, col: s.Col, style: s.Style,
82 originMode: s.originMode, insertMode: s.insertMode,
83 autoWrapMode: s.autoWrapMode, charsets: s.charsets,
84 shiftOut: s.shiftOut, saved: true,
85 }
86}
87
88func (s *Screen) restoreCursor() {
89 if !s.savedCursor.saved {
90 return
91 }
92 s.Row = clamp(s.savedCursor.row, 0, s.frame.Height-1)
93 s.Col = clamp(s.savedCursor.col, 0, s.frame.Width-1)
94 s.Style = s.savedCursor.style
95 s.originMode = s.savedCursor.originMode
96 s.insertMode = s.savedCursor.insertMode
97 s.autoWrapMode = s.savedCursor.autoWrapMode
98 s.charsets = s.savedCursor.charsets
99 s.shiftOut = s.savedCursor.shiftOut
100}