From d6a178e176c20a0ef1b245670b09df01783ade78 Mon Sep 17 00:00:00 2001 From: Christian Rocha Date: Thu, 20 Feb 2020 10:18:06 -0500 Subject: [PATCH] Quick 'n dirty submit button --- examples/inputs/main.go | 35 ++++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/examples/inputs/main.go b/examples/inputs/main.go index 584eec8..cfb3d89 100644 --- a/examples/inputs/main.go +++ b/examples/inputs/main.go @@ -6,13 +6,15 @@ import ( "github.com/charmbracelet/tea" "github.com/charmbracelet/teaparty/input" - "github.com/muesli/termenv" + te "github.com/muesli/termenv" ) var ( - color = termenv.ColorProfile().Color - focusedPrompt = termenv.String("> ").Foreground(color("205")).String() - blurredPrompt = termenv.String("> ").Foreground(color("244")).String() + color = te.ColorProfile().Color + focusedPrompt = te.String("> ").Foreground(color("205")).String() + blurredPrompt = "> " + focusedSubmitButton = "[ " + te.String("Submit").Foreground(color("205")).String() + " ]" + blurredSubmitButton = "[ " + te.String("Submit").Foreground(color("240")).String() + " ]" ) func main() { @@ -32,6 +34,7 @@ type Model struct { nameInput input.Model nickNameInput input.Model emailInput input.Model + submitButton string } func initialize() (tea.Model, tea.Cmd) { @@ -48,7 +51,7 @@ func initialize() (tea.Model, tea.Cmd) { email.Placeholder = "Email" email.Prompt = blurredPrompt - return Model{0, name, nickName, email}, nil + return Model{0, name, nickName, email, blurredSubmitButton}, nil } func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) { @@ -82,19 +85,27 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) { } s := msg.String() + + // Did the user press enter while the submit button was focused? + // If so, exit. + if s == "enter" && m.index == len(inputs) { + return m, tea.Quit + } + + // Cycle indexes if s == "up" || s == "shift+tab" { m.index-- } else { m.index++ } - if m.index > len(inputs)-1 { + if m.index > len(inputs) { m.index = 0 } else if m.index < 0 { - m.index = len(inputs) - 1 + m.index = len(inputs) } - for i := 0; i < len(inputs); i++ { + for i := 0; i <= len(inputs)-1; i++ { if i == m.index { inputs[i].Focus() inputs[i].Prompt = focusedPrompt @@ -108,6 +119,12 @@ func update(msg tea.Msg, model tea.Model) (tea.Model, tea.Cmd) { m.nickNameInput = inputs[1] m.emailInput = inputs[2] + if m.index == len(inputs) { + m.submitButton = focusedSubmitButton + } else { + m.submitButton = blurredSubmitButton + } + return m, nil default: @@ -155,7 +172,7 @@ func view(model tea.Model) string { } } - s += "\n" + s += "\n\n" + m.submitButton + "\n" return s }