Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add status message about wrong layout #12

Merged
merged 1 commit into from
May 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
rusinikita marked this conversation as resolved.
Show resolved Hide resolved
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(),
),
)
}