From a4e20623839c81701106b27d9785a48c625c0496 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Sun, 16 Feb 2025 00:56:53 +0200 Subject: [PATCH] Add HardMode --- colorizer/eraser.go | 7 +++++++ db/stat.go | 3 ++- main.go | 33 +++++++++++++++++++++++++-------- options.go | 8 ++++++-- reader/db_reader.go | 7 +++---- 5 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 colorizer/eraser.go diff --git a/colorizer/eraser.go b/colorizer/eraser.go new file mode 100644 index 0000000..5954268 --- /dev/null +++ b/colorizer/eraser.go @@ -0,0 +1,7 @@ +package colorizer + +import "fmt" + +func ClearFromCursorToEnd() { + fmt.Printf("\033[K") +} diff --git a/db/stat.go b/db/stat.go index 4cc32f2..ace888d 100644 --- a/db/stat.go +++ b/db/stat.go @@ -14,5 +14,6 @@ type Stat struct { CPM float64 WPM float64 // Time in milliseconds - TimeTaken int64 + TimeTaken int64 + InHardMode bool } diff --git a/main.go b/main.go index 27bdcc4..30b7525 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "log" "os" "os/signal" + "strings" "syscall" "time" "tipitypy/colorizer" @@ -26,7 +27,8 @@ func finish(start time.Time, stat *db.Stat, finished bool) { stat.WPM = float64(globalStat.Words+1) / past.Minutes() stat.TimeTaken = past.Milliseconds() fmt.Printf("\r\n%s", colorizer.Colors.Reset()) - fmt.Println(past) + fmt.Print(past) + fmt.Printf("\r\n") } func ChooseToPrintColorized(r rune) string { @@ -46,10 +48,11 @@ const ( ) func SingleCycle() (*db.Stat, int, error) { - source, err := reader.GetSourceLine() + words, err := reader.GetSourceWords() if err != nil { return nil, ExitStateUnspecified, fmt.Errorf("reader.GetSourceLine: %w", err) } + source := strings.Join(words, " ") log.Print("Start of tipitypy") if Color { colorizer.PrintColorized(source) @@ -58,14 +61,18 @@ func SingleCycle() (*db.Stat, int, error) { } fmt.Printf("\r\n") // + wordIdx := 0 + wordRunesI := 0 p := []rune(source) i := 0 // - startTime := time.Now() finished := true - stat := &db.Stat{} - defer finish(startTime, stat, finished) + stat := &db.Stat{ + InHardMode: HardMode, + } reader := bufio.NewReader(os.Stdin) + startTime := time.Now() + defer finish(startTime, stat, finished) for { r, _, err := reader.ReadRune() if err != nil { @@ -96,16 +103,26 @@ func SingleCycle() (*db.Stat, int, error) { } // Check if p[i] == r { - if r == ' ' { - globalStat.Words++ - } globalStat.Correct++ fmt.Printf("%s", ChooseToPrintColorized(r)) i++ + wordRunesI++ + if r == ' ' { + globalStat.Words++ + wordIdx++ + wordRunesI = 0 + } if i == len(p) { break } } else { + if HardMode && r != ' ' { + log.Printf("removing %d runes", wordRunesI) + fmt.Printf("%s", strings.Repeat("\b", wordRunesI)) + colorizer.ClearFromCursorToEnd() + i -= wordRunesI + wordRunesI = 0 + } globalStat.False++ } } diff --git a/options.go b/options.go index 87e159c..5bc9233 100644 --- a/options.go +++ b/options.go @@ -5,11 +5,13 @@ import "os" var ( IsEndless = false Color = true + HardMode = false ) const ( - EndlessOpt = "-e" - NoColorOpt = "-nc" + EndlessOpt = "-e" + NoColorOpt = "-nc" + HardModeOpt = "--hard" ) func ParseOptions() { @@ -19,6 +21,8 @@ func ParseOptions() { IsEndless = true case NoColorOpt: Color = false + case HardModeOpt: + HardMode = true } } } diff --git a/reader/db_reader.go b/reader/db_reader.go index 5a594db..5aec0bc 100644 --- a/reader/db_reader.go +++ b/reader/db_reader.go @@ -2,16 +2,15 @@ package reader import ( "fmt" - "strings" "tipitypy/db" ) -func GetSourceLine() (string, error) { +func GetSourceWords() ([]string, error) { dbc := db.Connect() var res []string err := dbc.Raw(`SELECT value FROM words WHERE deleted_at IS NULL ORDER BY RANDOM() LIMIT 10;`).Scan(&res).Error if err != nil { - return "", fmt.Errorf("dbc.Raw: %w", err) + return nil, fmt.Errorf("dbc.Raw: %w", err) } - return strings.Join(res, " "), nil + return res, nil }