feat: move init to tea.msg

This commit is contained in:
Anish Lakhwara 2024-05-25 03:20:21 -07:00
parent 98482784bc
commit 30f6f2b7b4

11
main.go
View file

@ -63,7 +63,7 @@ func loadTasks() tea.Msg {
Foreground(lipgloss.Color("0")). Foreground(lipgloss.Color("0")).
Background(lipgloss.Color("120")) Background(lipgloss.Color("120"))
t.SetStyles(s) t.SetStyles(s)
return t return LoadedTasksMsg{t}
} }
type editorFinishedMsg struct{ err error } type editorFinishedMsg struct{ err error }
@ -104,9 +104,9 @@ func (m model) openEditor() tea.Cmd {
}) })
} }
// TODO func (m model) Init() tea.Cmd {
// This is where the data should be loaded... return loadTasks
func (m model) Init() tea.Cmd { return nil } }
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd var cmd tea.Cmd
@ -122,6 +122,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
//top, right, bottom, left := baseStyle.GetMargin() //top, right, bottom, left := baseStyle.GetMargin()
m.taskList.SetWidth(msg.Width - 5) m.taskList.SetWidth(msg.Width - 5)
m.taskList.SetHeight(msg.Height - 5) m.taskList.SetHeight(msg.Height - 5)
case LoadedTasksMsg:
m.taskList = msg.TaskList
} }
m.taskList, cmd = m.taskList.Update(msg) m.taskList, cmd = m.taskList.Update(msg)
return m, cmd return m, cmd
@ -133,7 +135,6 @@ func (m model) View() string {
func main() { func main() {
var m model var m model
m.taskList = CreateTable()
if _, err := tea.NewProgram(m).Run(); err != nil { if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err) fmt.Println("Error running program:", err)
os.Exit(1) os.Exit(1)