Skip to content

Commit

Permalink
Add position from which file is loaded to progress computation
Browse files Browse the repository at this point in the history
  • Loading branch information
bunyk committed Mar 8, 2021
1 parent 55c588d commit 5955177
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
7 changes: 5 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ type App struct {
InputPosition int
ErrorInput []rune
StartedAt time.Time
Zen bool
Mute bool
Offset int

Zen bool
Mute bool

scr tcell.Screen
}
Expand Down Expand Up @@ -95,6 +97,7 @@ func (a App) ToDisplay() view.DisplayableData {
TODOText: a.Text[a.InputPosition:],
StartedAt: a.StartedAt,
Zen: a.Zen,
Offset: a.Offset,
}
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ var textCmd = &cobra.Command{
Short: "train to type contents of some file",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
text, err := phrase.FromFile(args[0], offset, limit)
text, skipped, err := phrase.FromFile(args[0], offset, limit)
fatal(err)

a, err := app.New(text)
fatal(err)
a.Zen = zen
a.Mute = mute
a.Offset = skipped

a.Run()
fatal(err)
Expand Down
14 changes: 8 additions & 6 deletions phrase/phrase.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ import (
"path/filepath"
"strings"
"time"
"unicode/utf8"

"github.com/bunyk/gokeybr/fs"
)

func FromFile(filename string, offset, minLength int) (string, error) {
items, err := readFileLines(filename, offset)
func FromFile(filename string, offset, minLength int) (string, int, error) {
items, skipped, err := readFileLines(filename, offset)
if err != nil {
return "", err
return "", skipped, err
}
items = slice(items, minLength)
return strings.Join(items, "\n"), nil
return strings.Join(items, "\n"), skipped, nil
}

func Words(filename string, n int) (string, error) {
words, err := readFileLines(filename, 0)
words, _, err := readFileLines(filename, 0)
if err != nil {
return "", err
}
Expand All @@ -38,7 +39,7 @@ func Words(filename string, n int) (string, error) {
return strings.Join(phrase, " "), nil
}

func readFileLines(filename string, offset int) (lines []string, err error) {
func readFileLines(filename string, offset int) (lines []string, skipped int, err error) {
var data []byte
if filename == "-" {
data, err = ioutil.ReadAll(os.Stdin)
Expand Down Expand Up @@ -69,6 +70,7 @@ func readFileLines(filename string, offset int) (lines []string, err error) {
}
if skip > 0 {
skip--
skipped += utf8.RuneCountInString(line) + 1
} else {
lines = append(lines, line[:len(line)-1])
}
Expand Down
3 changes: 2 additions & 1 deletion view/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type DisplayableData struct {
TODOText []rune
StartedAt time.Time
Zen bool
Offset int
}

func Render(s tcell.Screen, dd DisplayableData) {
Expand All @@ -39,7 +40,7 @@ func Render(s tcell.Screen, dd DisplayableData) {
// Stats:
seconds := 0.0
wpm := 0.0
done := float64(len(dd.DoneText))
done := float64(len(dd.DoneText) + dd.Offset)
if !dd.StartedAt.IsZero() {
seconds = time.Since(dd.StartedAt).Seconds()
wpm = wordsPerChar * done / seconds * 60.0
Expand Down

0 comments on commit 5955177

Please sign in to comment.