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 saved bool
18}
19
20func (s *Screen) cursorReportRow() int {
21 if s.originMode {
22 return clamp(s.Row, s.cursorMinRow(), s.cursorMaxRow()) - s.cursorMinRow() + 1
23 }
24 return s.Row + 1
25}
26
27func (s *Screen) cursorMinRow() int {
28 if s.originMode {
29 return clamp(s.scrollTop, 0, s.frame.Height-1)
30 }
31 return 0
32}
33
34func (s *Screen) cursorMaxRow() int {
35 if s.originMode {
36 return clamp(s.scrollBottom, 0, s.frame.Height-1)
37 }
38 return max(s.frame.Height-1, 0)
39}
40
41func (s *Screen) addressedRow(row int) int {
42 if s.originMode {
43 return clamp(s.scrollTop+row-1, s.cursorMinRow(), s.cursorMaxRow())
44 }
45 return clamp(row-1, 0, s.frame.Height-1)
46}
47
48func (s *Screen) addressCursor(row, col int) {
49 s.Row = s.addressedRow(row)
50 s.Col = clamp(col-1, 0, s.frame.Width-1)
51}
52
53func (s *Screen) homeCursor() {
54 s.addressCursor(1, 1)
55}
56
57func (s *Screen) applyCursorStyle(params string) {
58 if strings.HasPrefix(params, ">") || !strings.HasSuffix(params, " ") {
59 return
60 }
61 styleParam := strings.TrimSuffix(params, " ")
62 style := 0
63 if styleParam != "" {
64 v, err := strconv.Atoi(styleParam)
65 if err != nil {
66 return
67 }
68 style = v
69 }
70 if style < 0 || style > 6 {
71 return
72 }
73 s.cursorStyle = style
74 s.cursorStyleSet = true
75}
76
77func (s *Screen) saveCursor() {
78 s.savedCursor = cursorState{
79 row: s.Row, col: s.Col, style: s.Style,
80 originMode: s.originMode, insertMode: s.insertMode,
81 autoWrapMode: s.autoWrapMode, saved: true,
82 }
83}
84
85func (s *Screen) restoreCursor() {
86 if !s.savedCursor.saved {
87 return
88 }
89 s.Row = clamp(s.savedCursor.row, 0, s.frame.Height-1)
90 s.Col = clamp(s.savedCursor.col, 0, s.frame.Width-1)
91 s.Style = s.savedCursor.style
92 s.originMode = s.savedCursor.originMode
93 s.insertMode = s.savedCursor.insertMode
94 s.autoWrapMode = s.savedCursor.autoWrapMode
95}