From d2997ad522b8c2ace37f8e32730e83bc233d8f5b Mon Sep 17 00:00:00 2001 From: Andrei Gonchar Date: Fri, 3 May 2024 22:52:06 +0300 Subject: [PATCH] Add back key --- choose/choose.go | 3 +++ play/bindings.go | 3 +++ play/play.go | 10 ++++++++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/choose/choose.go b/choose/choose.go index 2f4ec3e..b7830a7 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.WindowSizeMsg) (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..c7f7a88 100644 --- a/play/play.go +++ b/play/play.go @@ -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) @@ -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, } @@ -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):