|
| 1 | +// Copyright 2026 PingCAP, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package ui |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/charmbracelet/bubbles/textinput" |
| 21 | + tea "github.com/charmbracelet/bubbletea" |
| 22 | + "github.com/juju/errors" |
| 23 | + "github.com/tidbcloud/tidbcloud-cli/internal/config" |
| 24 | + "github.com/tidbcloud/tidbcloud-cli/internal/util" |
| 25 | +) |
| 26 | + |
| 27 | +type TextOneInputModel struct { |
| 28 | + Prompt string |
| 29 | + Input textinput.Model |
| 30 | + Err error |
| 31 | + Interrupted bool |
| 32 | +} |
| 33 | + |
| 34 | +func (m TextOneInputModel) Init() tea.Cmd { |
| 35 | + return textinput.Blink |
| 36 | +} |
| 37 | + |
| 38 | +func (m TextOneInputModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 39 | + var cmd tea.Cmd |
| 40 | + |
| 41 | + switch msg := msg.(type) { |
| 42 | + case tea.KeyMsg: |
| 43 | + switch msg.Type { |
| 44 | + case tea.KeyEnter: |
| 45 | + return m, tea.Quit |
| 46 | + case tea.KeyCtrlC, tea.KeyEsc: |
| 47 | + m.Interrupted = true |
| 48 | + return m, tea.Quit |
| 49 | + } |
| 50 | + |
| 51 | + case errMsg: |
| 52 | + m.Err = msg |
| 53 | + return m, nil |
| 54 | + } |
| 55 | + |
| 56 | + m.Input, cmd = m.Input.Update(msg) |
| 57 | + return m, cmd |
| 58 | +} |
| 59 | + |
| 60 | +func (m TextOneInputModel) View() string { |
| 61 | + return fmt.Sprintf( |
| 62 | + "%s\n\n%s\n\n%s\n\n", |
| 63 | + m.Prompt, |
| 64 | + m.Input.View(), |
| 65 | + helpMessageStyle("Press Enter to submit (esc to quit)"), |
| 66 | + ) |
| 67 | +} |
| 68 | + |
| 69 | +// InitialOneInputModel runs an interactive single-line input and returns the model and any error. |
| 70 | +// view is the prompt line shown above the input. placeholder is used as the input placeholder text. |
| 71 | +func InitialOneInputModel(prompt, placeholder string) (TextOneInputModel, error) { |
| 72 | + ti := textinput.New() |
| 73 | + ti.Placeholder = placeholder |
| 74 | + ti.Focus() |
| 75 | + ti.PromptStyle = config.FocusedStyle |
| 76 | + ti.TextStyle = config.FocusedStyle |
| 77 | + |
| 78 | + p := tea.NewProgram(TextOneInputModel{Prompt: prompt, Input: ti}) |
| 79 | + model, err := p.Run() |
| 80 | + finalModel := model.(TextOneInputModel) |
| 81 | + if err != nil { |
| 82 | + return finalModel, errors.Trace(err) |
| 83 | + } |
| 84 | + if finalModel.Interrupted { |
| 85 | + return finalModel, util.InterruptError |
| 86 | + } |
| 87 | + if finalModel.Err != nil { |
| 88 | + return finalModel, finalModel.Err |
| 89 | + } |
| 90 | + return finalModel, nil |
| 91 | +} |
0 commit comments