From 29163594cfa165a72993c9f2ac225f6dca63aa70 Mon Sep 17 00:00:00 2001 From: Andrei Gonchar Date: Fri, 3 May 2024 22:52:06 +0300 Subject: [PATCH] add back hotkey --- choose/choose.go | 3 +++ play/bindings.go | 3 +++ play/play.go | 22 ++++++++++++++-------- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/choose/choose.go b/choose/choose.go index 2f4ec3e..9a13ad4 100644 --- a/choose/choose.go +++ b/choose/choose.go @@ -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.Msg) (m tea.Model, cmd tea.Cmd) { + return c.Update(msg) + }, ) } } diff --git a/play/bindings.go b/play/bindings.go index 46cd62f..5f83dcb 100644 --- a/play/bindings.go +++ b/play/bindings.go @@ -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 @@ -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, @@ -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"), diff --git a/play/play.go b/play/play.go index fb14c1d..c1cf656 100644 --- a/play/play.go +++ b/play/play.go @@ -28,13 +28,17 @@ type model struct { question questionModel learn learn + parentCallback Callback + 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 Callback func(tea.Msg) (tea.Model, tea.Cmd) + +func New(c challenge.Challenge, width, height int, callback Callback) (tea.Model, tea.Cmd) { // normalize line endings codeLines := lines(c.DefaultCodeSnippet) @@ -66,12 +70,13 @@ func New(c challenge.Challenge, width, height int) (tea.Model, tea.Cmd) { Width: width, Height: height, }, - l: l, - challengeName: c.Name, - questions: c.Questions, - question: newQuestionModel(c.Questions[0], len(c.Questions) == 1), - learn: newLearn(c.LearningAdvise, c.LearningLinks), - help: helpModel, + l: l, + challengeName: c.Name, + questions: c.Questions, + question: newQuestionModel(c.Questions[0], len(c.Questions) == 1), + learn: newLearn(c.LearningAdvise, c.LearningLinks), + parentCallback: callback, + help: helpModel, } return m.updateListSize(), cmd @@ -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.parentCallback(m.w) case key.Matches(msg, m.b.Help): m.help.ShowAll = !m.help.ShowAll case key.Matches(msg, m.b.Quit):