Skip to content

Commit

Permalink
add ui update (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Aug 5, 2023
1 parent 088f473 commit 2e77907
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions ui/update.go
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)
}

0 comments on commit 2e77907

Please sign in to comment.