forked from sammcj/gollama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_input.go
83 lines (71 loc) · 1.89 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
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
}
// text_input.go (modified)
func promptForNewName(oldName string) string {
ti := textinput.New()
ti.ShowSuggestions = true
ti.CharLimit = 300
ti.Width = 60
ti.Placeholder = oldName
ti.SetSuggestions([]string{oldName})
ti.KeyMap.AcceptSuggestion = key.NewBinding(key.WithKeys("tab"), key.WithHelp("tab", "accept suggestion"))
ti.Focus()
ti.Prompt = "Name for new model: "
ti.PromptStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FF00FF"))
ti.TextStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#AD00FF"))
ti.Cursor.Style = lipgloss.NewStyle().Background(lipgloss.Color("#AE00FF"))
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)",
)
}