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 support of returning to challenge list #11

Merged
merged 1 commit into from
May 3, 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
3 changes: 3 additions & 0 deletions choose/choose.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func (c choose) Update(msg tea.Msg) (m tea.Model, cmd tea.Cmd) {
challenge.Challenge(c.list.SelectedItem().(item)),
c.list.Width()+listStyle.GetHorizontalFrameSize(),
c.list.Height()+listStyle.GetVerticalFrameSize(),
func(msg tea.WindowSizeMsg) (m tea.Model, cmd tea.Cmd) {
return c.Update(msg)
},
)
}
}
Expand Down
3 changes: 3 additions & 0 deletions play/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type keyBindings struct {
Right key.Binding
Input key.Binding
Next key.Binding
Back key.Binding
Help key.Binding
Learn key.Binding
CloseLearn key.Binding
Expand All @@ -25,6 +26,7 @@ func (k keyBindings) FullHelp() [][]key.Binding {
return [][]key.Binding{
{k.Left, k.Right, k.Up, k.Down, k.Input, k.Next},
{
k.Back,
copyKey(k.Help, "close bindings help"),
k.Learn,
k.CloseLearn,
Expand All @@ -42,6 +44,7 @@ func newBindings() keyBindings {
Right: key.NewBinding(key.WithKeys("right", "d"), key.WithHelp("→/d", "right answer")),
Input: key.NewBinding(key.WithKeys("enter", " ", "f"), key.WithHelp("⮐ / /f", "select answer/open link")),
Next: key.NewBinding(key.WithKeys("n"), key.WithHelp("N", "next answer")),
Back: key.NewBinding(key.WithKeys("backspace", "b"), key.WithHelp("⌫/b", "return to challenge list")),
Help: key.NewBinding(key.WithKeys("h", "?"), key.WithHelp("h/?", "see key bindings")),
Learn: learnB,
CloseLearn: copyDisabled(learnB, "close learn info"),
Expand Down
10 changes: 8 additions & 2 deletions play/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ type model struct {
question questionModel
learn learn

backHandler BackHandler

help help.Model
}

const copyStatus = "Code has copied to clipboard"
const copyErrStatus = "Code has not copied. Please install xsel, xclip, wl-clipboard or Termux:API"

func New(c challenge.Challenge, width, height int) (tea.Model, tea.Cmd) {
type BackHandler func(tea.WindowSizeMsg) (tea.Model, tea.Cmd)

func New(c challenge.Challenge, width, height int, backHandler BackHandler) (tea.Model, tea.Cmd) {
// normalize line endings
codeLines := lines(c.DefaultCodeSnippet)

Expand Down Expand Up @@ -71,6 +75,7 @@ func New(c challenge.Challenge, width, height int) (tea.Model, tea.Cmd) {
questions: c.Questions,
question: newQuestionModel(c.Questions[0], len(c.Questions) == 1),
learn: newLearn(c.LearningAdvise, c.LearningLinks),
backHandler: backHandler,
help: helpModel,
}

Expand Down Expand Up @@ -140,7 +145,8 @@ func (m model) Update(msg tea.Msg) (r tea.Model, cmd tea.Cmd) {
m.currentQuestion == len(m.questions)-1,
)
}

case key.Matches(msg, m.b.Back):
return m.backHandler(m.w)
case key.Matches(msg, m.b.Help):
m.help.ShowAll = !m.help.ShowAll
case key.Matches(msg, m.b.Quit):
Expand Down