-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
164 lines (135 loc) · 3.62 KB
/
main.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
package main
import (
"flag"
"fmt"
"os"
"strings"
"time"
"unicode/utf8"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"unkeyb/generator"
)
func main() {
var leyNames, lang string
// Initializing the model
m := model{}
// Getting Layout names
for k := range layouts {
leyNames += k + ","
}
// Trimming last char
leyNames = strings.TrimSuffix(leyNames, ",")
// Handling flags
flag.StringVar(&lang, "l", "en", "Language (en,fr)")
flag.StringVar(&m.layout, "k", "qwerty", fmt.Sprintf("layout (%s)", leyNames))
flag.BoolVar(&m.minimal, "m", false, "enable minimal UI")
flag.Parse()
// Load the language
generator.Load(lang)
// generate keys
generateList(m.layout)
m.fistChar = true
m.sentence = generator.Sentence()
m.runeCount = utf8.RuneCountInString(m.sentence)
// Stating tea loop
p := tea.NewProgram(m)
if _, err := p.Run(); err != nil {
fmt.Printf("WHAAAAAT ITS BROKEN ALREAAADY ???\ndetails: %v", err)
os.Exit(1)
}
}
func (m model) Init() tea.Cmd {
return tea.EnterAltScreen
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Checking msg type
switch msg := msg.(type) {
// Keyboard input
case tea.KeyMsg:
switch msg.Type {
case tea.KeyTab:
if !m.done {
m.prevKey = "tab"
}
case tea.KeyEnter:
// new Sentence creation and States reset
if m.done || m.prevKey == "tab" {
m.sentence = generator.Sentence()
m.runeCount = utf8.RuneCountInString(m.sentence)
m.done = false
m.fistChar = true
m.prevKey = ""
}
case tea.KeyEscape, tea.KeyCtrlC:
// bay bay
return m, tea.Quit
case tea.KeyRunes, tea.KeySpace:
if !m.done {
// set the time when the first character is typed
if m.fistChar {
m.startTime = time.Now().Unix()
m.fistChar = false
}
// Register the typed character
m.selected = msg.Runes[0]
// Set the Requested character
m.requested = []rune(m.sentence)[0]
// Delete the first character if correct
if m.selected == m.requested {
m.sentence = strings.TrimPrefix(m.sentence, string([]rune(m.sentence)[0]))
}
// Calcuating wpm
if m.sentence == "" {
bTime := float32(time.Now().Unix()-m.startTime) / 60
m.wpm = float32((float32(m.runeCount) / 5) / bTime)
m.done = true
}
}
}
// Terminal resize
case tea.WindowSizeMsg:
m.termWidth = msg.Width
m.termHeight = msg.Height
}
return m, nil
}
func (m model) View() string {
// Getting the template
var visual string
if m.minimal {
visual = uiMinimal(&m)
} else {
visual = uiNormal(&m)
}
// Getting visual height & width
visualWidth := lipgloss.Width(visual)
visualHeight := lipgloss.Height(visual)
// Check if there is enough space
if m.termWidth < visualWidth || m.termHeight < visualHeight {
visual = "Terminal size too small:\n"
// Coloring
// Width
if m.termWidth < visualWidth {
visual += fmt.Sprintf("Width = %s%d%s",
generator.AnsiToString(1), m.termWidth, generator.AnsiReset)
} else {
visual += fmt.Sprintf("Width = %s%d%s",
generator.AnsiToString(2), m.termWidth, generator.AnsiReset)
}
// Height
if m.termHeight < visualHeight {
visual += fmt.Sprintf(" Height = %s%d%s\n\n",
generator.AnsiToString(1), m.termHeight, generator.AnsiReset)
} else {
visual += fmt.Sprintf(" Height = %s%d%s\n\n",
generator.AnsiToString(2), m.termHeight, generator.AnsiReset)
}
// Required size
visual += "Needed for current config:\n"
visual += fmt.Sprintf("Width = %d Height = %d", visualWidth, visualHeight)
}
// Centering
visual = lipgloss.Place(m.termWidth, m.termHeight, lipgloss.Center, lipgloss.Center, visual)
return visual
}