Skip to content

Commit 069fffe

Browse files
authored
fix: track progress if the terminal width allows it (#768)
- Reduce the width of the actions messages (remove 'to complete') - Only print progress when the terminal width is bigger than 80. - Reduce the padding when printing the actions messages Fixes #767
1 parent 102b963 commit 069fffe

File tree

6 files changed

+25
-15
lines changed

6 files changed

+25
-15
lines changed

internal/state/actions_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ func TestWaitForActionsSuccess(t *testing.T) {
4949

5050
assert.Equal(t,
5151
strings.Join([]string{
52-
"Waiting for attach_volume to complete (server: 46830545, volume: 46830546) ...\n",
53-
"Waiting for attach_volume to complete (server: 46830545, volume: 46830546) ... done\n",
52+
"Waiting for attach_volume (server: 46830545, volume: 46830546) ...\n",
53+
"Waiting for attach_volume (server: 46830545, volume: 46830546) ... done\n",
5454
}, ""),
5555
stderr,
5656
)
@@ -92,8 +92,8 @@ func TestWaitForActionsError(t *testing.T) {
9292

9393
assert.Equal(t,
9494
strings.Join([]string{
95-
"Waiting for attach_volume to complete (server: 46830545, volume: 46830546) ...\n",
96-
"Waiting for attach_volume to complete (server: 46830545, volume: 46830546) ... failed\n",
95+
"Waiting for attach_volume (server: 46830545, volume: 46830546) ...\n",
96+
"Waiting for attach_volume (server: 46830545, volume: 46830546) ... failed\n",
9797
}, ""),
9898
stderr,
9999
)

internal/ui/actions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

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

1616
// FakeActionMessage returns the initial value with a unused color to grow the string

internal/ui/actions_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func TestMessages(t *testing.T) {
2727
{ID: 46830545, Type: hcloud.ActionResourceTypeServer},
2828
},
2929
},
30-
wantAction: "Waiting for create_server to complete",
30+
wantAction: "Waiting for create_server",
3131
wantResources: "(server: 46830545)",
3232
},
3333
{
@@ -42,7 +42,7 @@ func TestMessages(t *testing.T) {
4242
{ID: 46830546, Type: hcloud.ActionResourceTypeVolume},
4343
},
4444
},
45-
wantAction: "Waiting for attach_volume to complete",
45+
wantAction: "Waiting for attach_volume",
4646
wantResources: "(server: 46830545, volume: 46830546)",
4747
},
4848
{
@@ -53,7 +53,7 @@ func TestMessages(t *testing.T) {
5353
Status: hcloud.ActionStatusRunning,
5454
Progress: 0,
5555
},
56-
wantAction: "Waiting for create_server to complete",
56+
wantAction: "Waiting for create_server",
5757
wantResources: "",
5858
},
5959
}
@@ -73,9 +73,9 @@ func TestFakeActionMessages(t *testing.T) {
7373
fakeMessage := FakeActionMessage("Some random message")
7474

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

79-
assert.Equal(t, actionMessage, "Waiting for create_server to complete ")
80-
assert.Equal(t, fakeMessage, "Some random message ")
79+
assert.Equal(t, "Waiting for create_server ", actionMessage)
80+
assert.Equal(t, "Some random message ", fakeMessage)
8181
}

internal/ui/helpers.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,13 @@ import (
1010
func StdoutIsTerminal() bool {
1111
return term.IsTerminal(int(os.Stdout.Fd()))
1212
}
13+
14+
// TerminalWidth returns the width of the terminal.
15+
func TerminalWidth() int {
16+
width, _, err := term.GetSize(int(os.Stdout.Fd()))
17+
if err != nil {
18+
return 0
19+
}
20+
21+
return width
22+
}

internal/ui/progress.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type ProgressGroup interface {
1111
}
1212

1313
func NewProgressGroup(output io.Writer) ProgressGroup {
14-
if StdoutIsTerminal() {
14+
if StdoutIsTerminal() && TerminalWidth() > 80 {
1515
return newTerminalProgressGroup(output)
1616
} else {
1717
return newScriptProgressGroup(output)
@@ -26,7 +26,7 @@ type Progress interface {
2626
}
2727

2828
func NewProgress(output io.Writer, message string, resources string) Progress {
29-
if StdoutIsTerminal() {
29+
if StdoutIsTerminal() && TerminalWidth() > 80 {
3030
return newTerminalProgress(output, message, resources)
3131
} else {
3232
return newScriptProgress(output, message, resources)

internal/ui/progress_terminal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func newTerminalProgress(output io.Writer, message string, resources string) *te
4646
p := &terminalProgress{pb.New(100)}
4747
p.el.SetWriter(output)
4848
p.el.SetTemplateString(termProgressRunning)
49-
p.el.Set("message", fmt.Sprintf("%-60s", message))
49+
p.el.Set("message", fmt.Sprintf("%-40s", message))
5050
p.el.Set("resources", resources)
5151
return p
5252
}

0 commit comments

Comments
 (0)