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 authored and rusinikita committed May 4, 2024
1 parent ab614eb commit f0529e0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
5 changes: 5 additions & 0 deletions choose/choose.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"strings"
"time"

"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
Expand All @@ -22,6 +23,7 @@ func New() tea.Model {
listModel := list.New(nil, itemDelegate{}, 20, 14)
listModel.SetFilteringEnabled(false)
listModel.SetShowStatusBar(false)
listModel.StatusMessageLifetime = 5 * time.Second
listModel.Title = "Select challenge"

return choose{
Expand Down Expand Up @@ -61,6 +63,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.RenderLayoutErr(msg))
}
if msg.String() == "enter" {
return play.New(
challenge.Challenge(c.list.SelectedItem().(item)),
Expand Down
14 changes: 13 additions & 1 deletion play/bindings.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package play

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

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

type keyBindings struct {
short key.Binding
Expand Down Expand Up @@ -53,6 +58,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(RenderLayoutErr(msg))
case key.Matches(msg, m.b.Next):
if m.question.questionFullyAnswered() && (!m.question.isLast) {
m.currentQuestion++
Expand Down
20 changes: 19 additions & 1 deletion play/style.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
package play

import "github.com/charmbracelet/lipgloss"
import (
"fmt"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

var (
questionNumberStyle = lipgloss.NewStyle().BorderStyle(lipgloss.RoundedBorder()).Padding(0, 1)
titleStyle = lipgloss.NewStyle().Margin(1)
answerStyle = lipgloss.NewStyle().
PaddingLeft(1).PaddingRight(1).
BorderStyle(lipgloss.NormalBorder())
warningMessageStyle = lipgloss.NewStyle().
Foreground(lipgloss.Color("231")).
Background(lipgloss.Color("166")).
Padding(0, 1)
answerSelectedStyle = answerStyle.Copy().
Foreground(lipgloss.Color("170")).
BorderStyle(lipgloss.DoubleBorder()).
Expand Down Expand Up @@ -42,3 +51,12 @@ var (
Foreground(lipgloss.Color("170")).
BorderForeground(lipgloss.Color("170"))
)

func RenderLayoutErr(msg tea.KeyMsg) string {
return warningMessageStyle.Render(
fmt.Sprintf(
"'%s' have been pressed. Please, change keyboard layout to english.",
msg.String(),
),
)
}

0 comments on commit f0529e0

Please sign in to comment.