Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
module tt
module github.com/jmonroynieto/tt_withTab

go 1.17
go 1.21.0

require (
github.com/gdamore/tcell v1.4.0
github.com/mattn/go-isatty v0.0.14
github.com/gerow/go-color v0.0.0-20140219113758-125d37f527f1
github.com/mattn/go-isatty v0.0.19
)

require (
github.com/gdamore/encoding v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
github.com/lucasb-eyer/go-colorful v1.0.3 // indirect
github.com/mattn/go-runewidth v0.0.7 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.3.0 // indirect
)
16 changes: 6 additions & 10 deletions go.sum
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,16 @@ github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdk
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell v1.4.0 h1:vUnHwJRvcPQa3tzi+0QI4U9JINXYJlOz9yiaiPQ2wMU=
github.com/gdamore/tcell v1.4.0/go.mod h1:vxEiSDZdW3L+Uhjii9c3375IlDmR05bzxY404ZVSMo0=
github.com/gerow/go-color v0.0.0-20140219113758-125d37f527f1 h1:DSA78HTfGC442ChonW9NdWuH5rsfJjTwsYwfIZhYjFo=
github.com/gerow/go-color v0.0.0-20140219113758-125d37f527f1/go.mod h1:uN90NshmoiEU0ECs3cPdEg3wshS8kG9Zez9RmYPuL5A=
github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
6 changes: 3 additions & 3 deletions src/datatest.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

func generateTestFromData(data []byte, raw bool, split bool) func() []segment {
if raw {
return func() []segment { return []segment{segment{string(data), ""}} }
return func() []segment { return []segment{{string(data), "", ""}} }
} else if split {
paragraphs := getParagraphs(string(data))
i := 0
Expand All @@ -11,7 +11,7 @@ func generateTestFromData(data []byte, raw bool, split bool) func() []segment {
if i < len(paragraphs) {
p := paragraphs[i]
i++
return []segment{segment{p, ""}}
return []segment{{p, "", ""}}
} else {
return nil
}
Expand All @@ -21,7 +21,7 @@ func generateTestFromData(data []byte, raw bool, split bool) func() []segment {
var segments []segment

for _, p := range getParagraphs(string(data)) {
segments = append(segments, segment{p, ""})
segments = append(segments, segment{p, "", ""})
}

return segments
Expand Down
Empty file modified src/db.go
100644 → 100755
Empty file.
6 changes: 3 additions & 3 deletions src/filetest.go
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
"io/ioutil"
"os"
"path/filepath"
)

Expand All @@ -25,7 +25,7 @@ func generateTestFromFile(path string, startParagraph int) func() []segment {

idx := db[path] - 1

if b, err := ioutil.ReadFile(path); err != nil {
if b, err := os.ReadFile(path); err != nil {
die("Failed to read %s.", path)
} else {
paragraphs = getParagraphs(string(b))
Expand All @@ -40,6 +40,6 @@ func generateTestFromFile(path string, startParagraph int) func() []segment {
return nil
}

return []segment{segment{paragraphs[idx], ""}}
return []segment{{paragraphs[idx], "", ""}}
}
}
Empty file modified src/quotetest.go
100644 → 100755
Empty file.
68 changes: 45 additions & 23 deletions src/tt.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"io"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -95,14 +95,14 @@ func showReport(scr tcell.Screen, cpm, wpm int, accuracy float64, attribution st
if len(mistakes) > 0 {
mistakeStr = "\nMistakes: "
for i, m := range mistakes {
mistakeStr += m.Word
mistakeStr += fmt.Sprintf("%q", m.Word)
if i != len(mistakes)-1 {
mistakeStr += ", "
}
}
}

report := fmt.Sprintf("WPM: %d\nCPM: %d\nAccuracy: %.2f%%%s%s", wpm, cpm, accuracy, mistakeStr, attribution)
report := fmt.Sprintf("WPM: %d\nCPM: %d\nAccuracy: %.2f%%\n%s%s", wpm, cpm, accuracy, wordWrap(mistakeStr, 20), attribution)

scr.Clear()
drawStringAtCenter(scr, report, tcell.StyleDefault)
Expand Down Expand Up @@ -293,7 +293,7 @@ func main() {

if listFlag != "" {
prefix := listFlag + "/"
for path, _ := range packedFiles {
for path := range packedFiles {
if strings.Index(path, prefix) == 0 {
_, f := filepath.Split(path)
fmt.Println(f)
Expand All @@ -320,19 +320,33 @@ func main() {
wsz = sw - 8
}

s = regexp.MustCompile("\\s+").ReplaceAllString(s, " ")
return strings.Replace(
wordWrap(strings.Trim(s, " "), wsz),
"\n", " \n", -1)
s = regexp.MustCompile(`\s+`).ReplaceAllString(s, ` `)
X := wordWrap(strings.TrimSpace(s), wsz)
return X
}

rawflow := func(s string) string {
sw, sh := scr.Size()
wsz := maxLineLen
if wsz > sw {
wsz = sw - 8
}
s = regexp.MustCompile("[ ]{4}").ReplaceAllString(s, "\t")
return softWrap(s, wsz, sh-6)
}

// maskRendered := func(old, new, s string) string {
// return regexp.MustCompile(old).ReplaceAllString(s, new)

// }

switch {
case wordFile != "":
testFn = generateWordTest(wordFile, n, g)
case quoteFile != "":
testFn = generateQuoteTest(quoteFile)
case !isatty.IsTerminal(os.Stdin.Fd()):
b, err := ioutil.ReadAll(os.Stdin)
b, err := io.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -383,56 +397,64 @@ func main() {
typer.ShowWpm = showWpm

if timeout != -1 {
timeout *= 1E9
timeout *= 1e9
}

var tests [][]segment
var idx = 0
var exercise = 0

for {
if idx >= len(tests) {
if exercise >= len(tests) {
tests = append(tests, testFn())
}

if tests[idx] == nil {
if tests[exercise] == nil {
exit(0)
}

if !rawMode {
for i, _ := range tests[idx] {
tests[idx][i].Text = reflow(tests[idx][i].Text)
for i := range tests[exercise] {
tests[exercise][i].Text = reflow(tests[exercise][i].Text)
}
} else {
for i := range tests[exercise] {
masked := rawflow(tests[exercise][i].Text)
tests[exercise][i].SetRenderForm(masked)
spacesAsTabs := strings.ReplaceAll(tests[exercise][i].Text, " ", "\t")
tests[exercise][i].SetContent(spacesAsTabs)
}
}

nerrs, ncorrect, t, rc, mistakes := typer.Start(tests[idx], time.Duration(timeout))
nerrs, ncorrect, t, rc, mistakes := typer.Start(tests[exercise], time.Duration(timeout), rawMode)
saveMistakes(mistakes)

switch rc {
case TyperNext:
idx++
exercise++
case TyperPrevious:
if idx > 0 {
idx--
if exercise > 0 {
exercise--
}
case TyperComplete:
cpm := int(float64(ncorrect) / (float64(t) / 60E9))
cpm := int(float64(ncorrect) / (float64(t) / 60e9))
wpm := cpm / 5
accuracy := float64(ncorrect) / float64(nerrs+ncorrect) * 100

results = append(results, result{wpm, cpm, accuracy, time.Now().Unix(), mistakes})
if !noReport {
attribution := ""
if len(tests[idx]) == 1 {
attribution = tests[idx][0].Attribution
if len(tests[exercise]) == 1 {
attribution = tests[exercise][0].Attribution
}
showReport(scr, cpm, wpm, accuracy, attribution, mistakes)
}
if oneShotMode {
exit(0)
}

idx++
exercise++
case TyperSigInt:
//system call reset the terminal, so we need to reinitialize it
exit(1)

case TyperResize:
Expand Down
Loading