Improve table output from codespace list command (#4516)

Co-authored-by: Jose Garcia <josebalius@github.com>
Co-authored-by: Mislav Marohnić <mislav@github.com>
This commit is contained in:
Mateusz Urbanek 2021-10-21 17:42:08 +01:00 committed by GitHub
parent bbea5ac95e
commit 67595110e8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 14 deletions

View file

@ -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 {

View file

@ -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()
}