-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
add bubble tea
- Loading branch information
Showing
7 changed files
with
227 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package ui | ||
|
||
import ( | ||
"github.com/charmbracelet/bubbles/textarea" | ||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
func (m *Model) Init() tea.Cmd { | ||
return textarea.Blink | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package ui | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/charmbracelet/bubbles/textarea" | ||
"github.com/charmbracelet/bubbles/viewport" | ||
"github.com/charmbracelet/lipgloss" | ||
) | ||
|
||
const ( | ||
TextAreaWidth = 200 | ||
TextAreaHeight = 3 | ||
TextAreaCharLimit = 200 | ||
ViewportWidth = 100 | ||
ViewportHeight = 8 | ||
) | ||
|
||
var ( | ||
senderStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("5")) | ||
qaListTemplate = []*QA{ | ||
{ | ||
question: `May I ask for your "Spotify ID"?`, | ||
answer: "", | ||
placeholder: `Please enter your "Spotify ID".`, | ||
}, | ||
{ | ||
question: `Next, may I ask for your "Spotify Secret"?`, | ||
answer: "", | ||
placeholder: `Please enter your "Spotify Secret".`, | ||
}, | ||
{ | ||
question: `Finally, may I ask for your "OpenAI API key"?`, | ||
answer: "", | ||
placeholder: `Please enter your "OpenAI API key".`, | ||
}, | ||
} | ||
) | ||
|
||
type Model struct { | ||
index int | ||
textarea textarea.Model | ||
displayMessages []string | ||
qaList []*QA | ||
viewport viewport.Model | ||
senderStyle lipgloss.Style | ||
err error | ||
} | ||
|
||
type QA struct { | ||
question string | ||
answer string | ||
placeholder string | ||
} | ||
|
||
func NewModel() *Model { | ||
greetings := []string{ | ||
senderStyle.Render("Chatify: ") + "Hi there, I'm Chatify!", | ||
" I want to know three things.", | ||
" " + qaListTemplate[0].question, | ||
} | ||
|
||
return &Model{ | ||
index: 0, | ||
displayMessages: greetings, | ||
qaList: qaListTemplate, | ||
textarea: newTextArea(), | ||
viewport: newViewport(greetings), | ||
senderStyle: senderStyle, | ||
err: nil, | ||
} | ||
} | ||
|
||
func newTextArea() textarea.Model { | ||
ta := textarea.New() | ||
ta.Placeholder = qaListTemplate[0].placeholder | ||
ta.Focus() | ||
ta.CharLimit = TextAreaCharLimit | ||
ta.SetHeight(TextAreaHeight) | ||
ta.SetWidth(TextAreaWidth) | ||
ta.FocusedStyle.CursorLine = lipgloss.NewStyle() | ||
ta.ShowLineNumbers = false | ||
ta.KeyMap.InsertNewline.SetEnabled(false) | ||
|
||
return ta | ||
} | ||
|
||
func newViewport(greetings []string) viewport.Model { | ||
vp := viewport.New(ViewportWidth, ViewportHeight) | ||
vp.SetContent(strings.Join(greetings, "\n")) | ||
|
||
return vp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package ui | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
) | ||
|
||
func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | ||
var ( | ||
tiCmd tea.Cmd | ||
vpCmd tea.Cmd | ||
) | ||
|
||
m.textarea, tiCmd = m.textarea.Update(msg) | ||
m.viewport, vpCmd = m.viewport.Update(msg) | ||
|
||
switch msg := msg.(type) { | ||
case tea.KeyMsg: | ||
switch msg.Type { | ||
case tea.KeyCtrlC, tea.KeyEsc: | ||
fmt.Println(m.textarea.Value()) | ||
return m, tea.Quit | ||
case tea.KeyEnter: | ||
m.qaList[m.index].answer = m.textarea.Value() | ||
|
||
m.index++ | ||
if m.index == len(m.qaList) { | ||
m.displayMessages = append(m.displayMessages, m.senderStyle.Render("Chatify: ")+"Thank you so much!") | ||
m.viewport.SetContent(strings.Join(m.displayMessages, "\n")) | ||
|
||
return m, tea.Quit | ||
} | ||
|
||
m.displayMessages = append( | ||
m.displayMessages, | ||
m.senderStyle.Render("You: ")+m.textarea.Value(), | ||
m.senderStyle.Render("Chatify: ")+m.qaList[m.index].question, | ||
) | ||
m.viewport.SetContent(strings.Join(m.displayMessages, "\n")) | ||
m.viewport.GotoBottom() | ||
|
||
m.textarea.Reset() | ||
m.textarea.Placeholder = m.qaList[m.index].placeholder | ||
} | ||
case error: | ||
m.err = msg | ||
return m, nil | ||
} | ||
|
||
return m, tea.Batch(tiCmd, vpCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package ui | ||
|
||
import "fmt" | ||
|
||
func (m *Model) View() string { | ||
return fmt.Sprintf( | ||
"%s\n\n%s\n\n", | ||
m.viewport.View(), | ||
m.textarea.View(), | ||
) | ||
} |