Add HardMode

This commit is contained in:
qowevisa 2025-02-16 00:56:53 +02:00
parent bbceeee889
commit a4e2062383
5 changed files with 43 additions and 15 deletions

7
colorizer/eraser.go Normal file
View File

@ -0,0 +1,7 @@
package colorizer
import "fmt"
func ClearFromCursorToEnd() {
fmt.Printf("\033[K")
}

View File

@ -15,4 +15,5 @@ type Stat struct {
WPM float64 WPM float64
// Time in milliseconds // Time in milliseconds
TimeTaken int64 TimeTaken int64
InHardMode bool
} }

33
main.go
View File

@ -6,6 +6,7 @@ import (
"log" "log"
"os" "os"
"os/signal" "os/signal"
"strings"
"syscall" "syscall"
"time" "time"
"tipitypy/colorizer" "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.WPM = float64(globalStat.Words+1) / past.Minutes()
stat.TimeTaken = past.Milliseconds() stat.TimeTaken = past.Milliseconds()
fmt.Printf("\r\n%s", colorizer.Colors.Reset()) fmt.Printf("\r\n%s", colorizer.Colors.Reset())
fmt.Println(past) fmt.Print(past)
fmt.Printf("\r\n")
} }
func ChooseToPrintColorized(r rune) string { func ChooseToPrintColorized(r rune) string {
@ -46,10 +48,11 @@ const (
) )
func SingleCycle() (*db.Stat, int, error) { func SingleCycle() (*db.Stat, int, error) {
source, err := reader.GetSourceLine() words, err := reader.GetSourceWords()
if err != nil { if err != nil {
return nil, ExitStateUnspecified, fmt.Errorf("reader.GetSourceLine: %w", err) return nil, ExitStateUnspecified, fmt.Errorf("reader.GetSourceLine: %w", err)
} }
source := strings.Join(words, " ")
log.Print("Start of tipitypy") log.Print("Start of tipitypy")
if Color { if Color {
colorizer.PrintColorized(source) colorizer.PrintColorized(source)
@ -58,14 +61,18 @@ func SingleCycle() (*db.Stat, int, error) {
} }
fmt.Printf("\r\n") fmt.Printf("\r\n")
// //
wordIdx := 0
wordRunesI := 0
p := []rune(source) p := []rune(source)
i := 0 i := 0
// //
startTime := time.Now()
finished := true finished := true
stat := &db.Stat{} stat := &db.Stat{
defer finish(startTime, stat, finished) InHardMode: HardMode,
}
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
startTime := time.Now()
defer finish(startTime, stat, finished)
for { for {
r, _, err := reader.ReadRune() r, _, err := reader.ReadRune()
if err != nil { if err != nil {
@ -96,16 +103,26 @@ func SingleCycle() (*db.Stat, int, error) {
} }
// Check // Check
if p[i] == r { if p[i] == r {
if r == ' ' {
globalStat.Words++
}
globalStat.Correct++ globalStat.Correct++
fmt.Printf("%s", ChooseToPrintColorized(r)) fmt.Printf("%s", ChooseToPrintColorized(r))
i++ i++
wordRunesI++
if r == ' ' {
globalStat.Words++
wordIdx++
wordRunesI = 0
}
if i == len(p) { if i == len(p) {
break break
} }
} else { } 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++ globalStat.False++
} }
} }

View File

@ -5,11 +5,13 @@ import "os"
var ( var (
IsEndless = false IsEndless = false
Color = true Color = true
HardMode = false
) )
const ( const (
EndlessOpt = "-e" EndlessOpt = "-e"
NoColorOpt = "-nc" NoColorOpt = "-nc"
HardModeOpt = "--hard"
) )
func ParseOptions() { func ParseOptions() {
@ -19,6 +21,8 @@ func ParseOptions() {
IsEndless = true IsEndless = true
case NoColorOpt: case NoColorOpt:
Color = false Color = false
case HardModeOpt:
HardMode = true
} }
} }
} }

View File

@ -2,16 +2,15 @@ package reader
import ( import (
"fmt" "fmt"
"strings"
"tipitypy/db" "tipitypy/db"
) )
func GetSourceLine() (string, error) { func GetSourceWords() ([]string, error) {
dbc := db.Connect() dbc := db.Connect()
var res []string var res []string
err := dbc.Raw(`SELECT value FROM words WHERE deleted_at IS NULL ORDER BY RANDOM() LIMIT 10;`).Scan(&res).Error err := dbc.Raw(`SELECT value FROM words WHERE deleted_at IS NULL ORDER BY RANDOM() LIMIT 10;`).Scan(&res).Error
if err != nil { 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
} }