X Tutup
Skip to content

Commit 6759511

Browse files
shanduurjosebaliusmislav
authored
Improve table output from codespace list command (cli#4516)
Co-authored-by: Jose Garcia <josebalius@github.com> Co-authored-by: Mislav Marohnić <mislav@github.com>
1 parent bbea5ac commit 6759511

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

internal/codespaces/api/api.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ type CodespaceGitStatus struct {
162162
const (
163163
// CodespaceStateAvailable is the state for a running codespace environment.
164164
CodespaceStateAvailable = "Available"
165+
// CodespaceStateShutdown is the state for a shutdown codespace environment.
166+
CodespaceStateShutdown = "Shutdown"
167+
// CodespaceStateStarting is the state for a starting codespace environment.
168+
CodespaceStateStarting = "Starting"
165169
)
166170

167171
type CodespaceConnection struct {

pkg/cmd/codespace/list.go

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package codespace
33
import (
44
"context"
55
"fmt"
6+
"time"
67

7-
"github.com/cli/cli/v2/pkg/cmd/codespace/output"
8+
"github.com/cli/cli/v2/internal/codespaces/api"
89
"github.com/cli/cli/v2/pkg/cmdutil"
10+
"github.com/cli/cli/v2/utils"
911
"github.com/spf13/cobra"
1012
)
1113

@@ -40,19 +42,49 @@ func (a *App) List(ctx context.Context, asJSON bool, limit int) error {
4042
return fmt.Errorf("error getting codespaces: %w", err)
4143
}
4244

43-
table := output.NewTable(a.io.Out, asJSON)
44-
table.SetHeader([]string{"Name", "Repository", "Branch", "State", "Created At"})
45+
if err := a.io.StartPager(); err != nil {
46+
a.errLogger.Printf("error starting pager: %v", err)
47+
}
48+
defer a.io.StopPager()
49+
50+
tp := utils.NewTablePrinter(a.io)
51+
if tp.IsTTY() {
52+
tp.AddField("NAME", nil, nil)
53+
tp.AddField("REPOSITORY", nil, nil)
54+
tp.AddField("BRANCH", nil, nil)
55+
tp.AddField("STATE", nil, nil)
56+
tp.AddField("CREATED AT", nil, nil)
57+
tp.EndRow()
58+
}
59+
60+
cs := a.io.ColorScheme()
4561
for _, apiCodespace := range codespaces {
46-
cs := codespace{apiCodespace}
47-
table.Append([]string{
48-
cs.Name,
49-
cs.Repository.FullName,
50-
cs.branchWithGitStatus(),
51-
cs.State,
52-
cs.CreatedAt,
53-
})
62+
c := codespace{apiCodespace}
63+
64+
var stateColor func(string) string
65+
switch c.State {
66+
case api.CodespaceStateStarting:
67+
stateColor = cs.Yellow
68+
case api.CodespaceStateAvailable:
69+
stateColor = cs.Green
70+
}
71+
72+
tp.AddField(c.Name, nil, cs.Yellow)
73+
tp.AddField(c.Repository.FullName, nil, nil)
74+
tp.AddField(c.branchWithGitStatus(), nil, cs.Cyan)
75+
tp.AddField(c.State, nil, stateColor)
76+
77+
if tp.IsTTY() {
78+
ct, err := time.Parse(time.RFC3339, c.CreatedAt)
79+
if err != nil {
80+
return fmt.Errorf("error parsing date %q: %w", c.CreatedAt, err)
81+
}
82+
tp.AddField(utils.FuzzyAgoAbbr(time.Now(), ct), nil, cs.Gray)
83+
} else {
84+
tp.AddField(c.CreatedAt, nil, nil)
85+
}
86+
tp.EndRow()
5487
}
5588

56-
table.Render()
57-
return nil
89+
return tp.Render()
5890
}

0 commit comments

Comments
 (0)
X Tutup