|
| 1 | +// Copyright 2024 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/textarea" |
| 21 | + tea "github.com/charmbracelet/bubbletea" |
| 22 | + "github.com/tidbcloud/tidbcloud-cli/internal/util" |
| 23 | +) |
| 24 | + |
| 25 | +type TextAreaModel struct { |
| 26 | + Textarea textarea.Model |
| 27 | + Err error |
| 28 | + Interrupted bool |
| 29 | +} |
| 30 | + |
| 31 | +func InitialTextAreaModel(placeholder string) (TextAreaModel, error) { |
| 32 | + ta := textarea.New() |
| 33 | + ta.Placeholder = placeholder |
| 34 | + ta.Focus() |
| 35 | + ta.SetWidth(80) |
| 36 | + ta.SetHeight(20) |
| 37 | + ta.ShowLineNumbers = false |
| 38 | + ta.CharLimit = 0 |
| 39 | + |
| 40 | + p := tea.NewProgram(TextAreaModel{Textarea: ta}) |
| 41 | + model, err := p.Run() |
| 42 | + finalModel := model.(TextAreaModel) |
| 43 | + if err != nil { |
| 44 | + return finalModel, err |
| 45 | + } |
| 46 | + if finalModel.Interrupted { |
| 47 | + return finalModel, util.InterruptError |
| 48 | + } |
| 49 | + if finalModel.Err != nil { |
| 50 | + return finalModel, finalModel.Err |
| 51 | + } |
| 52 | + return finalModel, nil |
| 53 | +} |
| 54 | + |
| 55 | +func (m TextAreaModel) Init() tea.Cmd { |
| 56 | + return textarea.Blink |
| 57 | +} |
| 58 | + |
| 59 | +func (m TextAreaModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 60 | + var cmds []tea.Cmd |
| 61 | + var cmd tea.Cmd |
| 62 | + |
| 63 | + switch msg := msg.(type) { |
| 64 | + case tea.KeyMsg: |
| 65 | + switch msg.Type { |
| 66 | + case tea.KeyEsc: |
| 67 | + if m.Textarea.Focused() { |
| 68 | + m.Textarea.Blur() |
| 69 | + } |
| 70 | + case tea.KeyCtrlS: |
| 71 | + return m, tea.Quit |
| 72 | + case tea.KeyCtrlC: |
| 73 | + m.Interrupted = true |
| 74 | + return m, tea.Quit |
| 75 | + default: |
| 76 | + if !m.Textarea.Focused() { |
| 77 | + cmd = m.Textarea.Focus() |
| 78 | + cmds = append(cmds, cmd) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // We handle errors just like any other message |
| 83 | + case errMsg: |
| 84 | + m.Err = msg |
| 85 | + return m, nil |
| 86 | + } |
| 87 | + |
| 88 | + m.Textarea, cmd = m.Textarea.Update(msg) |
| 89 | + cmds = append(cmds, cmd) |
| 90 | + return m, tea.Batch(cmds...) |
| 91 | +} |
| 92 | + |
| 93 | +func (m TextAreaModel) View() string { |
| 94 | + return fmt.Sprintf( |
| 95 | + "%s\n\n%s\n\n", |
| 96 | + m.Textarea.View(), |
| 97 | + helpMessageStyle("Press Ctrl+S to save and quit"), |
| 98 | + ) |
| 99 | +} |
0 commit comments