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

chore: improve spinner when used with --no-progress #105

Merged
merged 2 commits into from
May 29, 2024
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
6 changes: 4 additions & 2 deletions src/message/spinner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bufio"
"bytes"
"fmt"
"os"
"strings"

"github.com/defenseunicorns/pkg/helpers/v2"
Expand Down Expand Up @@ -56,7 +57,7 @@ var NewProgressSpinner = func(format string, a ...any) helpers.ProgressWriter {
func (p *Spinner) Write(raw []byte) (int, error) {
size := len(raw)
if NoProgress {
pterm.Printfln(" %s", string(raw))
os.Stderr.Write(raw)

return size, nil
}
Expand All @@ -66,7 +67,8 @@ func (p *Spinner) Write(raw []byte) (int, error) {
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
text := pterm.Sprintf(" %s", scanner.Text())
pterm.Fprinto(p.spinner.Writer, strings.Repeat(" ", pterm.GetTerminalWidth()))
// Clear the current line with the ANSI escape code
pterm.Fprinto(p.spinner.Writer, "\033[K")
pterm.Fprintln(p.spinner.Writer, text)
}

Expand Down
8 changes: 8 additions & 0 deletions src/test/e2e/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ func TestTaskRunner(t *testing.T) {
require.Contains(t, stdErr, "task loop detected")
})

t.Run("run interactive (with --no-progress)", func(t *testing.T) {
t.Parallel()
stdOut, stdErr, err := e2e.Maru("run", "interactive", "--file", "src/test/tasks/tasks.yaml", "--no-progress")
require.NoError(t, err, stdOut, stdErr)
// Ensure there are no extra chars that will interrupt interactive programs (i.e. a spinner) when --no-progress is set
require.Contains(t, stdErr, "\033[G ⬒ Spinning...\033[G ⬔ Spinning...\033[G ◨ Spinning...")
})

t.Run("test includes paths", func(t *testing.T) {
t.Parallel()
stdOut, stdErr, err := e2e.Maru("run", "foobar", "--file", "src/test/tasks/tasks.yaml")
Expand Down
20 changes: 20 additions & 0 deletions src/test/tasks/tasks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ tasks:
- name: action
actions:
- cmd: echo "specific test string"
- name: interactive
description: Run an interactive task
actions:
- description: Create a spinner that spins
cmd: |
printf '\033[G ⬒ Spinning...'
sleep 0.1
printf '\033[G ⬔ Spinning...'
sleep 0.1
printf '\033[G ◨ Spinning...'
sleep 0.1
printf '\033[G ◪ Spinning...'
sleep 0.1
printf '\033[G ⬓ Spinning...'
sleep 0.1
printf '\033[G ⬕ Spinning...'
sleep 0.1
printf '\033[G ◧ Spinning...'
sleep 0.1
printf '\033[G ◩ Spinning...'
- name: cmd-set-variable
actions:
- cmd: echo unique-value
Expand Down
Loading