From 11f15470ed8853892929e2dd0c0f6d7b6d3acc3f Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Sat, 23 Nov 2024 16:16:30 -0500 Subject: [PATCH 1/2] Minimise quit state checking Currently, everytime the view is rendered, the app checks for the quitting boolean which seems unnecessary. This approach simply returns a unique view for quitting which removes the need for a `quitting` boolean variable an the `if quitting {...}` check currently run on every `View()`. --- examples/help/main.go | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/examples/help/main.go b/examples/help/main.go index 67e5a4f480..750a48e435 100644 --- a/examples/help/main.go +++ b/examples/help/main.go @@ -69,7 +69,6 @@ type model struct { help help.Model inputStyle lipgloss.Style lastKey string - quitting bool } func newModel() model { @@ -104,8 +103,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case key.Matches(msg, m.keys.Help): m.help.ShowAll = !m.help.ShowAll case key.Matches(msg, m.keys.Quit): - m.quitting = true - return m, tea.Quit + return quitModel{}, tea.Quit } } @@ -113,10 +111,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } func (m model) View() string { - if m.quitting { - return "Bye!\n" - } - var status string if m.lastKey == "" { status = "Waiting for input..." @@ -130,6 +124,14 @@ func (m model) View() string { return "\n" + status + strings.Repeat("\n", height) + helpView } +type quitModel struct{} + +func (m quitModel) Init() tea.Cmd { return nil } + +func (m quitModel) View() string { return "Bye!\n" } + +func (m quitModel) Update(tea.Msg) (tea.Model, tea.Cmd) { return m, nil } + func main() { if os.Getenv("HELP_DEBUG") != "" { f, err := tea.LogToFile("debug.log", "help") From 1c3bd8fd619c8d30adbfde996d983d0b4952bcda Mon Sep 17 00:00:00 2001 From: Benjamin Chausse Date: Sat, 23 Nov 2024 16:19:57 -0500 Subject: [PATCH 2/2] Less whitespaces --- examples/help/main.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/help/main.go b/examples/help/main.go index 750a48e435..b69d261500 100644 --- a/examples/help/main.go +++ b/examples/help/main.go @@ -126,10 +126,8 @@ func (m model) View() string { type quitModel struct{} -func (m quitModel) Init() tea.Cmd { return nil } - -func (m quitModel) View() string { return "Bye!\n" } - +func (m quitModel) Init() tea.Cmd { return nil } +func (m quitModel) View() string { return "Bye!\n" } func (m quitModel) Update(tea.Msg) (tea.Model, tea.Cmd) { return m, nil } func main() {