Skip to content

Commit

Permalink
fix: stopwatch continued before new attempt + no more ctrl+w in-test
Browse files Browse the repository at this point in the history
When reseting with Enter, stopwatch would _only_ reset, not stop,
therefore continuing before starting the new attempt

Also, do not allow Ctrl+W mid-test
  • Loading branch information
marcelohdez committed Oct 10, 2024
1 parent c52d8e9 commit ae9689f
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions internal/views/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ type TestView struct {
}

func NewTestView(width int, height int) *TestView {
source := source.NewWordsSource("common-words-en.list", 20)
tv := TestView{
source: source.NewWordsSource("common-words-en.list", 20),
text: source.Generate(),
source: source,
stopwatch: stopwatch.New(),
width: width,
height: height,
}
tv.GenerateTest()

return &tv
}
Expand All @@ -42,20 +43,24 @@ func (tv TestView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyEsc:
return NewTitleView(tv.width, tv.height), nil
case tea.KeyEnter:
tv.GenerateTest()
return tv.GenerateTest()
case tea.KeyBackspace:
if len(tv.typed) > 0 {
tv.typed = tv.typed[:len(tv.typed)-1]
}
case tea.KeyCtrlW:
if tv.stopwatch.Running() {
break
}

if ws, ok := tv.source.(*source.WordsSource); ok {
ws.Count *= 2

if ws.Count > 80 {
ws.Count = 10
}

tv.GenerateTest()
return tv.GenerateTest()
}
default:
if len(tv.typed) < len(tv.text) {
Expand Down Expand Up @@ -135,8 +140,12 @@ func (tv TestView) View() string {
{key: "Enter", cmd: "restart"},
{key: "Control+C", cmd: "quit"},
}
if ws, ok := tv.source.(*source.WordsSource); ok {
cmds = append(cmds, keybind{key: "Control+W", cmd: fmt.Sprintf("word count (%d)", ws.Count)})

// commands only available while not in a test
if !tv.stopwatch.Running() {
if ws, ok := tv.source.(*source.WordsSource); ok {
cmds = append(cmds, keybind{key: "Control+W", cmd: fmt.Sprintf("word count (%d)", ws.Count)})
}
}

container := styleDefault.Height(tv.height-StatusBarHeight).Width(tv.width).Align(lipgloss.Center, lipgloss.Center).Render(styledText)
Expand Down Expand Up @@ -174,8 +183,10 @@ func (tv TestView) ShouldEndTest() bool {
return false
}

func (tv *TestView) GenerateTest() {
func (tv TestView) GenerateTest() (TestView, tea.Cmd) {
tv.text = tv.source.Generate()
tv.stopwatch.Reset()
tv.typed = ""

cmds := tea.Batch(tv.stopwatch.Stop(), tv.stopwatch.Reset())
return tv, cmds
}

0 comments on commit ae9689f

Please sign in to comment.