Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor greeting UI #85

Merged
merged 3 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cmd/greeting.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ func NewGreetingCommand() *cobra.Command {
Short: "config setting for Chatify",
Long: "config setting for Chatify",
RunE: func(cmd *cobra.Command, args []string) error {
p := tea.NewProgram(greetingUI.NewModel(), tea.WithAltScreen())
model, err := greetingUI.NewModel()
if err != nil {
return err
}

p := tea.NewProgram(model, tea.WithAltScreen())

if _, err := p.Run(); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion ui/cmd/greeting/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import (
)

func (m *Model) Init() tea.Cmd {
return tea.Batch(m.loadConfig, textinput.Blink)
return textinput.Blink
}
21 changes: 20 additions & 1 deletion ui/cmd/greeting/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,37 @@ type Model struct {
err error
}

func NewModel() *Model {
func NewModel() (*Model, error) {
window := utils.NewWindow()

cfg, err := loadConfig()
if err != nil {
return nil, err
}

return &Model{
ctx: context.Background(),
window: window,
textInput: newTextInput(window.Width),
cfg: cfg,
phase: questionPhase,
questionIndex: 0,
qaList: qaListTemplate,
conversation: conversationTemplate,
}, nil
}

func loadConfig() (*config.Config, error) {
cfg, err := config.New()
if err != nil {
return nil, err
}

if err := cfg.Load(); err != nil {
return nil, err
}

return cfg, nil
}

func newTextInput(width int) textinput.Model {
Expand Down
17 changes: 0 additions & 17 deletions ui/cmd/greeting/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Quit
}
}
case loadConfigMsg:
m.cfg = msg.cfg
case questionCompMsg:
if msg.isDone {
m.phase = authPhase
Expand Down Expand Up @@ -67,21 +65,6 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, inputCmd
}

type loadConfigMsg struct{ cfg *config.Config }

func (m *Model) loadConfig() tea.Msg {
cfg, err := config.New()
if err != nil {
return errMsg{err: err}
}

if err := cfg.Load(); err != nil {
return errMsg{err: err}
}

return loadConfigMsg{cfg: cfg}
}

type questionCompMsg struct {
isDone bool
}
Expand Down