This repository has been archived by the owner on Feb 10, 2023. It is now read-only.
/
util.go
182 lines (168 loc) · 4.66 KB
/
util.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"errors"
"fmt"
"log"
"regexp"
"runtime"
"strconv"
"strings"
"time"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
// takes year/race ID and returns full year and race nuber as strings
func getYearAndRace(input string) (string, string, error) {
var fullYear string
var raceNumber string
if len(input) < 4 {
return fullYear, raceNumber, errors.New("not long enough")
}
_, err := strconv.Atoi(input[:4])
if err != nil {
return fullYear, raceNumber, errors.New("not a valid YearRaceID")
}
// TODO fix before 2020
if input[:4] == "2018" || input[:4] == "2019" {
return input[:4], "0", nil
}
year := input[:2]
intYear, _ := strconv.Atoi(year)
// TODO: change before 2030
if intYear < 30 {
fullYear = "20" + year
} else {
fullYear = "19" + year
}
raceNumber = input[2:4]
return fullYear, raceNumber, nil
}
func (session *viewerSession) logErrorf(format string, v ...interface{}) {
session.logError(fmt.Sprintf(format, v...))
}
func (session *viewerSession) logError(v ...interface{}) {
if session.textWindow != nil {
fmt.Fprintln(session.textWindow, fmt.Sprintf("[%s::b]ERROR:[-::-]", colortoHexString(activeTheme.ErrorColor)), fmt.Sprint(v...))
}
log.Println("[ERROR]", fmt.Sprint(v...))
}
func (session *viewerSession) logInfof(format string, v ...interface{}) {
session.logInfo(fmt.Sprintf(format, v...))
}
func (session *viewerSession) logInfo(v ...interface{}) {
if session.textWindow != nil {
fmt.Fprintln(session.textWindow, fmt.Sprintf("[%s::b]INFO:[-::-]", colortoHexString(activeTheme.InfoColor)), fmt.Sprint(v...))
}
log.Println("[INFO]", fmt.Sprint(v...))
}
func (session *viewerSession) withBlink(node *tview.TreeNode, fn func(), after func()) func() {
return func() {
done := make(chan struct{})
go func() {
fn()
done <- struct{}{}
}()
go func() {
session.blinkNode(node, done)
if after != nil {
after()
}
}()
}
}
func (session *viewerSession) blinkNode(node *tview.TreeNode, done chan struct{}) {
originalText := node.GetText()
originalColor := node.GetColor()
color1 := originalColor
color2 := activeTheme.LoadingColor
node.SetText("loading...")
ticker := time.NewTicker(200 * time.Millisecond)
for {
select {
case <-done:
node.SetText(originalText)
node.SetColor(originalColor)
session.app.Draw()
return
case <-ticker.C:
node.SetColor(color2)
session.app.Draw()
c := color1
color1 = color2
color2 = c
}
}
}
func hexStringToColor(hex string) tcell.Color {
hex = strings.ReplaceAll(hex, "#", "")
color, _ := strconv.ParseInt(hex, 16, 32)
return tcell.NewHexColor(int32(color))
}
func colortoHexString(color tcell.Color) string {
return fmt.Sprintf("#%06x", color.Hex())
}
func (t theme) apply() {
if t.TerminalTextColor != "" {
tview.Styles.PrimaryTextColor = hexStringToColor(t.TerminalTextColor)
}
if t.CategoryNodeColor != "" {
activeTheme.CategoryNodeColor = hexStringToColor(t.CategoryNodeColor)
}
if t.FolderNodeColor != "" {
activeTheme.FolderNodeColor = hexStringToColor(t.FolderNodeColor)
}
if t.ItemNodeColor != "" {
activeTheme.ItemNodeColor = hexStringToColor(t.ItemNodeColor)
}
if t.ActionNodeColor != "" {
activeTheme.ActionNodeColor = hexStringToColor(t.ActionNodeColor)
}
if t.BackgroundColor != "" {
tview.Styles.PrimitiveBackgroundColor = hexStringToColor(t.BackgroundColor)
} else {
tview.Styles.PrimitiveBackgroundColor = tcell.ColorDefault
}
if t.BorderColor != "" {
tview.Styles.BorderColor = hexStringToColor(t.BorderColor)
}
if t.NoContentColor != "" {
activeTheme.NoContentColor = hexStringToColor(t.NoContentColor)
}
if t.LoadingColor != "" {
activeTheme.LoadingColor = hexStringToColor(t.LoadingColor)
}
if t.LiveColor != "" {
activeTheme.LiveColor = hexStringToColor(t.LiveColor)
}
if t.UpdateColor != "" {
activeTheme.UpdateColor = hexStringToColor(t.UpdateColor)
}
if t.TerminalAccentColor != "" {
activeTheme.TerminalAccentColor = hexStringToColor(t.TerminalAccentColor)
}
if t.TerminalTextColor != "" {
activeTheme.TerminalTextColor = hexStringToColor(t.TerminalTextColor)
}
if t.InfoColor != "" {
activeTheme.InfoColor = hexStringToColor(t.InfoColor)
}
if t.ErrorColor != "" {
activeTheme.ErrorColor = hexStringToColor(t.ErrorColor)
}
if t.MultiCommandColor != "" {
activeTheme.MultiCommandColor = hexStringToColor(t.MultiCommandColor)
}
}
func sanitizeFileName(s string) string {
whitespace := regexp.MustCompile(`\s+`)
var illegal *regexp.Regexp
if runtime.GOOS == "windows" {
illegal = regexp.MustCompile(`[<>:"/\\|?*]`)
} else {
illegal = regexp.MustCompile(`/`)
}
s = illegal.ReplaceAllString(s, " ")
s = whitespace.ReplaceAllString(s, " ")
s = strings.TrimSpace(s)
return s
}