92 lines
1.7 KiB
Go
92 lines
1.7 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
"tipitypy/colorizer"
|
|
"tipitypy/reader"
|
|
|
|
"golang.org/x/term"
|
|
)
|
|
|
|
func finish(start time.Time) {
|
|
past := time.Now().Sub(start)
|
|
fmt.Printf("\r\n%s", colorizer.Colors.Reset())
|
|
fmt.Printf("Correct: %d ; False: %d ; Skipped: %d\r\n", globalStat.Correct, globalStat.False, globalStat.Skipped)
|
|
fmt.Println(past)
|
|
fmt.Print("\r\n")
|
|
fmt.Printf("CPM: %.2f ;; WPM: %.2f\r\n",
|
|
float64(globalStat.Correct)/past.Minutes(),
|
|
float64(globalStat.Words+1)/past.Minutes())
|
|
}
|
|
|
|
func main() {
|
|
logFile, err := os.OpenFile("ttt.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer logFile.Close()
|
|
log.SetOutput(logFile)
|
|
source, err := reader.GetSourceLine()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
log.Print("Start of tipitypy")
|
|
colorizer.PrintColorized(source)
|
|
fmt.Printf("\n")
|
|
//
|
|
// Put the terminal in raw mode to read characters as they are typed
|
|
oldState, err := term.MakeRaw(int(os.Stdin.Fd()))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer term.Restore(int(os.Stdin.Fd()), oldState) // Restore terminal state at the end
|
|
//
|
|
p := []rune(source)
|
|
i := 0
|
|
//
|
|
startTime := time.Now()
|
|
defer finish(startTime)
|
|
reader := bufio.NewReader(os.Stdin)
|
|
for {
|
|
r, _, err := reader.ReadRune()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
log.Printf("Read %c ; %d as rune", r, r)
|
|
|
|
// CTRL + C
|
|
if r == 3 {
|
|
break
|
|
}
|
|
// CTRL + D
|
|
if r == 4 {
|
|
// TODO: Delete
|
|
}
|
|
// CTRL + S
|
|
if r == 19 {
|
|
globalStat.Skipped++
|
|
fmt.Printf("%s", colorizer.Accept(p[i]))
|
|
i++
|
|
continue
|
|
}
|
|
// Check
|
|
if p[i] == r {
|
|
if r == ' ' {
|
|
globalStat.Words++
|
|
}
|
|
globalStat.Correct++
|
|
fmt.Printf("%s", colorizer.Accept(r))
|
|
i++
|
|
if i == len(p) {
|
|
break
|
|
}
|
|
} else {
|
|
globalStat.False++
|
|
}
|
|
}
|
|
}
|