diff --git a/internal/codespaces/api/api.go b/internal/codespaces/api/api.go index bf3f204ee..505e292ae 100644 --- a/internal/codespaces/api/api.go +++ b/internal/codespaces/api/api.go @@ -162,6 +162,10 @@ type CodespaceGitStatus struct { const ( // CodespaceStateAvailable is the state for a running codespace environment. CodespaceStateAvailable = "Available" + // CodespaceStateShutdown is the state for a shutdown codespace environment. + CodespaceStateShutdown = "Shutdown" + // CodespaceStateStarting is the state for a starting codespace environment. + CodespaceStateStarting = "Starting" ) type CodespaceConnection struct { diff --git a/pkg/cmd/codespace/list.go b/pkg/cmd/codespace/list.go index dab3f87d5..0e5bf9242 100644 --- a/pkg/cmd/codespace/list.go +++ b/pkg/cmd/codespace/list.go @@ -3,9 +3,11 @@ package codespace import ( "context" "fmt" + "time" - "github.com/cli/cli/v2/pkg/cmd/codespace/output" + "github.com/cli/cli/v2/internal/codespaces/api" "github.com/cli/cli/v2/pkg/cmdutil" + "github.com/cli/cli/v2/utils" "github.com/spf13/cobra" ) @@ -40,19 +42,49 @@ func (a *App) List(ctx context.Context, asJSON bool, limit int) error { return fmt.Errorf("error getting codespaces: %w", err) } - table := output.NewTable(a.io.Out, asJSON) - table.SetHeader([]string{"Name", "Repository", "Branch", "State", "Created At"}) - for _, apiCodespace := range codespaces { - cs := codespace{apiCodespace} - table.Append([]string{ - cs.Name, - cs.Repository.FullName, - cs.branchWithGitStatus(), - cs.State, - cs.CreatedAt, - }) + if err := a.io.StartPager(); err != nil { + a.errLogger.Printf("error starting pager: %v", err) + } + defer a.io.StopPager() + + tp := utils.NewTablePrinter(a.io) + if tp.IsTTY() { + tp.AddField("NAME", nil, nil) + tp.AddField("REPOSITORY", nil, nil) + tp.AddField("BRANCH", nil, nil) + tp.AddField("STATE", nil, nil) + tp.AddField("CREATED AT", nil, nil) + tp.EndRow() } - table.Render() - return nil + cs := a.io.ColorScheme() + for _, apiCodespace := range codespaces { + c := codespace{apiCodespace} + + var stateColor func(string) string + switch c.State { + case api.CodespaceStateStarting: + stateColor = cs.Yellow + case api.CodespaceStateAvailable: + stateColor = cs.Green + } + + tp.AddField(c.Name, nil, cs.Yellow) + tp.AddField(c.Repository.FullName, nil, nil) + tp.AddField(c.branchWithGitStatus(), nil, cs.Cyan) + tp.AddField(c.State, nil, stateColor) + + if tp.IsTTY() { + ct, err := time.Parse(time.RFC3339, c.CreatedAt) + if err != nil { + return fmt.Errorf("error parsing date %q: %w", c.CreatedAt, err) + } + tp.AddField(utils.FuzzyAgoAbbr(time.Now(), ct), nil, cs.Gray) + } else { + tp.AddField(c.CreatedAt, nil, nil) + } + tp.EndRow() + } + + return tp.Render() }