@@ -6,7 +6,7 @@ mod util;
6
6
use crate :: { lang:: Language , thok:: Thok } ;
7
7
use clap:: { ArgEnum , ErrorKind , IntoApp , Parser } ;
8
8
use crossterm:: {
9
- event:: { self , Event , KeyCode } ,
9
+ event:: { self , Event , KeyCode , KeyEvent } ,
10
10
execute,
11
11
terminal:: { disable_raw_mode, enable_raw_mode, EnterAlternateScreen , LeaveAlternateScreen } ,
12
12
tty:: IsTty ,
@@ -129,7 +129,10 @@ fn start_tui<B: Backend>(
129
129
mut app : & mut App ,
130
130
) -> Result < ( ) , Box < dyn Error > > {
131
131
let cli = app. cli . clone ( ) ;
132
- let events = get_events ( cli. unwrap ( ) . number_of_secs . unwrap_or ( 0 ) > 0 ) ;
132
+
133
+ let should_tick = cli. unwrap ( ) . number_of_secs . unwrap_or ( 0 ) > 0 ;
134
+
135
+ let thok_events = get_thok_events ( should_tick) ;
133
136
134
137
loop {
135
138
let mut exit_type: ExitType = ExitType :: Quit ;
@@ -138,8 +141,8 @@ fn start_tui<B: Backend>(
138
141
loop {
139
142
let app = & mut app;
140
143
141
- match events . recv ( ) ? {
142
- Events :: Tick => {
144
+ match thok_events . recv ( ) ? {
145
+ ThokEvent :: Tick => {
143
146
if app. thok . has_started ( ) && !app. thok . has_finished ( ) {
144
147
app. thok . on_tick ( ) ;
145
148
@@ -149,8 +152,11 @@ fn start_tui<B: Backend>(
149
152
terminal. draw ( |f| ui ( f, app) ) ?;
150
153
}
151
154
}
152
- Events :: Input ( key) => {
153
- match key {
155
+ ThokEvent :: Resize => {
156
+ terminal. draw ( |f| ui ( f, app) ) ?;
157
+ }
158
+ ThokEvent :: Key ( key) => {
159
+ match key. code {
154
160
KeyCode :: Esc => {
155
161
break ;
156
162
}
@@ -174,10 +180,10 @@ fn start_tui<B: Backend>(
174
180
app. thok . calc_results ( ) ;
175
181
}
176
182
}
177
- true => match key {
183
+ true => match key. code {
178
184
KeyCode :: Char ( 't' ) => {
179
185
webbrowser:: open ( & format ! ( "https://twitter.com/intent/tweet?text={}%20wpm%20%2F%20{}%25%20acc%20%2F%20{:.2}%20sd%0A%0Ahttps%3A%2F%2Fgithub.com%2Fcoloradocolby%2Fthokr" , app. thok. wpm, app. thok. accuracy, app. thok. std_dev) )
180
- . unwrap_or_default ( ) ;
186
+ . unwrap_or_default ( ) ;
181
187
}
182
188
KeyCode :: Char ( 'r' ) => {
183
189
exit_type = ExitType :: Restart ;
@@ -214,25 +220,32 @@ fn start_tui<B: Backend>(
214
220
}
215
221
216
222
#[ derive( Clone ) ]
217
- enum Events {
218
- Input ( KeyCode ) ,
223
+ enum ThokEvent {
224
+ Key ( KeyEvent ) ,
225
+ Resize ,
219
226
Tick ,
220
227
}
221
228
222
- fn get_events ( should_tick : bool ) -> mpsc:: Receiver < Events > {
229
+ fn get_thok_events ( should_tick : bool ) -> mpsc:: Receiver < ThokEvent > {
223
230
let ( tx, rx) = mpsc:: channel ( ) ;
224
231
225
232
if should_tick {
226
233
let tick_x = tx. clone ( ) ;
227
234
thread:: spawn ( move || loop {
228
- tick_x. send ( Events :: Tick ) . unwrap ( ) ;
235
+ tick_x. send ( ThokEvent :: Tick ) . unwrap ( ) ;
229
236
thread:: sleep ( Duration :: from_millis ( 100 ) )
230
237
} ) ;
231
238
}
232
239
233
240
thread:: spawn ( move || loop {
234
- if let Event :: Key ( key) = event:: read ( ) . unwrap ( ) {
235
- tx. send ( Events :: Input ( key. code ) ) . unwrap ( ) ;
241
+ match event:: read ( ) . unwrap ( ) {
242
+ Event :: Key ( key) => {
243
+ tx. send ( ThokEvent :: Key ( key) ) . unwrap ( ) ;
244
+ }
245
+ Event :: Resize ( _, _) => {
246
+ tx. send ( ThokEvent :: Resize ) . unwrap ( ) ;
247
+ }
248
+ _ => { }
236
249
}
237
250
} ) ;
238
251
0 commit comments