GitLabgitlab-org

cli

main
ply6 git calls27 ms98 KiB5 cachedprotocol v2
369 lines9.4 KiBRaw
1package tableprinter
3import (
4 "fmt"
5 "strings"
7 "gitlab.com/gitlab-org/cli/internal/text"
8)
10var tp *TablePrinter
12func init() {
13 tp = &TablePrinter{
14 TotalRows: 0,
15 Wrap: false,
16 MaxColWidth: 0,
17 TTYSeparator: "\t",
18 NonTTYSeparator: "\t",
19 TerminalWidth: 80,
20 }
23// TablePrinter represents a decorator that renders the data formatted in a tabular form.
24type TablePrinter struct {
25 // Total number of records. Needed if AddRowFunc is used
26 TotalRows int
27 // Wrap when set to true wraps the contents of the columns when the length exceeds the MaxColWidth
28 Wrap bool
29 // MaxColWidth is the maximum allowed width for cells in the table
30 MaxColWidth int
31 // TTYSeparator is the separator for columns in the table on TTYs. Default is "\t"
32 TTYSeparator string
33 // NonTTYSeparator is the separator for columns in the table on non-TTYs. Default is "\t"
34 NonTTYSeparator string
35 // Rows is the collection of rows in the table
36 Rows []*TableRow
37 // TerminalWidth is the max width of the terminal
38 TerminalWidth int
39 // IsTTY indicates whether output is a TTY or non-TTY
40 IsTTY bool
41 // Pad, when true, pads cells to their column width independently of IsTTY, so
42 // non-TTY output can be aligned without being truncated. It forces a
43 // single-space column separator, overriding TTYSeparator/NonTTYSeparator.
44 Pad bool
47type TableCell struct {
48 // Value in the cell
49 Value any
50 // Width is the width of the cell
51 Width int
52 // Wrap when true wraps the contents of the cell when the length exceeds the width
53 Wrap bool
55 isaTTY bool
56 pad bool
57 // widthComputed records that Render assigned Width from colWidths.
58 // It lets a padded cell distinguish a column capped to zero width
59 // from a width that is simply not set yet: LineWidth measures cells
60 // before widths exist and must see the raw value.
61 widthComputed bool
64type TableRow struct {
65 Cells []*TableCell
66 // Separator is the separator for columns in the table. Default is " "
67 Separator string
70func NewTablePrinter() *TablePrinter {
71 t := &TablePrinter{
72 TTYSeparator: tp.TTYSeparator,
73 NonTTYSeparator: tp.NonTTYSeparator,
74 MaxColWidth: tp.MaxColWidth,
75 Wrap: false,
76 TerminalWidth: tp.TerminalWidth,
77 IsTTY: tp.IsTTY,
78 Pad: tp.Pad,
79 }
81 return t
84func (t *TablePrinter) Separator() string {
85 // when padding, use a fixed-width space so a tab doesn't overshoot the computed widths
86 if t.Pad {
87 return " "
88 }
90 if t.IsTTY {
91 return t.TTYSeparator
92 }
94 return t.NonTTYSeparator
97// SetTerminalWidth sets the maximum width for the terminal
98func SetTerminalWidth(width int) { tp.SetTerminalWidth(width) }
100func (t *TablePrinter) SetTerminalWidth(width int) {
101 t.TerminalWidth = width
104// SetIsTTY sets the IsTTY variable which indicates whether terminal
105// output is a TTY or nonTTY
106func SetIsTTY(isTTY bool) { tp.SetIsTTY(isTTY) }
108func (t *TablePrinter) SetIsTTY(isTTY bool) {
109 t.IsTTY = isTTY
112// SetPad sets the Pad variable which, when true, pads cells to their column
113// width independently of IsTTY
114func SetPad(pad bool) { tp.SetPad(pad) }
116func (t *TablePrinter) SetPad(pad bool) {
117 t.Pad = pad
120// SetTTYSeparator sets the separator for the columns in the table for TTYs
121func SetTTYSeparator(s string) { tp.SetTTYSeparator(s) }
123func (t *TablePrinter) SetTTYSeparator(s string) {
124 t.TTYSeparator = s
127func (t *TablePrinter) makeRow() {
128 if t.Rows == nil {
129 t.Rows = make([]*TableRow, 1)
130 t.Rows[0] = &TableRow{}
131 }
134func (t *TablePrinter) AddCell(s any) {
135 t.makeRow()
136 rowI := len(t.Rows) - 1
137 row := t.Rows[rowI]
139 cell := &TableCell{
140 Value: s,
141 isaTTY: t.IsTTY,
142 pad: t.Pad,
143 }
145 row.Separator = t.Separator()
147 row.Cells = append(row.Cells, cell)
150// AddCellf formats according to a format specifier and adds cell to row
151func (t *TablePrinter) AddCellf(s string, f ...any) {
152 t.AddCell(fmt.Sprintf(s, f...))
155func (t *TablePrinter) AddRow(str ...any) {
156 for _, s := range str {
157 t.AddCell(s)
158 }
159 t.EndRow()
162func (t *TablePrinter) AddRowFunc(f func(int, int) string) {
163 for ri := range t.TotalRows {
164 row := make([]any, t.TotalRows)
165 for ci := range row {
166 row[ci] = f(ri, ci)
167 }
168 t.AddRow(row)
169 t.EndRow()
170 }
173func (t *TablePrinter) EndRow() {
174 t.Rows = append(t.Rows, &TableRow{Cells: make([]*TableCell, 1)})
177// Bytes returns the []byte value of table
178func (t *TablePrinter) Bytes() []byte {
179 return []byte(t.String())
182// String returns the string value of table. Alternative to Render()
183func (t *TablePrinter) String() string {
184 return t.Render()
187// String returns the string representation of the row
188func (r *TableRow) String() string {
189 // get the max number of lines for each cell
190 var lc int // line count
191 for _, cell := range r.Cells {
192 if clc := len(strings.Split(cell.String(), "\n")); clc > lc {
193 lc = clc
194 }
195 }
197 // allocate a two-dimensional array of cells for each line and add size them
198 cells := make([][]*TableCell, lc)
199 for x := range lc {
200 cells[x] = make([]*TableCell, len(r.Cells))
201 for y := range r.Cells {
202 cells[x][y] = &TableCell{Width: r.Cells[y].Width}
203 }
204 }
206 // insert each line in a cell as new cell in the cells array
207 for y, cell := range r.Cells {
208 lines := strings.Split(cell.String(), "\n")
209 for x, line := range lines {
210 cells[x][y].Value = line
211 }
212 }
214 // format each line
215 lines := make([]string, lc)
216 for x := range lines {
217 line := make([]string, len(cells[x]))
218 for y := range cells[x] {
219 line[y] = cells[x][y].String()
220 }
221 lines[x] = text.Join(line, r.Separator)
222 }
223 return strings.Join(lines, "\n")
226// purgeRow removes nil cells and rows
227func (t *TablePrinter) purgeRow() {
228 newSlice := make([]*TableRow, 0, len(t.Rows))
229 for _, row := range t.Rows {
230 var newRow *TableRow
231 if len(row.Cells) > 0 && row.Cells != nil {
232 var newCells []*TableCell
233 for _, cell := range row.Cells {
234 if cell != nil {
235 newCells = append(newCells, cell)
236 }
237 }
238 newRow = &TableRow{Cells: newCells}
239 }
241 if newRow != nil {
242 newSlice = append(newSlice, newRow)
243 }
244 }
245 t.Rows = newSlice
248// Render builds and returns the string representation of the table
249func (t *TablePrinter) Render() string {
250 if len(t.Rows) == 0 {
251 return ""
252 }
253 // remove nil cells and rows
254 t.purgeRow()
256 colWidths := t.colWidths()
258 var lines []string
259 for _, row := range t.Rows {
260 row.Separator = t.Separator()
261 for i, cell := range row.Cells {
262 cell.Width = colWidths[i]
263 cell.widthComputed = true
264 cell.Wrap = t.Wrap
265 }
266 lines = append(lines, row.String())
267 }
268 return text.Join(lines, "\n")
271// LineWidth returns the max width of all the lines in a cell
272func (c *TableCell) LineWidth() int {
273 width := 0
274 for s := range strings.SplitSeq(c.String(), "\n") {
275 w := text.StringWidth(s)
276 if w > width {
277 width = w
278 }
279 }
280 return width
283// String returns the string formatted representation of the cell
284func (c *TableCell) String() string {
285 if c == nil {
286 return ""
287 }
288 if c.Value == nil {
289 return text.PadLeft(" ", c.Width, ' ')
290 }
292 // convert value to string
293 s := fmt.Sprintf("%v", c.Value)
294 // Truncate both pads short values and bounds long ones, so it aligns the cell
295 // to its width. Width is only terminal derived on a TTY (colWidths skips the
296 // terminal cap for a padded non-TTY), so a padded non-TTY stays lossless
297 // unless an explicit MaxColWidth applies. A padded cell is formatted even at
298 // a computed width of zero, a column squeezed out by a narrow terminal
299 // collapses rather than printing unformatted and breaking alignment.
300 if (c.Width > 0 || (c.pad && c.widthComputed)) && (c.isaTTY || c.pad) {
301 if c.Wrap && c.Width > 0 && len(s) > c.Width {
302 return text.WrapString(s, c.Width)
303 } else {
304 return text.Truncate(s, c.Width)
305 }
306 }
307 return s
310// colWidths determine the width for each column (cell in a row)
311func (t *TablePrinter) colWidths() []int {
312 var colWidths []int
313 for _, row := range t.Rows {
314 for i, cell := range row.Cells {
315 // resize colwidth array
316 if i+1 > len(colWidths) {
317 colWidths = append(colWidths, 0)
318 }
319 cellwidth := cell.LineWidth()
320 if t.MaxColWidth != 0 && cellwidth > t.MaxColWidth {
321 cellwidth = t.MaxColWidth
322 }
324 if cellwidth > colWidths[i] {
325 colWidths[i] = cellwidth
326 }
327 }
328 }
329 numCols := len(colWidths)
330 separatorWidth := (numCols - 1) * len(t.Separator())
331 totalWidth := separatorWidth
332 for _, width := range colWidths {
333 totalWidth += width
334 }
336 // a padded non-TTY has no terminal to fit into, so terminal width must never drive truncation there;
337 // an explicit MaxColWidth still applies.
338 if !(t.Pad && !t.IsTTY) && t.MaxColWidth == 0 && totalWidth > t.TerminalWidth {
339 availWidth := t.TerminalWidth - colWidths[0] - separatorWidth
340 // add extra space from columns that are already narrower than threshold
341 for col := 1; col < numCols; col++ {
342 availColWidth := availWidth / (numCols - 1)
343 if extra := availColWidth - colWidths[col]; extra > 0 {
344 availWidth += extra
345 }
346 }
347 // cap all but first column to fit available terminal width
348 for col := 1; col < numCols; col++ {
349 availColWidth := availWidth / (numCols - 1)
350 if colWidths[col] > availColWidth {
351 colWidths[col] = max(availColWidth, 0)
352 }
353 }
354 // even with the other columns bottomed out at zero, the first column
355 // alone can overflow the terminal; a padded row promises to fit, so the
356 // first column is shrunk to whatever width is left
357 if t.Pad {
358 remainingWidth := t.TerminalWidth - separatorWidth
359 for col := 1; col < numCols; col++ {
360 remainingWidth -= colWidths[col]
361 }
362 if colWidths[0] > remainingWidth {
363 colWidths[0] = max(remainingWidth, 0)
364 }
365 }
366 }
368 return colWidths
Diagnostics6 git calls · 27 ms
CallStatusSizeTime027 ms
discover200185 B3 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=185
git.server.agent=git/2.56.g1891de4-Linux
git.server.object_format=sha1
git.server.caps=agent ls-refs fetch server-option object-format
git.server.fetch=shallow wait-for-done filter
git.protocol.version=2
git.pkt.frames=9
git.pkt.data=7
git.pkt.flush=2
git.pkt.delim=0
git.pkt.bytes=185
git.pkt.payload.max=35
ls-refs20058 KiB · hit2 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=59849
git.response.cache=HIT
git.response.duration_ms=2
git.protocol.version=2
git.pkt.frames=731
git.pkt.data=730
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=59849
git.pkt.payload.max=143
git.ls_refs.count=730
git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=32926
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=tree:0
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=10
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=22
git.pkt.data=20
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=32926
git.pkt.payload.max=32046
git.sideband.band1.bytes=32046
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=80
git.pack.bytes=32046
git.pack.ofs_delta=36
git.pack.ref_delta=0
git.pack.chain.max=2
git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91
git.pack.full=44
Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=4
git.response.bytes=4273
git.response.cache=HIT
git.response.duration_ms=2
git.fetch.filter=combine:blob:none+tree:2
git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9
git.fetch.haves.count=0
git.fetch.deepen=1
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=7
git.pkt.data=5
git.pkt.flush=1
git.pkt.delim=1
git.pkt.bytes=4273
git.pkt.payload.max=4173
git.sideband.band1.bytes=4173
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=11
git.pack.bytes=4173
git.fetch.wide.objects=11
git.fetch.wide.extra_bytes=3930
git.fetch.wide.blobs=0
fetchtree:0200145 B · hit4 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=18
git.response.bytes=145
git.response.cache=HIT
git.response.duration_ms=4
git.fetch.filter=tree:0
git.fetch.wants=08b25f85c676220137ef481de00d814b6336a715
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=145
git.pkt.payload.max=118
git.sideband.band1.bytes=118
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0
git.pack.objects=1
git.pack.bytes=118
git.pack.trailer=e7b25ba0d135b7b9f7bbafe37a2bbcffac288e70
Open pack map
fetch2003.2 KiB · hit18 ms
git.upload-pack fetch
http.response.status_code=200
git.response.bytes=3235
git.response.cache=HIT
git.response.duration_ms=18
git.fetch.wants=dfb7f57883980a90395543df169da2992140300c
git.fetch.haves.count=0
git.fetch.deepen=0
git.fetch.thin_pack=false
git.fetch.ofs_delta=true
git.protocol.version=2
git.pkt.frames=4
git.pkt.data=3
git.pkt.flush=1
git.pkt.delim=0
git.pkt.bytes=3235
git.pkt.payload.max=3208
git.sideband.band1.bytes=3208
git.sideband.band2.bytes=0
git.sideband.band3.bytes=0

Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.

Remote
https://gitlab.com/gitlab-org/cli
Ref
refs/heads/main
Commit
0dd7574dce9e385c5c30540e6c15657adc6bdce9
cf-ray
a5e13003bc11a9fe
Request
30a6f6c0…
Protocol
v2 · sha1
Agent
git/2.56.g1891de4-Linux
Filter
advertised
Fetch
shallow wait-for-done filter
Capabilities
agent ls-refs fetch server-option object-format