MCPcopy
hub / github.com/restic/restic / Write

Method Write

internal/ui/table/table.go:115–209  ·  view source on GitHub ↗

Write prints the table to w.

(w io.Writer)

Source from the content-addressed store, hash-verified

113
114// Write prints the table to w.
115func (t *Table) Write(w io.Writer) error {
116 columns := len(t.templates)
117 if columns == 0 {
118 return nil
119 }
120
121 // collect all data fields from all columns
122 lines := make([][]string, 0, len(t.data))
123 buf := bytes.NewBuffer(nil)
124
125 for _, data := range t.data {
126 row := make([]string, 0, len(t.templates))
127 for _, tmpl := range t.templates {
128 err := tmpl.Execute(buf, data)
129 if err != nil {
130 return err
131 }
132
133 row = append(row, buf.String())
134 buf.Reset()
135 }
136 lines = append(lines, row)
137 }
138
139 // find max width for each cell
140 columnWidths := make([]int, columns)
141 for i, desc := range t.columns {
142 for _, line := range strings.Split(desc, "\n") {
143 width := ui.DisplayWidth(line)
144 if columnWidths[i] < width {
145 columnWidths[i] = width
146 }
147 }
148 }
149 for _, line := range lines {
150 for i, content := range line {
151 for _, l := range strings.Split(content, "\n") {
152 width := ui.DisplayWidth(l)
153 if columnWidths[i] < width {
154 columnWidths[i] = width
155 }
156 }
157 }
158 }
159
160 // calculate the total width of the table
161 totalWidth := 0
162 for _, width := range columnWidths {
163 totalWidth += width
164 }
165 totalWidth += (columns - 1) * ui.DisplayWidth(t.CellSeparator)
166
167 // write header
168 if len(t.columns) > 0 {
169 err := printLine(w, t.PrintHeader, t.CellSeparator, t.columns, columnWidths)
170 if err != nil {
171 return err
172 }

Callers 14

PosixClearCurrentLineFunction · 0.45
PosixMoveCursorUpFunction · 0.45
PosixMoveCursorDownFunction · 0.45
AddNodeMethod · 0.45
createTempDirFunction · 0.45
updateSnapshotsMethod · 0.45
writeNodeMethod · 0.45
dumpNodeZipMethod · 0.45
appendToFileFunction · 0.45
saveFunction · 0.45
writeTempfileFunction · 0.45
TestTableFunction · 0.45

Calls 5

DisplayWidthFunction · 0.92
printLineFunction · 0.85
ExecuteMethod · 0.80
ResetMethod · 0.65
StringMethod · 0.45

Tested by 6

createTempDirFunction · 0.36
appendToFileFunction · 0.36
saveFunction · 0.36
writeTempfileFunction · 0.36
TestTableFunction · 0.36
TestExtractToFileZipFunction · 0.36