Skip to content

Commit

Permalink
Add status message about wrong layout
Browse files Browse the repository at this point in the history
  • Loading branch information
mortum5 committed May 4, 2024
1 parent ab614eb commit 540801a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
3 changes: 3 additions & 0 deletions choose/choose.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func (c choose) Update(msg tea.Msg) (m tea.Model, cmd tea.Cmd) {
)

case tea.KeyMsg:
if play.ValidateBindings(msg) {
return c, c.list.NewStatusMessage(play.LayoutErrStatus)
}
if msg.String() == "enter" {
return play.New(
challenge.Challenge(c.list.SelectedItem().(item)),
Expand Down
16 changes: 15 additions & 1 deletion play/bindings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package play

import "github.com/charmbracelet/bubbles/key"
import (
"unicode"

"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
)

const LayoutErrStatus = "Change keyboard layout to english"

type keyBindings struct {
short key.Binding
Expand Down Expand Up @@ -53,6 +60,13 @@ func newBindings() keyBindings {
}
}

func ValidateBindings(msg tea.KeyMsg) bool {
return msg.Type == tea.KeyRunes &&
len(msg.Runes) == 1 &&
unicode.IsLetter(msg.Runes[0]) &&
!unicode.Is(unicode.Latin, msg.Runes[0])
}

func copyKey(k key.Binding, desc string) key.Binding {
k.SetHelp(k.Help().Key, desc)

Expand Down
2 changes: 2 additions & 0 deletions play/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ func (m model) Update(msg tea.Msg) (r tea.Model, cmd tea.Cmd) {

case tea.KeyMsg:
switch {
case ValidateBindings(msg):
return m, m.l.NewStatusMessage(LayoutErrStatus)
case key.Matches(msg, m.b.Next):
if m.question.questionFullyAnswered() && (!m.question.isLast) {
m.currentQuestion++
Expand Down

0 comments on commit 540801a

Please sign in to comment.