-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
text_input.go
88 lines (73 loc) · 2.11 KB
/
text_input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// text_input.go contains the textInputModel struct which is used to render the text input view.
package main
import (
"fmt"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/sammcj/gollama/logging"
)
type textInputModel struct {
textInput textinput.Model
oldName string
quitting bool
}
// promptForNewName displays a text input prompt for renaming a model.
func promptForNewName(oldName string) string {
ti := textinput.New()
// print 'renaming oldName' to the console with the oldName in purple
ti.Prompt = oldName + "\n" + "Name for new model: "
ti.Placeholder = oldName
ti.Focus()
ti.KeyMap.AcceptSuggestion = key.NewBinding(key.WithKeys("tab"))
ti.SetSuggestions([]string{oldName})
ti.ShowSuggestions = true
ti.CharLimit = 300
ti.Width = 140
ti.PromptStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FF00FF"))
ti.TextStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FF00FF"))
ti.Cursor.Style = lipgloss.NewStyle().Background(lipgloss.Color("#4E00FF")).Background(lipgloss.Color("#111111"))
ti.PlaceholderStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#AD00FF"))
m := textInputModel{
textInput: ti,
oldName: oldName,
}
p := tea.NewProgram(&m)
if _, err := p.Run(); err != nil {
logging.ErrorLogger.Printf("Error starting text input program: %v\n", err)
}
newName := m.textInput.Value()
if newName == "" {
// error handling
logging.ErrorLogger.Println("No new name entered, returning old name")
return oldName
}
return newName
}
func (m *textInputModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "enter":
m.quitting = true
return m, tea.Quit
}
}
m.textInput, cmd = m.textInput.Update(msg)
return m, cmd
}
func (m textInputModel) Init() tea.Cmd {
return textinput.Blink
}
func (m textInputModel) View() string {
if m.quitting {
return ""
}
return fmt.Sprintf(
"\n%s\n\n%s",
m.textInput.View(),
"(ctrl+c to cancel)",
)
}