Skip to content

Commit

Permalink
abbreviate multiline strings in table
Browse files Browse the repository at this point in the history
  • Loading branch information
kajes committed Sep 13, 2024
1 parent 0c7a7ae commit e440946
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cmd/sites/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,15 @@ func NewSitesListCmd(parentOpts *SitesOptions) *cobra.Command {
p := util.NewPrinter(opts.Out, 4)
p.AddHeader("Site Name", "Short Name", "ID", "Tags", "Description", "Status")
for _, s := range sites {
p.AddLine(s.GetName(), s.GetShortName(), s.GetId(), s.GetTags(), s.GetDescription(), s.GetStatus())

p.AddLine(
util.StringAbbreviate(s.GetName()),
util.StringAbbreviate(s.GetShortName()),
util.StringAbbreviate(s.GetId()),
s.GetTags(),
util.StringAbbreviate(s.GetDescription()),
util.StringAbbreviate(s.GetStatus()),
)
}
p.Print()
}
Expand Down
29 changes: 29 additions & 0 deletions cmd/sites/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,35 @@ SomeSiteName 32bf476b-0ab9-4d9d-879e-321651586b6a []`,
--------- ---------- -- ---- ----------- ------
SomeSiteName []`,
},
{
desc: "list sites with multiline description",
cli: "list",
stubs: []httpmock.Stub{
{
URL: "/admin/sites/status",
Responder: func(w http.ResponseWriter, r *http.Request) {
res := openapi.SiteWithStatusList{
Data: []openapi.SiteWithStatus{
{
Name: "SomeSiteName",
Description: openapi.PtrString("This is a comment\nspanning multiple lines"),
},
},
}
b, err := json.Marshal(res)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json")
w.Write(b)
},
},
},
want: `Site Name Short Name ID Tags Description Status
--------- ---------- -- ---- ----------- ------
SomeSiteName [] This is a comment [...]`,
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,11 @@ func AddSocketLogHook(path string) error {
log.AddHook(hook)
return nil
}

func StringAbbreviate(s string) string {
i := strings.Index(s, "\n")
if i < 0 {
return s
}
return s[:i] + " [...]"
}

0 comments on commit e440946

Please sign in to comment.