From 540801aaf7f833c4132f4bbab06846cb1332134a Mon Sep 17 00:00:00 2001 From: Andrei Gonchar Date: Sat, 4 May 2024 14:22:42 +0300 Subject: [PATCH] Add status message about wrong layout --- choose/choose.go | 3 +++ play/bindings.go | 16 +++++++++++++++- play/play.go | 2 ++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/choose/choose.go b/choose/choose.go index b7830a7..6a5a17f 100644 --- a/choose/choose.go +++ b/choose/choose.go @@ -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)), diff --git a/play/bindings.go b/play/bindings.go index 5f83dcb..137eea8 100644 --- a/play/bindings.go +++ b/play/bindings.go @@ -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 @@ -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) diff --git a/play/play.go b/play/play.go index c7f7a88..3b9f3eb 100644 --- a/play/play.go +++ b/play/play.go @@ -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++