does Update() get called even when there are no keys being pressed?
aravindanvenkataraju opened this issue · 0 comments
Hi,
I have just started using this library (pretty new to golang, as well), and I am following the youtube video tutorial where text input is used to input answers for a series of questions. What I am noticing is that Update function is getting called even though no keys are being pressed.
I have added log.Println("update is called. type: ", msg)
as the default case in the switch stmt of Update() function and I can see this log getting printed atleast twice per second with information. What is the struct that is getting printed here?
sample log below.
DEBUG 2024/04/01 22:33:09 update is called. type: {}
DEBUG 2024/04/01 22:33:09 update is called. type: {}
DEBUG 2024/04/01 22:33:09 update is called. type: {0 40}
DEBUG 2024/04/01 22:33:09 question: What is your favourite editor?, answer: nvim
DEBUG 2024/04/01 22:33:10 update is called. type: {0 41}
DEBUG 2024/04/01 22:33:10 update is called. type: {}
DEBUG 2024/04/01 22:33:10 update is called. type: {}
DEBUG 2024/04/01 22:33:11 update is called. type: {}
DEBUG 2024/04/01 22:33:11 update is called. type: {}
I believe this is not an issue, but a gap in my understanding. Could you please help me understand why this is happening? Any further documentations that I should go through?
Below is the code:
package main
import (
"log"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type Styles struct {
BorderColor lipgloss.Color
InputField lipgloss.Style
}
type model struct {
index int
questions []Question
answerField textinput.Model
width int
height int
styles *Styles
}
type Question struct {
question string
answer string
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
current := &m.questions[m.index]
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc":
return m, tea.Quit
case "enter":
current.answer = m.answerField.Value()
m.answerField.SetValue("")
m.Next()
log.Printf("question: %s, answer: %s\n", current.question, current.answer)
return m, nil
}
default:
log.Println("update is called. type: ", msg)
}
m.answerField, cmd = m.answerField.Update(msg)
return m, cmd
}
func (m model) View() string {
if m.width == 0 {
return "Loading...."
}
return lipgloss.Place(
m.width,
m.height,
lipgloss.Center,
lipgloss.Center,
lipgloss.JoinVertical(
lipgloss.Center,
m.questions[m.index].question,
m.styles.InputField.Render(m.answerField.View())),
)
}
func New() *model {
questions := []Question{
NewQuestion("What is your name?"),
NewQuestion("What is your favourite editor?"),
NewQuestion("What is your favourit quote?"),
}
answerField := textinput.New()
answerField.Placeholder = "Your answer here..."
answerField.Focus()
return &model{
questions: questions,
answerField: answerField,
styles: DefaultStyles(),
}
}
func (m *model) Next() {
if m.index < len(m.questions)-1 {
m.index++
} else {
m.index = 0
}
}
func NewQuestion(question string) Question {
return Question{question: question}
}
func DefaultStyles() *Styles {
s := new(Styles)
s.BorderColor = lipgloss.Color("36")
s.InputField = lipgloss.NewStyle().BorderForeground(s.BorderColor).BorderStyle(lipgloss.NormalBorder()).Padding(1).Width(80)
return s
}
func main() {
f, err := tea.LogToFile("debug.log", "DEBUG")
if err != nil {
log.Fatal(err)
}
defer f.Close()
m := New()
p := tea.NewProgram(m, tea.WithAltScreen())
if _, err := p.Run(); err != nil {
log.Fatal(err)
}
}