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

fix: track progress if the terminal width allows it #768

Merged
merged 1 commit into from
Jun 5, 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
8 changes: 4 additions & 4 deletions internal/state/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func TestWaitForActionsSuccess(t *testing.T) {

assert.Equal(t,
strings.Join([]string{
"Waiting for attach_volume to complete (server: 46830545, volume: 46830546) ...\n",
"Waiting for attach_volume to complete (server: 46830545, volume: 46830546) ... done\n",
"Waiting for attach_volume (server: 46830545, volume: 46830546) ...\n",
"Waiting for attach_volume (server: 46830545, volume: 46830546) ... done\n",
}, ""),
stderr,
)
Expand Down Expand Up @@ -92,8 +92,8 @@ func TestWaitForActionsError(t *testing.T) {

assert.Equal(t,
strings.Join([]string{
"Waiting for attach_volume to complete (server: 46830545, volume: 46830546) ...\n",
"Waiting for attach_volume to complete (server: 46830545, volume: 46830546) ... failed\n",
"Waiting for attach_volume (server: 46830545, volume: 46830546) ...\n",
"Waiting for attach_volume (server: 46830545, volume: 46830546) ... failed\n",
}, ""),
stderr,
)
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func ActionMessage(action *hcloud.Action) string {
return fmt.Sprintf("Waiting for %s to complete", color.New(color.Bold).Sprint(action.Command))
return fmt.Sprintf("Waiting for %s", color.New(color.Bold).Sprint(action.Command))
}

// FakeActionMessage returns the initial value with a unused color to grow the string
Expand Down
14 changes: 7 additions & 7 deletions internal/ui/actions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestMessages(t *testing.T) {
{ID: 46830545, Type: hcloud.ActionResourceTypeServer},
},
},
wantAction: "Waiting for create_server to complete",
wantAction: "Waiting for create_server",
wantResources: "(server: 46830545)",
},
{
Expand All @@ -42,7 +42,7 @@ func TestMessages(t *testing.T) {
{ID: 46830546, Type: hcloud.ActionResourceTypeVolume},
},
},
wantAction: "Waiting for attach_volume to complete",
wantAction: "Waiting for attach_volume",
wantResources: "(server: 46830545, volume: 46830546)",
},
{
Expand All @@ -53,7 +53,7 @@ func TestMessages(t *testing.T) {
Status: hcloud.ActionStatusRunning,
Progress: 0,
},
wantAction: "Waiting for create_server to complete",
wantAction: "Waiting for create_server",
wantResources: "",
},
}
Expand All @@ -73,9 +73,9 @@ func TestFakeActionMessages(t *testing.T) {
fakeMessage := FakeActionMessage("Some random message")

// The padding is important
actionMessage = fmt.Sprintf("%-60s", actionMessage)
fakeMessage = fmt.Sprintf("%-60s", fakeMessage)
actionMessage = fmt.Sprintf("%-40s", actionMessage)
fakeMessage = fmt.Sprintf("%-40s", fakeMessage)

assert.Equal(t, actionMessage, "Waiting for create_server to complete ")
assert.Equal(t, fakeMessage, "Some random message ")
assert.Equal(t, "Waiting for create_server ", actionMessage)
assert.Equal(t, "Some random message ", fakeMessage)
}
10 changes: 10 additions & 0 deletions internal/ui/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,13 @@ import (
func StdoutIsTerminal() bool {
return term.IsTerminal(int(os.Stdout.Fd()))
}

// TerminalWidth returns the width of the terminal.
func TerminalWidth() int {
width, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
return 0
}

return width
}
4 changes: 2 additions & 2 deletions internal/ui/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type ProgressGroup interface {
}

func NewProgressGroup(output io.Writer) ProgressGroup {
if StdoutIsTerminal() {
if StdoutIsTerminal() && TerminalWidth() > 80 {
return newTerminalProgressGroup(output)
} else {
return newScriptProgressGroup(output)
Expand All @@ -26,7 +26,7 @@ type Progress interface {
}

func NewProgress(output io.Writer, message string, resources string) Progress {
if StdoutIsTerminal() {
if StdoutIsTerminal() && TerminalWidth() > 80 {
return newTerminalProgress(output, message, resources)
} else {
return newScriptProgress(output, message, resources)
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/progress_terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func newTerminalProgress(output io.Writer, message string, resources string) *te
p := &terminalProgress{pb.New(100)}
p.el.SetWriter(output)
p.el.SetTemplateString(termProgressRunning)
p.el.Set("message", fmt.Sprintf("%-60s", message))
p.el.Set("message", fmt.Sprintf("%-40s", message))
p.el.Set("resources", resources)
return p
}
Expand Down
Loading