Skip to content

Commit b5a790c

Browse files
authored
Skip TUI for commands that don't use it (#267)
`PersistentPostRunE` was starting `tea.NewProgram` with `WithAltScreen` even when the stack was empty, causing a visible flash for non-interactive commands like object get/put/list.
1 parent a775d0d commit b5a790c

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var rootCmd = &cobra.Command{
5252
output := command.GetFormatFromContext(ctx)
5353
if output.Interactive() {
5454
stack := tui.GetStackFromContext(ctx)
55-
if stack == nil {
55+
if stack.IsEmpty() {
5656
return nil
5757
}
5858

pkg/tui/stack.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ func NewStack() *StackModel {
8080
return &StackModel{}
8181
}
8282

83+
func (m *StackModel) IsEmpty() bool {
84+
return m == nil || len(m.stack) == 0
85+
}
86+
8387
func (m *StackModel) WithDone(f func(tea.Msg) (tea.Model, tea.Cmd)) {
8488
m.done = f
8589
}

pkg/tui/stack_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,28 @@ import (
1515
"github.com/stretchr/testify/require"
1616
)
1717

18+
func TestStackIsEmpty(t *testing.T) {
19+
t.Run("returns true for new stack", func(t *testing.T) {
20+
stack := tui.NewStack()
21+
require.True(t, stack.IsEmpty())
22+
})
23+
24+
t.Run("returns false after push", func(t *testing.T) {
25+
stack := tui.NewStack()
26+
fooModel := &testhelper.SimpleModel{Str: "foo"}
27+
stack.Push(tui.ModelWithCmd{Model: fooModel})
28+
require.False(t, stack.IsEmpty())
29+
})
30+
31+
t.Run("returns true after popping all items", func(t *testing.T) {
32+
stack := tui.NewStack()
33+
fooModel := &testhelper.SimpleModel{Str: "foo"}
34+
stack.Push(tui.ModelWithCmd{Model: fooModel})
35+
stack.Pop()
36+
require.True(t, stack.IsEmpty())
37+
})
38+
}
39+
1840
func TestStack(t *testing.T) {
1941
t.Run("Can append and go back", func(t *testing.T) {
2042
stack := tui.NewStack()

0 commit comments

Comments
 (0)