typeman 1.0.1

Typing speed test with practice mode in GUI, TUI and CLI
Documentation
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
use eframe::egui;
use egui::{Area, pos2};
use egui_plot::{Line, Plot};
use macroquad::prelude::*;
use std::collections::VecDeque;

use crate::color_scheme::ColorScheme;
use crate::practice;
use crate::utils;

fn calc_standard_deviation(values: &[f64], average_word_length: f64) -> f64 {
    let wpm_values: Vec<f64> = values
        .iter()
        .map(|&cpm| cpm / average_word_length)
        .collect();
    let mean = wpm_values.iter().sum::<f64>() / wpm_values.len() as f64;
    let variance =
        wpm_values.iter().map(|&x| (x - mean).powi(2)).sum::<f64>() / wpm_values.len() as f64;
    variance.sqrt()
}

pub fn write_results(
    is_correct: &VecDeque<i32>,
    screen_width: f32,
    screen_height: f32,
    font: Option<&Font>,
    test_time: f32,
    speed_per_second: &[f64],
    average_word_length: f64,
    mode: &str,
    punctuation: bool,
    numbers: bool,
    errors_per_second: &Vec<f64>,
    reference: &String,
    practice_level: Option<usize>,
    saved_results: &mut bool,
    color_scheme: &ColorScheme,
) {
    let (no_corrected_words, correct_words, all_words) =
        utils::count_correct_words(reference, is_correct);

    let correct_count = is_correct.iter().filter(|&&v| v == 1 || v == 2).count();
    let all_pressed_count = is_correct.iter().filter(|&&v| v != 0).count();
    let accuracy = if correct_count > 0 {
        (correct_count as f64 / all_pressed_count as f64) * 100.0
    } else {
        0.0
    };

    let wpm = if practice_level.is_some() {
        (no_corrected_words as f32 / (test_time / 60.0)).round()
    } else {
        (correct_words as f32 / (test_time / 60.0)).round()
    };
    let raw = all_words as f32 / (test_time / 60.0);

    let chart_width = f32::min(
        f32::max(
            0.75 * f32::min(screen_width, screen_height),
            0.6 * screen_width,
        ),
        1800.0,
    );
    let chart_height: f32 = f32::min(chart_width / 5.0, 360.0);

    let fontsize_1 = (chart_height / 3.0) as u16;
    let fontsize_2 = (chart_height / 7.0) as u16;
    let fontsize_3 = (chart_height / 5.0) as u16;
    let fontsize_4 = (chart_height / 12.0) as u16;

    let text_size = {
        let a = measure_text(&format!("{:0}%", accuracy.floor()), font, fontsize_1, 1.0);
        let b = measure_text(&format!("{:0}", wpm.floor()), font, fontsize_1, 1.0);
        if a.width > b.width { a } else { b }
    };

    let chart_x = (screen_width - chart_width) / 2.0 + fontsize_1 as f32;
    let chart_y = if practice_level.is_some() {
        (screen_height - chart_height) / 4.0
    } else {
        (screen_height - chart_height) / 3.0
    };

    let text2_width = measure_text("consistency", font, 25, 1.0).width;
    let padding = (chart_width - 4.0 * text2_width) / 5.0;
    let wpm_y = if practice_level.is_some() {
        (screen_height - chart_height) / 3.8
    } else {
        (screen_height - chart_height) / 3.0
    };

    let avg_wpm = write_wpm(
        font,
        chart_x - 1.2 * text_size.width,
        wpm_y,
        wpm,
        fontsize_1,
        fontsize_2,
        color_scheme,
    );
    write_acc(
        accuracy,
        font,
        chart_x - 1.2 * text_size.width,
        wpm_y + text_size.height * 2.0,
        fontsize_1,
        fontsize_2,
        color_scheme,
    );
    write_raw_wpm(
        raw,
        font,
        chart_x + padding,
        chart_y + chart_height + fontsize_4 as f32 * 2.0,
        fontsize_3,
        fontsize_4,
        color_scheme,
    );
    write_consistency(
        speed_per_second,
        average_word_length,
        font,
        chart_x + padding + text2_width + padding,
        chart_y + chart_height + fontsize_4 as f32 * 2.0,
        avg_wpm,
        fontsize_3,
        fontsize_4,
        color_scheme,
    );
    write_time(
        test_time,
        font,
        chart_x + padding + (text2_width + padding) * 2.0,
        chart_y + chart_height + fontsize_4 as f32 * 2.0,
        fontsize_3,
        fontsize_4,
        color_scheme,
    );
    write_mode(
        font,
        chart_x + padding + (text2_width + padding) * 3.0,
        chart_y + chart_height + fontsize_4 as f32 * 2.0,
        mode,
        punctuation,
        numbers,
        fontsize_3,
        fontsize_4,
        practice_level,
        color_scheme,
    );

    let mut speed2: Vec<f64> = speed_per_second.to_owned();
    speed2.push(*speed_per_second.last().unwrap_or(&0.0));
    let smoothed_speeds = smooth(&speed2, 2, average_word_length);

    let chart_points: Vec<[f64; 2]> = smoothed_speeds
        .iter()
        .enumerate()
        .map(|(i, &cpm)| [i as f64, cpm])
        .collect();

    draw_chart(
        &chart_points,
        chart_width,
        chart_height,
        chart_x,
        chart_y,
        errors_per_second,
        fontsize_1,
        color_scheme,
    );
    egui_macroquad::draw();

    if practice_level.is_some() {
        let passed_text_font = if screen_width > 1900.0 && screen_height > 1000.0 {
            30
        } else if screen_height > 1000.0 {
            22
        } else {
            19
        };

        let practice_text = if wpm >= practice::WPM_MIN as f32 {
            "Congratulations! You passed this level.".to_string()
        } else {
            format!(
                "You need at least {} WPM to pass this level.",
                practice::WPM_MIN
            )
        };
        let text_size = measure_text(&practice_text, font, passed_text_font, 1.0);

        draw_text_ex(
            practice_text.as_str(),
            (screen_width - text_size.width) / 2.0,
            chart_y + chart_height + screen_height / 4.0,
            TextParams {
                font,
                font_size: passed_text_font,
                font_scale: 1.0,
                color: Color::from_rgba(255, 255, 255, 100),
                ..Default::default()
            },
        );
        if practice::get_prev_best_wpm(practice_level.unwrap() + 1) < wpm as f64 {
            let new_highscore_text = "New highscore for this level!";
            let text_size = measure_text(new_highscore_text, font, 30, 1.0);
            draw_text_ex(
                new_highscore_text,
                (screen_width - text_size.width) / 2.0,
                chart_y + chart_height + 250.0,
                TextParams {
                    font,
                    font_size: passed_text_font,
                    font_scale: 1.0,
                    color: Color::from_rgba(255, 255, 255, 100),
                    ..Default::default()
                },
            );
        }
        if !*saved_results {
            *saved_results = true;
            practice::save_results(
                test_time as f64,
                accuracy,
                wpm as f64,
                practice_level.unwrap() + 1,
            );
        }
    }
}

fn write_mode(
    font: Option<&Font>,
    x: f32,
    y: f32,
    mode: &str,
    punctuation: bool,
    numbers: bool,
    fontsize_3: u16,
    fontsize4: u16,
    practice_level: Option<usize>,
    color_scheme: &ColorScheme,
) {
    let mut fontsize_3 = fontsize_3;
    let fontsize_4 = fontsize4;
    let mode_text = mode.to_string();
    let mut punct_pos: f32 = y;
    let mut number_pos: f32 = y;
    let mut mode_pos: f32 = y + fontsize_3 as f32;
    let level_pos = y + fontsize_3 as f32 * 1.25;
    draw_text_ex(
        "mode",
        x,
        y,
        TextParams {
            font,
            font_size: fontsize_4,
            color: color_scheme.ref_color(),
            ..Default::default()
        },
    );
    if punctuation && numbers {
        punct_pos = y + fontsize_3 as f32 * 1.2;
        number_pos = y + fontsize_3 as f32 * 1.65;
        fontsize_3 = (fontsize_3 as f32 * 0.7) as u16;
        mode_pos = y + fontsize_3 as f32;
    } else if punctuation || numbers {
        fontsize_3 = (fontsize_3 as f32 * 0.8) as u16;
        mode_pos = y + fontsize_3 as f32;
        if punctuation {
            punct_pos = y + fontsize_3 as f32 * 1.65;
        } else {
            number_pos = y + fontsize_3 as f32 * 1.65;
        }
    } else if practice_level.is_some() {
        fontsize_3 = (fontsize_3 as f32 * 0.65) as u16;
        mode_pos = y + fontsize_3 as f32 * 0.9;
    }

    draw_text_ex(
        &mode_text,
        x,
        mode_pos,
        TextParams {
            font,
            font_size: fontsize_3,
            color: color_scheme.main_color(),
            ..Default::default()
        },
    );
    if punctuation {
        draw_text_ex(
            "punctuation",
            x,
            punct_pos,
            TextParams {
                font,
                font_size: fontsize_4,
                color: color_scheme.main_color(),
                ..Default::default()
            },
        );
    }
    if numbers {
        draw_text_ex(
            "numbers",
            x,
            number_pos,
            TextParams {
                font,
                font_size: fontsize_4,
                color: color_scheme.main_color(),
                ..Default::default()
            },
        );
    }
    if practice_level.is_some() && mode == "practice" {
        let level_text = format!("level {}", practice_level.unwrap() + 1);
        draw_text_ex(
            &level_text,
            x,
            level_pos,
            TextParams {
                font,
                font_size: fontsize_4,
                color: color_scheme.main_color(),
                ..Default::default()
            },
        );
    }
}

fn write_time(
    test_time: f32,
    font: Option<&Font>,
    x: f32,
    y: f32,
    fontsize_3: u16,
    fontsize_4: u16,
    color_scheme: &ColorScheme,
) {
    let time_text = format!("{:.0}s", test_time);
    draw_text_ex(
        "time",
        x,
        y,
        TextParams {
            font,
            font_size: fontsize_4,
            color: color_scheme.ref_color(),
            ..Default::default()
        },
    );
    draw_text_ex(
        &time_text,
        x,
        y + fontsize_3 as f32,
        TextParams {
            font,
            font_size: fontsize_3,
            color: color_scheme.main_color(),
            ..Default::default()
        },
    );
}

fn write_consistency(
    speed_per_second: &[f64],
    average_word_length: f64,
    font: Option<&Font>,
    x: f32,
    y: f32,
    avg_wpm: f32,
    fontsize_3: u16,
    fontsize_4: u16,
    color_scheme: &ColorScheme,
) {
    let standard_deviation = calc_standard_deviation(speed_per_second, average_word_length);
    let consistency = if avg_wpm > 0.0 {
        (100.0 - (standard_deviation / avg_wpm as f64 * 100.0).round()).max(0.0)
    } else {
        0.0
    };

    let consistency_text = format!("{consistency}%");
    draw_text_ex(
        "consistency",
        x,
        y,
        TextParams {
            font,
            font_size: fontsize_4,
            color: color_scheme.ref_color(),
            ..Default::default()
        },
    );
    draw_text_ex(
        &consistency_text,
        x,
        y + fontsize_3 as f32,
        TextParams {
            font,
            font_size: fontsize_3,
            color: color_scheme.main_color(),
            ..Default::default()
        },
    );
}

fn write_wpm(
    font: Option<&Font>,
    x: f32,
    y: f32,
    wpm: f32,
    fontsize_1: u16,
    fontsize_2: u16,
    color_scheme: &ColorScheme,
) -> f32 {
    let wpm_text = format!("{:.0}", wpm);
    draw_text_ex(
        "wpm",
        x,
        y,
        TextParams {
            font,
            font_size: fontsize_2,
            color: color_scheme.ref_color(),
            ..Default::default()
        },
    );
    draw_text_ex(
        &wpm_text,
        x,
        y + fontsize_1 as f32,
        TextParams {
            font,
            font_size: fontsize_1,
            color: color_scheme.main_color(),
            ..Default::default()
        },
    );
    wpm
}

fn write_acc(
    accuracy: f64,
    font: Option<&Font>,
    x: f32,
    y: f32,
    fontsize_1: u16,
    fontsize_2: u16,
    color_scheme: &ColorScheme,
) {
    let acc_text = format!("{:.0}%", accuracy);
    draw_text_ex(
        "acc",
        x,
        y,
        TextParams {
            font,
            font_size: fontsize_2,
            color: color_scheme.ref_color(),
            ..Default::default()
        },
    );
    draw_text_ex(
        &acc_text,
        x,
        y + fontsize_1 as f32,
        TextParams {
            font,
            font_size: fontsize_1,
            color: color_scheme.main_color(),
            ..Default::default()
        },
    );
}

fn write_raw_wpm(
    raw_wpm: f32,
    font: Option<&Font>,
    x: f32,
    y: f32,
    fontsize_3: u16,
    fontsize_4: u16,
    color_scheme: &ColorScheme,
) {
    let raw_text = format!("{:.0}", raw_wpm);
    draw_text_ex(
        "raw wpm",
        x,
        y,
        TextParams {
            font,
            font_size: fontsize_4,
            color: color_scheme.ref_color(),
            ..Default::default()
        },
    );
    draw_text_ex(
        &raw_text,
        x,
        y + fontsize_3 as f32,
        TextParams {
            font,
            font_size: fontsize_3,
            color: color_scheme.main_color(),
            ..Default::default()
        },
    );
}

fn smooth(values: &[f64], window: usize, average_word_length: f64) -> Vec<f64> {
    let len = values.len();
    let mut smoothed = Vec::with_capacity(len + 1);
    smoothed.push(0.0);

    for i in 0..len {
        let start = i.saturating_sub(window);
        let end = (i + window + 1).min(len);
        let slice = &values[start..end];

        let avg = slice.iter().sum::<f64>() / slice.len() as f64 / average_word_length;
        smoothed.push(avg);
    }
    smoothed[0] = smoothed[1];

    smoothed
}

fn draw_chart(
    points: &[[f64; 2]],
    chart_width: f32,
    chart_height: f32,
    chart_x: f32,
    chart_y: f32,
    errors_per_second: &[f64],
    fontsize_1: u16,
    color_scheme: &ColorScheme,
) {
    let mut errors: Vec<f64> = Vec::new();
    errors.push(0.0);
    errors.extend(errors_per_second.iter().cloned());

    egui_macroquad::ui(|ctx| {
        Area::new("chart_area".into())
            .fixed_pos(pos2(chart_x, chart_y))
            .show(ctx, |ui| {
                let size = egui::Vec2::new(chart_width, chart_height);
                let (rect, _response) = ui.allocate_exact_size(size, egui::Sense::hover());

                let mut child_ui =
                    ui.new_child(egui::UiBuilder::new().max_rect(rect).layout(*ui.layout()));

                let grid_spacer = |input: egui_plot::GridInput| -> Vec<egui_plot::GridMark> {
                    let min = input.bounds.0;
                    let max = input.bounds.1;
                    let step = 20.0;
                    let mut marks = Vec::new();

                    let mut current = (min / step).ceil() * step;
                    while current <= max {
                        marks.push(egui_plot::GridMark {
                            value: current,
                            step_size: step,
                        });
                        current += step;
                    }
                    marks
                };

                let max_x = f64::max(points.len() as f64 - 1.0, 5.0);
                let mut max_y = 50.0;
                for point in points {
                    if point[1] > max_y {
                        max_y = point[1];
                    }
                }

                Plot::new("performance_plot")
                    .include_y(0.0)
                    .show_background(false)
                    .show_axes([true, true])
                    .show_grid(true)
                    .view_aspect(5.0)
                    .x_axis_label("Time (s)")
                    .y_axis_label("Speed (WPM)")
                    .y_grid_spacer(grid_spacer)
                    .default_x_bounds(0.8, max_x)
                    .default_y_bounds(0.0, 1.2 * max_y)
                    .show(&mut child_ui, |plot_ui| {
                        let line = Line::new("Performance", points.to_vec())
                            .color(ColorScheme::mq_to_color32(color_scheme.main_color()))
                            .highlight(true)
                            .name("Performance");
                        plot_ui.line(line);

                        let mut last_drawn_pixel_x: Option<f32> = None;
                        for (i, &val) in errors.iter().enumerate() {
                            if val > 0.0 {
                                if let Some(&[x, _]) = points.get(i) {
                                    let screen_pos =
                                        plot_ui.screen_from_plot(egui_plot::PlotPoint::new(x, 0.0));
                                    let current_pixel_x = screen_pos.x;

                                    let should_draw = match last_drawn_pixel_x {
                                        None => true,
                                        Some(last_x) => (current_pixel_x - last_x).abs() >= 10.0,
                                    };

                                    if should_draw {
                                        let size = if val <= 1.0 {
                                            fontsize_1 as f32 / 15.0
                                        } else if val <= 2.0 {
                                            fontsize_1 as f32 / 10.0
                                        } else {
                                            fontsize_1 as f32 / 4.0
                                        };

                                        let cross_y = max_y / 7.0;

                                        let cross =
                                            egui_plot::Points::new("Error", vec![[x, cross_y]])
                                                .color(ColorScheme::mq_to_color32(
                                                    color_scheme.incorrect_color(),
                                                ))
                                                .radius(size)
                                                .shape(egui_plot::MarkerShape::Cross);

                                        plot_ui.points(cross);
                                        last_drawn_pixel_x = Some(current_pixel_x);
                                    }
                                }
                            }
                        }
                    });
            });
    });
}