-
Notifications
You must be signed in to change notification settings - Fork 46
/
cursor.go
76 lines (55 loc) · 1.35 KB
/
cursor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package render
import (
"fmt"
"github.com/aaronjanse/3mux/ecma48"
)
// deltaMarkup returns markup to transform from one cursor to another
func deltaMarkup(fromCur, toCur ecma48.Cursor) string {
out := ""
/* update position */
xDiff := toCur.X - fromCur.X
yDiff := toCur.Y - fromCur.Y
if xDiff == 0 && yDiff == 1 {
out += "\033[1B"
} else if xDiff != 0 || yDiff != 0 {
out += fmt.Sprintf("\033[%d;%dH", toCur.Y+1, toCur.X+1)
}
/* update colors */
to := toCur.Style
from := fromCur.Style
differentBgCode := (to.Bg.ColorMode != ecma48.ColorNone) && to.Bg.Code != from.Bg.Code
if to.Bg.ColorMode != from.Bg.ColorMode || differentBgCode {
out += to.Bg.ToANSI(true)
}
differentFgCode := (to.Fg.ColorMode != ecma48.ColorNone) && to.Fg.Code != from.Fg.Code
if to.Fg.ColorMode != from.Fg.ColorMode || differentFgCode {
out += to.Fg.ToANSI(false)
}
/* remove effects */
if from.Bold && !to.Bold {
out += "\033[22m"
}
if from.Faint && !to.Faint {
out += "\033[22m"
}
if from.Underline && !to.Underline {
out += "\033[24m"
}
if from.Reverse && !to.Reverse {
out += "\033[27m"
}
/* add effects */
if !from.Bold && to.Bold {
out += "\033[1m"
}
if !from.Faint && to.Faint {
out += "\033[2m"
}
if !from.Underline && to.Underline {
out += "\033[4m"
}
if !from.Reverse && to.Reverse {
out += "\033[7m"
}
return out
}