| 1 |
# TableWriter for Go |
| 2 |
|
| 3 |
[](https://github.com/olekukonko/tablewriter/actions/workflows/go.yml) |
| 4 |
[](https://pkg.go.dev/github.com/olekukonko/tablewriter) |
| 5 |
[](https://goreportcard.com/report/github.com/olekukonko/tablewriter) |
| 6 |
[](LICENSE) |
| 7 |
[](README.md#benchmarks) |
| 8 |
|
| 9 |
`tablewriter` is a Go library for generating **rich text-based tables** with support for multiple output formats, including ASCII, Unicode, Markdown, HTML, and colorized terminals. Perfect for CLI tools, logs, and web applications. |
| 10 |
|
| 11 |
### Key Features |
| 12 |
- **Multi-format rendering**: ASCII, Unicode, Markdown, HTML, ANSI-colored |
| 13 |
- **Advanced styling**: Cell merging, alignment, padding, borders |
| 14 |
- **Flexible input**: CSV, structs, slices, or streaming data |
| 15 |
- **High performance**: Minimal allocations, buffer reuse |
| 16 |
- **Modern features**: Generics support, hierarchical merging, real-time streaming |
| 17 |
|
| 18 |
--- |
| 19 |
|
| 20 |
### Installation |
| 21 |
|
| 22 |
#### Legacy Version (v0.0.5) |
| 23 |
For use with legacy applications: |
| 24 |
```bash |
| 25 |
go get github.com/olekukonko/[email protected] |
| 26 |
``` |
| 27 |
|
| 28 |
#### Latest Version |
| 29 |
The latest stable version |
| 30 |
```bash |
| 31 |
go get github.com/olekukonko/[email protected] |
| 32 |
``` |
| 33 |
|
| 34 |
**Warning:** Version `v1.0.0` contains missing functionality and should not be used. |
| 35 |
|
| 36 |
|
| 37 |
> **Version Guidance** |
| 38 |
> - Legacy: Use `v0.0.5` (stable) |
| 39 |
> - New Features: Use `@latest` (includes generics, super fast streaming APIs) |
| 40 |
> - Legacy Docs: See [README_LEGACY.md](README_LEGACY.md) |
| 41 |
|
| 42 |
--- |
| 43 |
|
| 44 |
### Why TableWriter? |
| 45 |
- **CLI Ready**: Instant compatibility with terminal outputs |
| 46 |
- **Database Friendly**: Native support for `sql.Null*` types |
| 47 |
- **Secure**: Auto-escaping for HTML/Markdown |
| 48 |
- **Extensible**: Custom renderers and formatters |
| 49 |
|
| 50 |
--- |
| 51 |
|
| 52 |
### Quick Example |
| 53 |
```go |
| 54 |
package main |
| 55 |
|
| 56 |
import ( |
| 57 |
"github.com/olekukonko/tablewriter" |
| 58 |
"os" |
| 59 |
) |
| 60 |
|
| 61 |
func main() { |
| 62 |
data := [][]string{ |
| 63 |
{"Package", "Version", "Status"}, |
| 64 |
{"tablewriter", "v0.0.5", "legacy"}, |
| 65 |
{"tablewriter", "v1.1.5", "latest"}, |
| 66 |
} |
| 67 |
|
| 68 |
table := tablewriter.NewWriter(os.Stdout) |
| 69 |
table.Header(data[0]) |
| 70 |
table.Bulk(data[1:]) |
| 71 |
table.Render() |
| 72 |
} |
| 73 |
``` |
| 74 |
**Output**: |
| 75 |
``` |
| 76 |
┌─────────────┬─────────┬────────┐ |
| 77 |
│ PACKAGE │ VERSION │ STATUS │ |
| 78 |
├─────────────┼─────────┼────────┤ |
| 79 |
│ tablewriter │ v0.0.5 │ legacy │ |
| 80 |
│ tablewriter │ v1.1.5 │ latest │ |
| 81 |
└─────────────┴─────────┴────────┘ |
| 82 |
``` |
| 83 |
|
| 84 |
|
| 85 |
## Detailed Usage |
| 86 |
|
| 87 |
Create a table with `NewTable` or `NewWriter`, configure it using options or a `Config` struct, add data with `Append` or `Bulk`, and render to an `io.Writer`. Use renderers like `Blueprint` (ASCII), `HTML`, `Markdown`, `Colorized`, or `Ocean` (streaming). |
| 88 |
|
| 89 |
Here's how the API primitives map to the generated ASCII table: |
| 90 |
|
| 91 |
``` |
| 92 |
API Call ASCII Table Component |
| 93 |
-------- --------------------- |
| 94 |
|
| 95 |
table.Header([]string{"NAME", "AGE"}) ┌──────┬─────┐ ← Borders.Top |
| 96 |
│ NAME │ AGE │ ← Header row |
| 97 |
├──────┼─────┤ ← Lines.ShowTop (header separator) |
| 98 |
|
| 99 |
table.Append([]string{"Alice", "25"}) │ Alice│ 25 │ ← Data row |
| 100 |
├──────┼─────┤ ← Separators.BetweenRows |
| 101 |
|
| 102 |
table.Append([]string{"Bob", "30"}) │ Bob │ 30 │ ← Data row |
| 103 |
├──────┼─────┤ ← Lines.ShowBottom (footer separator) |
| 104 |
|
| 105 |
table.Footer([]string{"Total", "2"}) │ Total│ 2 │ ← Footer row |
| 106 |
└──────┴─────┘ ← Borders.Bottom |
| 107 |
``` |
| 108 |
|
| 109 |
The core components include: |
| 110 |
|
| 111 |
- **Renderer** - Implements the core interface for converting table data into output formats. Available renderers include Blueprint (ASCII), HTML, Markdown, Colorized (ASCII with color), Ocean (streaming ASCII), and SVG. |
| 112 |
|
| 113 |
- **Config** - The root configuration struct that controls all table behavior and appearance |
| 114 |
- **Behavior** - Controls high-level rendering behaviors including auto-hiding empty columns, trimming row whitespace, header/footer visibility, and compact mode for optimized merged cell calculations |
| 115 |
- **CellConfig** - The comprehensive configuration template used for table sections (header, row, footer). Combines formatting, padding, alignment, filtering, callbacks, and width constraints with global and per-column control |
| 116 |
- **StreamConfig** - Configuration for streaming mode including enable/disable state and strict column validation |
| 117 |
|
| 118 |
- **Rendition** - Defines how a renderer formats tables and contains the complete visual styling configuration |
| 119 |
- **Borders** - Control the outer frame visibility (top, bottom, left, right edges) of the table |
| 120 |
- **Lines** - Control horizontal boundary lines (above/below headers, above footers) that separate different table sections |
| 121 |
- **Separators** - Control the visibility of separators between rows and between columns within the table content |
| 122 |
- **Symbols** - Define the characters used for drawing table borders, corners, and junctions |
| 123 |
|
| 124 |
These components can be configured with various `tablewriter.With*()` functional options when creating a new table. |
| 125 |
|
| 126 |
## Examples |
| 127 |
|
| 128 |
### Basic Examples |
| 129 |
|
| 130 |
#### 1. Simple Tables |
| 131 |
|
| 132 |
Create a basic table with headers and rows. |
| 133 |
|
| 134 |
|
| 135 |
##### default |
| 136 |
|
| 137 |
```go |
| 138 |
package main |
| 139 |
|
| 140 |
import ( |
| 141 |
"fmt" |
| 142 |
"github.com/olekukonko/tablewriter" |
| 143 |
"os" |
| 144 |
) |
| 145 |
|
| 146 |
type Age int |
| 147 |
|
| 148 |
func (a Age) String() string { |
| 149 |
return fmt.Sprintf("%d yrs", a) |
| 150 |
} |
| 151 |
|
| 152 |
func main() { |
| 153 |
data := [][]any{ |
| 154 |
{"Alice", Age(25), "New York"}, |
| 155 |
{"Bob", Age(30), "Boston"}, |
| 156 |
} |
| 157 |
|
| 158 |
table := tablewriter.NewTable(os.Stdout) |
| 159 |
table.Header("Name", "Age", "City") |
| 160 |
table.Bulk(data) |
| 161 |
table.Render() |
| 162 |
} |
| 163 |
``` |
| 164 |
|
| 165 |
**Output**: |
| 166 |
|
| 167 |
``` |
| 168 |
┌───────┬────────┬──────────┐ |
| 169 |
│ NAME │ AGE │ CITY │ |
| 170 |
├───────┼────────┼──────────┤ |
| 171 |
│ Alice │ 25 yrs │ New York │ |
| 172 |
│ Bob │ 30 yrs │ Boston │ |
| 173 |
└───────┴────────┴──────────┘ |
| 174 |
|
| 175 |
``` |
| 176 |
|
| 177 |
|
| 178 |
##### with customization |
| 179 |
|
| 180 |
|
| 181 |
```go |
| 182 |
package main |
| 183 |
|
| 184 |
import ( |
| 185 |
"fmt" |
| 186 |
"github.com/olekukonko/tablewriter" |
| 187 |
"github.com/olekukonko/tablewriter/renderer" |
| 188 |
"github.com/olekukonko/tablewriter/tw" |
| 189 |
"os" |
| 190 |
) |
| 191 |
|
| 192 |
type Age int |
| 193 |
|
| 194 |
func (a Age) String() string { |
| 195 |
return fmt.Sprintf("%d yrs", a) |
| 196 |
} |
| 197 |
|
| 198 |
func main() { |
| 199 |
data := [][]any{ |
| 200 |
{"Alice", Age(25), "New York"}, |
| 201 |
{"Bob", Age(30), "Boston"}, |
| 202 |
} |
| 203 |
|
| 204 |
symbols := tw.NewSymbolCustom("Nature"). |
| 205 |
WithRow("~"). |
| 206 |
WithColumn("|"). |
| 207 |
WithTopLeft("🌱"). |
| 208 |
WithTopMid("🌿"). |
| 209 |
WithTopRight("🌱"). |
| 210 |
WithMidLeft("🍃"). |
| 211 |
WithCenter("❀"). |
| 212 |
WithMidRight("🍃"). |
| 213 |
WithBottomLeft("🌻"). |
| 214 |
WithBottomMid("🌾"). |
| 215 |
WithBottomRight("🌻") |
| 216 |
|
| 217 |
table := tablewriter.NewTable(os.Stdout, tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{Symbols: symbols}))) |
| 218 |
table.Header("Name", "Age", "City") |
| 219 |
table.Bulk(data) |
| 220 |
table.Render() |
| 221 |
} |
| 222 |
``` |
| 223 |
|
| 224 |
``` |
| 225 |
🌱~~~~~~❀~~~~~~~~❀~~~~~~~~~🌱 |
| 226 |
| NAME | AGE | CITY | |
| 227 |
🍃~~~~~~❀~~~~~~~~❀~~~~~~~~~🍃 |
| 228 |
| Alice | 25 yrs | New York | |
| 229 |
| Bob | 30 yrs | Boston | |
| 230 |
🌻~~~~~~❀~~~~~~~~❀~~~~~~~~~🌻 |
| 231 |
``` |
| 232 |
|
| 233 |
See [symbols example](https://github.com/olekukonko/tablewriter/blob/master/_example/symbols/main.go) for more |
| 234 |
|
| 235 |
#### 2. Markdown Table |
| 236 |
|
| 237 |
Generate a Markdown table for documentation. |
| 238 |
|
| 239 |
```go |
| 240 |
package main |
| 241 |
|
| 242 |
import ( |
| 243 |
"fmt" |
| 244 |
"github.com/olekukonko/tablewriter" |
| 245 |
"github.com/olekukonko/tablewriter/renderer" |
| 246 |
"os" |
| 247 |
"strings" |
| 248 |
"unicode" |
| 249 |
) |
| 250 |
|
| 251 |
type Name struct { |
| 252 |
First string |
| 253 |
Last string |
| 254 |
} |
| 255 |
|
| 256 |
// this will be ignored since Format() is present |
| 257 |
func (n Name) String() string { |
| 258 |
return fmt.Sprintf("%s %s", n.First, n.Last) |
| 259 |
} |
| 260 |
|
| 261 |
// Note: Format() overrides String() if both exist. |
| 262 |
func (n Name) Format() string { |
| 263 |
return fmt.Sprintf("%s %s", n.clean(n.First), n.clean(n.Last)) |
| 264 |
} |
| 265 |
|
| 266 |
// clean ensures the first letter is capitalized and the rest are lowercase |
| 267 |
func (n Name) clean(s string) string { |
| 268 |
s = strings.TrimSpace(strings.ToLower(s)) |
| 269 |
words := strings.Fields(s) |
| 270 |
s = strings.Join(words, "") |
| 271 |
|
| 272 |
if s == "" { |
| 273 |
return s |
| 274 |
} |
| 275 |
// Capitalize the first letter |
| 276 |
runes := []rune(s) |
| 277 |
runes[0] = unicode.ToUpper(runes[0]) |
| 278 |
return string(runes) |
| 279 |
} |
| 280 |
|
| 281 |
type Age int |
| 282 |
|
| 283 |
// Age int will be ignore and string will be used |
| 284 |
func (a Age) String() string { |
| 285 |
return fmt.Sprintf("%d yrs", a) |
| 286 |
} |
| 287 |
|
| 288 |
func main() { |
| 289 |
data := [][]any{ |
| 290 |
{Name{"Al i CE", " Ma SK"}, Age(25), "New York"}, |
| 291 |
{Name{"bOb", "mar le y"}, Age(30), "Boston"}, |
| 292 |
} |
| 293 |
|
| 294 |
table := tablewriter.NewTable(os.Stdout, |
| 295 |
tablewriter.WithRenderer(renderer.NewMarkdown()), |
| 296 |
) |
| 297 |
|
| 298 |
table.Header([]string{"Name", "Age", "City"}) |
| 299 |
table.Bulk(data) |
| 300 |
table.Render() |
| 301 |
} |
| 302 |
``` |
| 303 |
|
| 304 |
**Output**: |
| 305 |
|
| 306 |
``` |
| 307 |
| NAME | AGE | CITY | |
| 308 |
|:----------:|:------:|:--------:| |
| 309 |
| Alice Mask | 25 yrs | New York | |
| 310 |
| Bob Marley | 30 yrs | Boston | |
| 311 |
|
| 312 |
|
| 313 |
``` |
| 314 |
|
| 315 |
#### 3. CSV Input |
| 316 |
|
| 317 |
Create a table from a CSV file with custom row alignment. |
| 318 |
|
| 319 |
```go |
| 320 |
package main |
| 321 |
|
| 322 |
import ( |
| 323 |
"github.com/olekukonko/tablewriter" |
| 324 |
"github.com/olekukonko/tablewriter/tw" |
| 325 |
"log" |
| 326 |
"os" |
| 327 |
) |
| 328 |
|
| 329 |
func main() { |
| 330 |
// Assuming "test.csv" contains: "First Name,Last Name,SSN\nJohn,Barry,123456\nKathy,Smith,687987" |
| 331 |
table, err := tablewriter.NewCSV(os.Stdout, "test.csv", true) |
| 332 |
if err != nil { |
| 333 |
log.Fatalf("Error: %v", err) |
| 334 |
} |
| 335 |
|
| 336 |
table.Configure(func(config *tablewriter.Config) { |
| 337 |
config.Row.Alignment.Global = tw.AlignLeft |
| 338 |
}) |
| 339 |
table.Render() |
| 340 |
} |
| 341 |
``` |
| 342 |
|
| 343 |
**Output**: |
| 344 |
|
| 345 |
``` |
| 346 |
┌────────────┬───────────┬─────────┐ |
| 347 |
│ FIRST NAME │ LAST NAME │ SSN │ |
| 348 |
├────────────┼───────────┼─────────┤ |
| 349 |
│ John │ Barry │ 123456 │ |
| 350 |
│ Kathy │ Smith │ 687987 │ |
| 351 |
└────────────┴───────────┴─────────┘ |
| 352 |
``` |
| 353 |
|
| 354 |
### Advanced Examples |
| 355 |
|
| 356 |
#### 4. Colorized Table with Long Values |
| 357 |
|
| 358 |
Create a colorized table with wrapped long values, per-column colors, and a styled footer (inspired by `TestColorizedLongValues` and `TestColorizedCustomColors`). |
| 359 |
|
| 360 |
```go |
| 361 |
package main |
| 362 |
|
| 363 |
import ( |
| 364 |
"github.com/fatih/color" |
| 365 |
"github.com/olekukonko/tablewriter" |
| 366 |
"github.com/olekukonko/tablewriter/renderer" |
| 367 |
"github.com/olekukonko/tablewriter/tw" |
| 368 |
"os" |
| 369 |
) |
| 370 |
|
| 371 |
func main() { |
| 372 |
data := [][]string{ |
| 373 |
{"1", "This is a very long description that needs wrapping for readability", "OK"}, |
| 374 |
{"2", "Short description", "DONE"}, |
| 375 |
{"3", "Another lengthy description requiring truncation or wrapping", "ERROR"}, |
| 376 |
} |
| 377 |
|
| 378 |
// Configure colors: green headers, cyan/magenta rows, yellow footer |
| 379 |
colorCfg := renderer.ColorizedConfig{ |
| 380 |
Header: renderer.Tint{ |
| 381 |
FG: renderer.Colors{color.FgGreen, color.Bold}, // Green bold headers |
| 382 |
BG: renderer.Colors{color.BgHiWhite}, |
| 383 |
}, |
| 384 |
Column: renderer.Tint{ |
| 385 |
FG: renderer.Colors{color.FgCyan}, // Default cyan for rows |
| 386 |
Columns: []renderer.Tint{ |
| 387 |
{FG: renderer.Colors{color.FgMagenta}}, // Magenta for column 0 |
| 388 |
{}, // Inherit default (cyan) |
| 389 |
{FG: renderer.Colors{color.FgHiRed}}, // High-intensity red for column 2 |
| 390 |
}, |
| 391 |
}, |
| 392 |
Footer: renderer.Tint{ |
| 393 |
FG: renderer.Colors{color.FgYellow, color.Bold}, // Yellow bold footer |
| 394 |
Columns: []renderer.Tint{ |
| 395 |
{}, // Inherit default |
| 396 |
{FG: renderer.Colors{color.FgHiYellow}}, // High-intensity yellow for column 1 |
| 397 |
{}, // Inherit default |
| 398 |
}, |
| 399 |
}, |
| 400 |
Border: renderer.Tint{FG: renderer.Colors{color.FgWhite}}, // White borders |
| 401 |
Separator: renderer.Tint{FG: renderer.Colors{color.FgWhite}}, // White separators |
| 402 |
} |
| 403 |
|
| 404 |
table := tablewriter.NewTable(os.Stdout, |
| 405 |
tablewriter.WithRenderer(renderer.NewColorized(colorCfg)), |
| 406 |
tablewriter.WithConfig(tablewriter.Config{ |
| 407 |
Row: tw.CellConfig{ |
| 408 |
Formatting: tw.CellFormatting{AutoWrap: tw.WrapNormal}, // Wrap long content |
| 409 |
Alignment: tw.CellAlignment{Global: tw.AlignLeft}, // Left-align rows |
| 410 |
ColMaxWidths: tw.CellWidth{Global: 25}, |
| 411 |
}, |
| 412 |
Footer: tw.CellConfig{ |
| 413 |
Alignment: tw.CellAlignment{Global: tw.AlignRight}, |
| 414 |
}, |
| 415 |
}), |
| 416 |
) |
| 417 |
|
| 418 |
table.Header([]string{"ID", "Description", "Status"}) |
| 419 |
table.Bulk(data) |
| 420 |
table.Footer([]string{"", "Total", "3"}) |
| 421 |
table.Render() |
| 422 |
} |
| 423 |
``` |
| 424 |
|
| 425 |
**Output** (colors visible in ANSI-compatible terminals): |
| 426 |
|
| 427 |
 |
| 428 |
|
| 429 |
##### 24-bit (RGB / true color) tints |
| 430 |
|
| 431 |
Besides the named `color.Fg*`/`color.Bg*` attributes, tints accept 24-bit |
| 432 |
colors. `renderer.RGB` and `renderer.BgRGB` take red, green and blue channels |
| 433 |
(0-255, out-of-range values are clamped), while `renderer.Hex`/`renderer.BgHex` |
| 434 |
parse a `#RRGGBB` or `#RGB` string. All of them return a `renderer.Colors`, so |
| 435 |
they slot in wherever named attributes do and can be combined with `append`: |
| 436 |
|
| 437 |
```go |
| 438 |
orange, _ := renderer.Hex("#ff8800") |
| 439 |
|
| 440 |
colorCfg := renderer.ColorizedConfig{ |
| 441 |
// Bold orange headers on a dark-grey background. |
| 442 |
Header: renderer.Tint{ |
| 443 |
FG: append(orange, color.Bold), |
| 444 |
BG: renderer.BgRGB(30, 30, 30), |
| 445 |
}, |
| 446 |
// Teal rows. |
| 447 |
Column: renderer.Tint{FG: renderer.RGB(0, 200, 180)}, |
| 448 |
} |
| 449 |
``` |
| 450 |
|
| 451 |
Terminals without true-color support may approximate or ignore these colors. |
| 452 |
|
| 453 |
#### 5. Streaming Table with Truncation |
| 454 |
|
| 455 |
Stream a table incrementally with truncation and a footer, simulating a real-time data feed (inspired by `TestOceanStreamTruncation` and `TestOceanStreamSlowOutput`). |
| 456 |
|
| 457 |
```go |
| 458 |
package main |
| 459 |
|
| 460 |
import ( |
| 461 |
"github.com/olekukonko/tablewriter" |
| 462 |
"github.com/olekukonko/tablewriter/tw" |
| 463 |
"log" |
| 464 |
"os" |
| 465 |
"time" |
| 466 |
) |
| 467 |
|
| 468 |
func main() { |
| 469 |
table := tablewriter.NewTable(os.Stdout, tablewriter.WithStreaming(tw.StreamConfig{Enable: true})) |
| 470 |
|
| 471 |
// Start streaming |
| 472 |
if err := table.Start(); err != nil { |
| 473 |
log.Fatalf("Start failed: %v", err) |
| 474 |
} |
| 475 |
|
| 476 |
defer table.Close() |
| 477 |
|
| 478 |
// Stream header |
| 479 |
table.Header([]string{"ID", "Description", "Status"}) |
| 480 |
|
| 481 |
// Stream rows with simulated delay |
| 482 |
data := [][]string{ |
| 483 |
{"1", "This description is too long", "OK"}, |
| 484 |
{"2", "Short desc", "DONE"}, |
| 485 |
{"3", "Another long description here", "ERROR"}, |
| 486 |
} |
| 487 |
for _, row := range data { |
| 488 |
table.Append(row) |
| 489 |
time.Sleep(500 * time.Millisecond) // Simulate real-time data feed |
| 490 |
} |
| 491 |
|
| 492 |
// Stream footer |
| 493 |
table.Footer([]string{"", "Total", "3"}) |
| 494 |
} |
| 495 |
``` |
| 496 |
|
| 497 |
**Output** (appears incrementally): |
| 498 |
|
| 499 |
``` |
| 500 |
┌────────┬───────────────┬──────────┐ |
| 501 |
│ ID │ DESCRIPTION │ STATUS │ |
| 502 |
├────────┼───────────────┼──────────┤ |
| 503 |
│ 1 │ This │ OK │ |
| 504 |
│ │ description │ │ |
| 505 |
│ │ is too long │ │ |
| 506 |
│ 2 │ Short desc │ DONE │ |
| 507 |
│ 3 │ Another long │ ERROR │ |
| 508 |
│ │ description │ │ |
| 509 |
│ │ here │ │ |
| 510 |
├────────┼───────────────┼──────────┤ |
| 511 |
│ │ Total │ 3 │ |
| 512 |
└────────┴───────────────┴──────────┘ |
| 513 |
``` |
| 514 |
|
| 515 |
**Note**: Long descriptions are truncated with `…` due to fixed column widths. The output appears row-by-row, simulating a real-time feed. |
| 516 |
|
| 517 |
#### 6. Hierarchical Merging for Organizational Data |
| 518 |
|
| 519 |
Show hierarchical merging for a tree-like structure, such as an organizational hierarchy (inspired by `TestMergeHierarchicalUnicode`). |
| 520 |
|
| 521 |
```go |
| 522 |
package main |
| 523 |
|
| 524 |
import ( |
| 525 |
"github.com/olekukonko/tablewriter" |
| 526 |
"github.com/olekukonko/tablewriter/renderer" |
| 527 |
"github.com/olekukonko/tablewriter/tw" |
| 528 |
"os" |
| 529 |
) |
| 530 |
|
| 531 |
func main() { |
| 532 |
data := [][]string{ |
| 533 |
{"Engineering", "Backend", "API Team", "Alice"}, |
| 534 |
{"Engineering", "Backend", "Database Team", "Bob"}, |
| 535 |
{"Engineering", "Frontend", "UI Team", "Charlie"}, |
| 536 |
{"Marketing", "Digital", "SEO Team", "Dave"}, |
| 537 |
{"Marketing", "Digital", "Content Team", "Eve"}, |
| 538 |
} |
| 539 |
|
| 540 |
table := tablewriter.NewTable(os.Stdout, |
| 541 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ |
| 542 |
Settings: tw.Settings{Separators: tw.Separators{BetweenRows: tw.On}}, |
| 543 |
})), |
| 544 |
tablewriter.WithConfig(tablewriter.Config{ |
| 545 |
Header: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}}, |
| 546 |
Row: tw.CellConfig{ |
| 547 |
Merging: tw.CellMerging{Mode: tw.MergeHierarchical}, |
| 548 |
Alignment: tw.CellAlignment{Global: tw.AlignLeft}, |
| 549 |
}, |
| 550 |
}), |
| 551 |
) |
| 552 |
table.Header([]string{"Department", "Division", "Team", "Lead"}) |
| 553 |
table.Bulk(data) |
| 554 |
table.Render() |
| 555 |
} |
| 556 |
``` |
| 557 |
|
| 558 |
**Output**: |
| 559 |
|
| 560 |
``` |
| 561 |
┌────────────┬──────────┬──────────────┬────────┐ |
| 562 |
│ DEPARTMENT │ DIVISION │ TEAM │ LEAD │ |
| 563 |
├────────────┼──────────┼──────────────┼────────┤ |
| 564 |
│ Engineering│ Backend │ API Team │ Alice │ |
| 565 |
│ │ ├──────────────┼────────┤ |
| 566 |
│ │ │ Database Team│ Bob │ |
| 567 |
│ │ Frontend ├──────────────┼────────┤ |
| 568 |
│ │ │ UI Team │ Charlie│ |
| 569 |
├────────────┼──────────┼──────────────┼────────┤ |
| 570 |
│ Marketing │ Digital │ SEO Team │ Dave │ |
| 571 |
│ │ ├──────────────┼────────┤ |
| 572 |
│ │ │ Content Team │ Eve │ |
| 573 |
└────────────┴──────────┴──────────────┴────────┘ |
| 574 |
``` |
| 575 |
|
| 576 |
**Note**: Hierarchical merging groups repeated values (e.g., "Engineering" spans multiple rows, "Backend" spans two teams), creating a tree-like structure. |
| 577 |
|
| 578 |
#### 7. Custom Padding with Merging |
| 579 |
|
| 580 |
Showcase custom padding and combined horizontal/vertical merging (inspired by `TestMergeWithPadding` in `merge_test.go`). |
| 581 |
|
| 582 |
```go |
| 583 |
package main |
| 584 |
|
| 585 |
import ( |
| 586 |
"github.com/olekukonko/tablewriter" |
| 587 |
"github.com/olekukonko/tablewriter/renderer" |
| 588 |
"github.com/olekukonko/tablewriter/tw" |
| 589 |
"os" |
| 590 |
) |
| 591 |
|
| 592 |
func main() { |
| 593 |
data := [][]string{ |
| 594 |
{"1/1/2014", "Domain name", "Successful", "Successful"}, |
| 595 |
{"1/1/2014", "Domain name", "Pending", "Waiting"}, |
| 596 |
{"1/1/2014", "Domain name", "Successful", "Rejected"}, |
| 597 |
{"", "", "TOTAL", "$145.93"}, |
| 598 |
} |
| 599 |
|
| 600 |
table := tablewriter.NewTable(os.Stdout, |
| 601 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ |
| 602 |
Settings: tw.Settings{Separators: tw.Separators{BetweenRows: tw.On}}, |
| 603 |
})), |
| 604 |
tablewriter.WithConfig(tablewriter.Config{ |
| 605 |
Row: tw.CellConfig{ |
| 606 |
Merging: tw.CellMerging{Mode: tw.MergeBoth}, |
| 607 |
Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.Skip, tw.Skip, tw.AlignRight, tw.AlignLeft}}, |
| 608 |
}, |
| 609 |
|
| 610 |
Footer: tw.CellConfig{ |
| 611 |
Padding: tw.CellPadding{ |
| 612 |
Global: tw.Padding{Left: "*", Right: "*"}, |
| 613 |
PerColumn: []tw.Padding{{}, {}, {Bottom: "^"}, {Bottom: "^"}}, |
| 614 |
}, |
| 615 |
Alignment: tw.CellAlignment{PerColumn: []tw.Align{tw.Skip, tw.Skip, tw.AlignRight, tw.AlignLeft}}, |
| 616 |
}, |
| 617 |
}), |
| 618 |
) |
| 619 |
table.Header([]string{"Date", "Description", "Status", "Conclusion"}) |
| 620 |
table.Bulk(data) |
| 621 |
table.Render() |
| 622 |
} |
| 623 |
``` |
| 624 |
|
| 625 |
**Output**: |
| 626 |
|
| 627 |
``` |
| 628 |
┌──────────┬─────────────┬────────────┬────────────┐ |
| 629 |
│ DATE │ DESCRIPTION │ STATUS │ CONCLUSION │ |
| 630 |
├──────────┼─────────────┼────────────┴────────────┤ |
| 631 |
│ 1/1/2014 │ Domain name │ Successful │ |
| 632 |
│ │ ├────────────┬────────────┤ |
| 633 |
│ │ │ Pending │ Waiting │ |
| 634 |
│ │ ├────────────┼────────────┤ |
| 635 |
│ │ │ Successful │ Rejected │ |
| 636 |
├──────────┼─────────────┼────────────┼────────────┤ |
| 637 |
│ │ │ TOTAL │ $145.93 │ |
| 638 |
│ │ │^^^^^^^^^^^^│^^^^^^^^^^^^│ |
| 639 |
└──────────┴─────────────┴────────────┴────────────┘ |
| 640 |
``` |
| 641 |
|
| 642 |
#### 8. Nested Tables |
| 643 |
|
| 644 |
Create a table with nested sub-tables for complex layouts (inspired by `TestMasterClass` in `extra_test.go`). |
| 645 |
|
| 646 |
```go |
| 647 |
package main |
| 648 |
|
| 649 |
import ( |
| 650 |
"bytes" |
| 651 |
"github.com/olekukonko/tablewriter" |
| 652 |
"github.com/olekukonko/tablewriter/renderer" |
| 653 |
"github.com/olekukonko/tablewriter/tw" |
| 654 |
"os" |
| 655 |
) |
| 656 |
|
| 657 |
func main() { |
| 658 |
// Helper to create a sub-table |
| 659 |
createSubTable := func(s string) string { |
| 660 |
var buf bytes.Buffer |
| 661 |
table := tablewriter.NewTable(&buf, |
| 662 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ |
| 663 |
Borders: tw.BorderNone, |
| 664 |
Symbols: tw.NewSymbols(tw.StyleASCII), |
| 665 |
Settings: tw.Settings{ |
| 666 |
Separators: tw.Separators{BetweenRows: tw.On}, |
| 667 |
Lines: tw.Lines{ShowFooterLine: tw.On}, |
| 668 |
}, |
| 669 |
})), |
| 670 |
tablewriter.WithConfig(tablewriter.Config{ |
| 671 |
MaxWidth: 10, |
| 672 |
Row: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}}, |
| 673 |
}), |
| 674 |
) |
| 675 |
table.Append([]string{s, s}) |
| 676 |
table.Append([]string{s, s}) |
| 677 |
table.Render() |
| 678 |
return buf.String() |
| 679 |
} |
| 680 |
|
| 681 |
// Main table |
| 682 |
table := tablewriter.NewTable(os.Stdout, |
| 683 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ |
| 684 |
Borders: tw.BorderNone, |
| 685 |
Settings: tw.Settings{Separators: tw.Separators{BetweenColumns: tw.On}}, |
| 686 |
})), |
| 687 |
tablewriter.WithConfig(tablewriter.Config{ |
| 688 |
MaxWidth: 30, |
| 689 |
Row: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignCenter}}, |
| 690 |
}), |
| 691 |
) |
| 692 |
table.Append([]string{createSubTable("A"), createSubTable("B")}) |
| 693 |
table.Append([]string{createSubTable("C"), createSubTable("D")}) |
| 694 |
table.Render() |
| 695 |
} |
| 696 |
``` |
| 697 |
|
| 698 |
**Output**: |
| 699 |
|
| 700 |
``` |
| 701 |
A | A │ B | B |
| 702 |
---+--- │ ---+--- |
| 703 |
A | A │ B | B |
| 704 |
C | C │ D | D |
| 705 |
---+--- │ ---+--- |
| 706 |
C | C │ D | D |
| 707 |
``` |
| 708 |
|
| 709 |
#### 9. Structs with Database |
| 710 |
|
| 711 |
Render a table from a slice of structs, simulating a database query (inspired by `TestStructTableWithDB` in `struct_test.go`). |
| 712 |
|
| 713 |
```go |
| 714 |
package main |
| 715 |
|
| 716 |
import ( |
| 717 |
"fmt" |
| 718 |
"github.com/olekukonko/tablewriter" |
| 719 |
"github.com/olekukonko/tablewriter/renderer" |
| 720 |
"github.com/olekukonko/tablewriter/tw" |
| 721 |
"os" |
| 722 |
) |
| 723 |
|
| 724 |
type Employee struct { |
| 725 |
ID int |
| 726 |
Name string |
| 727 |
Age int |
| 728 |
Department string |
| 729 |
Salary float64 |
| 730 |
} |
| 731 |
|
| 732 |
func employeeStringer(e interface{}) []string { |
| 733 |
emp, ok := e.(Employee) |
| 734 |
if !ok { |
| 735 |
return []string{"Error: Invalid type"} |
| 736 |
} |
| 737 |
return []string{ |
| 738 |
fmt.Sprintf("%d", emp.ID), |
| 739 |
emp.Name, |
| 740 |
fmt.Sprintf("%d", emp.Age), |
| 741 |
emp.Department, |
| 742 |
fmt.Sprintf("%.2f", emp.Salary), |
| 743 |
} |
| 744 |
} |
| 745 |
|
| 746 |
func main() { |
| 747 |
employees := []Employee{ |
| 748 |
{ID: 1, Name: "Alice Smith", Age: 28, Department: "Engineering", Salary: 75000.50}, |
| 749 |
{ID: 2, Name: "Bob Johnson", Age: 34, Department: "Marketing", Salary: 62000.00}, |
| 750 |
{ID: 3, Name: "Charlie Brown", Age: 45, Department: "HR", Salary: 80000.75}, |
| 751 |
} |
| 752 |
|
| 753 |
table := tablewriter.NewTable(os.Stdout, |
| 754 |
tablewriter.WithRenderer(renderer.NewBlueprint(tw.Rendition{ |
| 755 |
Symbols: tw.NewSymbols(tw.StyleRounded), |
| 756 |
})), |
| 757 |
tablewriter.WithStringer(employeeStringer), |
| 758 |
tablewriter.WithConfig(tablewriter.Config{ |
| 759 |
Header: tw.CellConfig{ |
| 760 |
Formatting: tw.CellFormatting{AutoFormat: tw.On}, |
| 761 |
Alignment: tw.CellAlignment{Global: tw.AlignCenter}, |
| 762 |
}, |
| 763 |
Row: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignLeft}}, |
| 764 |
Footer: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignRight}}, |
| 765 |
}), |
| 766 |
) |
| 767 |
table.Header([]string{"ID", "Name", "Age", "Department", "Salary"}) |
| 768 |
|
| 769 |
for _, emp := range employees { |
| 770 |
table.Append(emp) |
| 771 |
} |
| 772 |
|
| 773 |
totalSalary := 0.0 |
| 774 |
for _, emp := range employees { |
| 775 |
totalSalary += emp.Salary |
| 776 |
} |
| 777 |
table.Footer([]string{"", "", "", "Total", fmt.Sprintf("%.2f", totalSalary)}) |
| 778 |
table.Render() |
| 779 |
} |
| 780 |
``` |
| 781 |
|
| 782 |
**Output**: |
| 783 |
|
| 784 |
``` |
| 785 |
╭────┬───────────────┬─────┬─────────────┬───────────╮ |
| 786 |
│ ID │ NAME │ AGE │ DEPARTMENT │ SALARY │ |
| 787 |
├────┼───────────────┼─────┼─────────────┼───────────┤ |
| 788 |
│ 1 │ Alice Smith │ 28 │ Engineering │ 75000.50 │ |
| 789 |
│ 2 │ Bob Johnson │ 34 │ Marketing │ 62000.00 │ |
| 790 |
│ 3 │ Charlie Brown │ 45 │ HR │ 80000.75 │ |
| 791 |
├────┼───────────────┼─────┼─────────────┼───────────┤ |
| 792 |
│ │ │ │ Total │ 217001.25 │ |
| 793 |
╰────┴───────────────┴─────┴─────────────┴───────────╯ |
| 794 |
``` |
| 795 |
|
| 796 |
|
| 797 |
#### 10. Simple Html Table |
| 798 |
|
| 799 |
|
| 800 |
```go |
| 801 |
package main |
| 802 |
|
| 803 |
import ( |
| 804 |
"github.com/olekukonko/tablewriter" |
| 805 |
"github.com/olekukonko/tablewriter/renderer" |
| 806 |
"github.com/olekukonko/tablewriter/tw" |
| 807 |
"os" |
| 808 |
) |
| 809 |
|
| 810 |
func main() { |
| 811 |
data := [][]string{ |
| 812 |
{"North", "Q1 & Q2", "Q1 & Q2", "$2200.00"}, |
| 813 |
{"South", "Q1", "Q1", "$1000.00"}, |
| 814 |
{"South", "Q2", "Q2", "$1200.00"}, |
| 815 |
} |
| 816 |
|
| 817 |
// Configure HTML with custom CSS classes and content escaping |
| 818 |
htmlCfg := renderer.HTMLConfig{ |
| 819 |
TableClass: "sales-table", |
| 820 |
HeaderClass: "table-header", |
| 821 |
BodyClass: "table-body", |
| 822 |
FooterClass: "table-footer", |
| 823 |
RowClass: "table-row", |
| 824 |
HeaderRowClass: "header-row", |
| 825 |
FooterRowClass: "footer-row", |
| 826 |
EscapeContent: true, // Escape HTML characters (e.g., "&" to "&") |
| 827 |
} |
| 828 |
|
| 829 |
table := tablewriter.NewTable(os.Stdout, |
| 830 |
tablewriter.WithRenderer(renderer.NewHTML(htmlCfg)), |
| 831 |
tablewriter.WithConfig(tablewriter.Config{ |
| 832 |
Header: tw.CellConfig{ |
| 833 |
Merging: tw.CellMerging{Mode: tw.MergeHorizontal}, // Merge identical header cells |
| 834 |
Alignment: tw.CellAlignment{Global: tw.AlignCenter}, |
| 835 |
}, |
| 836 |
Row: tw.CellConfig{ |
| 837 |
Merging: tw.CellMerging{Mode: tw.MergeHorizontal}, // Merge identical row cells |
| 838 |
Alignment: tw.CellAlignment{Global: tw.AlignLeft}, |
| 839 |
}, |
| 840 |
Footer: tw.CellConfig{Alignment: tw.CellAlignment{Global: tw.AlignRight}}, |
| 841 |
}), |
| 842 |
) |
| 843 |
|
| 844 |
table.Header([]string{"Region", "Quarter", "Quarter", "Sales"}) |
| 845 |
table.Bulk(data) |
| 846 |
table.Footer([]string{"", "", "Total", "$4400.00"}) |
| 847 |
table.Render() |
| 848 |
} |
| 849 |
``` |
| 850 |
|
| 851 |
**Output**: |
| 852 |
|
| 853 |
``` |
| 854 |
<table class="sales-table"> |
| 855 |
<thead class="table-header"> |
| 856 |
<tr class="header-row"> |
| 857 |
<th style="text-align: center;">REGION</th> |
| 858 |
<th colspan="2" style="text-align: center;">QUARTER</th> |
| 859 |
<th style="text-align: center;">SALES</th> |
| 860 |
</tr> |
| 861 |
</thead> |
| 862 |
<tbody class="table-body"> |
| 863 |
<tr class="table-row"> |
| 864 |
<td style="text-align: left;">North</td> |
| 865 |
<td colspan="2" style="text-align: left;">Q1 & Q2</td> |
| 866 |
<td style="text-align: left;">$2200.00</td> |
| 867 |
</tr> |
| 868 |
<tr class="table-row"> |
| 869 |
<td style="text-align: left;">South</td> |
| 870 |
<td colspan="2" style="text-align: left;">Q1</td> |
| 871 |
<td style="text-align: left;">$1000.00</td> |
| 872 |
</tr> |
| 873 |
<tr class="table-row"> |
| 874 |
<td style="text-align: left;">South</td> |
| 875 |
<td colspan="2" style="text-align: left;">Q2</td> |
| 876 |
<td style="text-align: left;">$1200.00</td> |
| 877 |
</tr> |
| 878 |
</tbody> |
| 879 |
<tfoot class="table-footer"> |
| 880 |
<tr class="footer-row"> |
| 881 |
<td style="text-align: right;"></td> |
| 882 |
<td style="text-align: right;"></td> |
| 883 |
<td style="text-align: right;">Total</td> |
| 884 |
<td style="text-align: right;">$4400.00</td> |
| 885 |
</tr> |
| 886 |
</tfoot> |
| 887 |
</table> |
| 888 |
|
| 889 |
``` |
| 890 |
|
| 891 |
#### 11. SVG Support |
| 892 |
```go |
| 893 |
package main |
| 894 |
|
| 895 |
import ( |
| 896 |
"fmt" |
| 897 |
"github.com/olekukonko/ll" |
| 898 |
"github.com/olekukonko/tablewriter" |
| 899 |
"github.com/olekukonko/tablewriter/renderer" |
| 900 |
"os" |
| 901 |
) |
| 902 |
|
| 903 |
type Age int |
| 904 |
|
| 905 |
func (a Age) String() string { |
| 906 |
return fmt.Sprintf("%d yrs", a) |
| 907 |
} |
| 908 |
|
| 909 |
func main() { |
| 910 |
data := [][]any{ |
| 911 |
{"Alice", Age(25), "New York"}, |
| 912 |
{"Bob", Age(30), "Boston"}, |
| 913 |
} |
| 914 |
|
| 915 |
file, err := os.OpenFile("out.svg", os.O_CREATE|os.O_WRONLY, 0644) |
| 916 |
if err != nil { |
| 917 |
ll.Fatal(err) |
| 918 |
} |
| 919 |
defer file.Close() |
| 920 |
|
| 921 |
table := tablewriter.NewTable(file, tablewriter.WithRenderer(renderer.NewSVG())) |
| 922 |
table.Header("Name", "Age", "City") |
| 923 |
table.Bulk(data) |
| 924 |
table.Render() |
| 925 |
} |
| 926 |
``` |
| 927 |
|
| 928 |
```go |
| 929 |
<svg xmlns="http://www.w3.org/2000/svg" width="170.80" height="84.40" font-family="sans-serif" font-size="12.00"> |
| 930 |
<style>text { stroke: none; }</style> |
| 931 |
<rect x="1.00" y="1.00" width="46.00" height="26.80" fill="#F0F0F0"/> |
| 932 |
<text x="24.00" y="14.40" fill="black" text-anchor="middle" dominant-baseline="middle">NAME</text> |
| 933 |
<rect x="48.00" y="1.00" width="53.20" height="26.80" fill="#F0F0F0"/> |
| 934 |
<text x="74.60" y="14.40" fill="black" text-anchor="middle" dominant-baseline="middle">AGE</text> |
| 935 |
<rect x="102.20" y="1.00" width="67.60" height="26.80" fill="#F0F0F0"/> |
| 936 |
<text x="136.00" y="14.40" fill="black" text-anchor="middle" dominant-baseline="middle">CITY</text> |
| 937 |
<rect x="1.00" y="28.80" width="46.00" height="26.80" fill="white"/> |
| 938 |
<text x="6.00" y="42.20" fill="black" text-anchor="start" dominant-baseline="middle">Alice</text> |
| 939 |
<rect x="48.00" y="28.80" width="53.20" height="26.80" fill="white"/> |
| 940 |
<text x="53.00" y="42.20" fill="black" text-anchor="start" dominant-baseline="middle">25 yrs</text> |
| 941 |
<rect x="102.20" y="28.80" width="67.60" height="26.80" fill="white"/> |
| 942 |
<text x="107.20" y="42.20" fill="black" text-anchor="start" dominant-baseline="middle">New York</text> |
| 943 |
<rect x="1.00" y="56.60" width="46.00" height="26.80" fill="#F9F9F9"/> |
| 944 |
<text x="6.00" y="70.00" fill="black" text-anchor="start" dominant-baseline="middle">Bob</text> |
| 945 |
<rect x="48.00" y="56.60" width="53.20" height="26.80" fill="#F9F9F9"/> |
| 946 |
<text x="53.00" y="70.00" fill="black" text-anchor="start" dominant-baseline="middle">30 yrs</text> |
| 947 |
<rect x="102.20" y="56.60" width="67.60" height="26.80" fill="#F9F9F9"/> |
| 948 |
<text x="107.20" y="70.00" fill="black" text-anchor="start" dominant-baseline="middle">Boston</text> |
| 949 |
<g class="table-borders" stroke="black" stroke-width="1.00" stroke-linecap="square"> |
| 950 |
<line x1="0.50" y1="0.50" x2="170.30" y2="0.50" /> |
| 951 |
<line x1="0.50" y1="28.30" x2="170.30" y2="28.30" /> |
| 952 |
<line x1="0.50" y1="56.10" x2="170.30" y2="56.10" /> |
| 953 |
<line x1="0.50" y1="83.90" x2="170.30" y2="83.90" /> |
| 954 |
<line x1="0.50" y1="0.50" x2="0.50" y2="83.90" /> |
| 955 |
<line x1="47.50" y1="0.50" x2="47.50" y2="83.90" /> |
| 956 |
<line x1="101.70" y1="0.50" x2="101.70" y2="83.90" /> |
| 957 |
<line x1="170.30" y1="0.50" x2="170.30" y2="83.90" /> |
| 958 |
</g> |
| 959 |
</svg> |
| 960 |
|
| 961 |
``` |
| 962 |
|
| 963 |
#### 12 Simple Application |
| 964 |
|
| 965 |
|
| 966 |
```go |
| 967 |
package main |
| 968 |
|
| 969 |
import ( |
| 970 |
"fmt" |
| 971 |
"github.com/olekukonko/tablewriter" |
| 972 |
"github.com/olekukonko/tablewriter/tw" |
| 973 |
"io/fs" |
| 974 |
"os" |
| 975 |
"path/filepath" |
| 976 |
"strings" |
| 977 |
"time" |
| 978 |
) |
| 979 |
|
| 980 |
const ( |
| 981 |
folder = "📁" |
| 982 |
file = "📄" |
| 983 |
baseDir = "../" |
| 984 |
indentStr = " " |
| 985 |
) |
| 986 |
|
| 987 |
func main() { |
| 988 |
table := tablewriter.NewTable(os.Stdout, tablewriter.WithTrimSpace(tw.Off)) |
| 989 |
table.Header([]string{"Tree", "Size", "Permissions", "Modified"}) |
| 990 |
err := filepath.WalkDir(baseDir, func(path string, d fs.DirEntry, err error) error { |
| 991 |
if err != nil { |
| 992 |
return err |
| 993 |
} |
| 994 |
|
| 995 |
if d.Name() == "." || d.Name() == ".." { |
| 996 |
return nil |
| 997 |
} |
| 998 |
|
| 999 |
// Calculate relative path depth |
| 1000 |
relPath, err := filepath.Rel(baseDir, path) |
| 1001 |
if err != nil { |
| 1002 |
return err |
| 1003 |
} |
| 1004 |
|
| 1005 |
depth := 0 |
| 1006 |
if relPath != "." { |
| 1007 |
depth = len(strings.Split(relPath, string(filepath.Separator))) - 1 |
| 1008 |
} |
| 1009 |
|
| 1010 |
indent := strings.Repeat(indentStr, depth) |
| 1011 |
|
| 1012 |
var name string |
| 1013 |
if d.IsDir() { |
| 1014 |
name = fmt.Sprintf("%s%s %s", indent, folder, d.Name()) |
| 1015 |
} else { |
| 1016 |
name = fmt.Sprintf("%s%s %s", indent, file, d.Name()) |
| 1017 |
} |
| 1018 |
|
| 1019 |
info, err := d.Info() |
| 1020 |
if err != nil { |
| 1021 |
return err |
| 1022 |
} |
| 1023 |
|
| 1024 |
table.Append([]string{ |
| 1025 |
name, |
| 1026 |
Size(info.Size()).String(), |
| 1027 |
info.Mode().String(), |
| 1028 |
Time(info.ModTime()).Format(), |
| 1029 |
}) |
| 1030 |
|
| 1031 |
return nil |
| 1032 |
}) |
| 1033 |
|
| 1034 |
if err != nil { |
| 1035 |
fmt.Fprintf(os.Stdout, "Error: %v\n", err) |
| 1036 |
return |
| 1037 |
} |
| 1038 |
|
| 1039 |
table.Render() |
| 1040 |
} |
| 1041 |
|
| 1042 |
const ( |
| 1043 |
KB = 1024 |
| 1044 |
MB = KB * 1024 |
| 1045 |
GB = MB * 1024 |
| 1046 |
TB = GB * 1024 |
| 1047 |
) |
| 1048 |
|
| 1049 |
type Size int64 |
| 1050 |
|
| 1051 |
func (s Size) String() string { |
| 1052 |
switch { |
| 1053 |
case s < KB: |
| 1054 |
return fmt.Sprintf("%d B", s) |
| 1055 |
case s < MB: |
| 1056 |
return fmt.Sprintf("%.2f KB", float64(s)/KB) |
| 1057 |
case s < GB: |
| 1058 |
return fmt.Sprintf("%.2f MB", float64(s)/MB) |
| 1059 |
case s < TB: |
| 1060 |
return fmt.Sprintf("%.2f GB", float64(s)/GB) |
| 1061 |
default: |
| 1062 |
return fmt.Sprintf("%.2f TB", float64(s)/TB) |
| 1063 |
} |
| 1064 |
} |
| 1065 |
|
| 1066 |
type Time time.Time |
| 1067 |
|
| 1068 |
func (t Time) Format() string { |
| 1069 |
now := time.Now() |
| 1070 |
diff := now.Sub(time.Time(t)) |
| 1071 |
|
| 1072 |
if diff.Seconds() < 60 { |
| 1073 |
return "just now" |
| 1074 |
} else if diff.Minutes() < 60 { |
| 1075 |
return fmt.Sprintf("%d minutes ago", int(diff.Minutes())) |
| 1076 |
} else if diff.Hours() < 24 { |
| 1077 |
return fmt.Sprintf("%d hours ago", int(diff.Hours())) |
| 1078 |
} else if diff.Hours() < 24*7 { |
| 1079 |
return fmt.Sprintf("%d days ago", int(diff.Hours()/24)) |
| 1080 |
} else { |
| 1081 |
return time.Time(t).Format("Jan 2, 2006") |
| 1082 |
} |
| 1083 |
} |
| 1084 |
|
| 1085 |
``` |
| 1086 |
|
| 1087 |
``` |
| 1088 |
┌──────────────────┬─────────┬─────────────┬──────────────┐ |
| 1089 |
│ TREE │ SIZE │ PERMISSIONS │ MODIFIED │ |
| 1090 |
├──────────────────┼─────────┼─────────────┼──────────────┤ |
| 1091 |
│ 📁 filetable │ 160 B │ drwxr-xr-x │ just now │ |
| 1092 |
│ 📄 main.go │ 2.19 KB │ -rw-r--r-- │ 22 hours ago │ |
| 1093 |
│ 📄 out.txt │ 0 B │ -rw-r--r-- │ just now │ |
| 1094 |
│ 📁 testdata │ 128 B │ drwxr-xr-x │ 1 days ago │ |
| 1095 |
│ 📄 a.txt │ 11 B │ -rw-r--r-- │ 1 days ago │ |
| 1096 |
│ 📄 b.txt │ 17 B │ -rw-r--r-- │ 1 days ago │ |
| 1097 |
│ 📁 symbols │ 128 B │ drwxr-xr-x │ just now │ |
| 1098 |
│ 📄 main.go │ 4.58 KB │ -rw-r--r-- │ 1 hours ago │ |
| 1099 |
│ 📄 out.txt │ 8.72 KB │ -rw-r--r-- │ just now │ |
| 1100 |
└──────────────────┴─────────┴─────────────┴──────────────┘ |
| 1101 |
``` |
| 1102 |
|
| 1103 |
|
| 1104 |
## Changes |
| 1105 |
|
| 1106 |
- `AutoFormat` changes See [#261](https://github.com/olekukonko/tablewriter/issues/261) |
| 1107 |
|
| 1108 |
## What is new |
| 1109 |
- `Counting` changes See [#294](https://github.com/olekukonko/tablewriter/issues/294) |
| 1110 |
|
| 1111 |
## Command-Line Tool |
| 1112 |
|
| 1113 |
The `csv2table` tool converts CSV files to ASCII tables. See `cmd/csv2table/csv2table.go` for details. |
| 1114 |
|
| 1115 |
Example usage: |
| 1116 |
|
| 1117 |
```bash |
| 1118 |
csv2table -f test.csv -h true -a left |
| 1119 |
``` |
| 1120 |
|
| 1121 |
## Contributing |
| 1122 |
|
| 1123 |
Contributions are welcome! Submit issues or pull requests to the [GitHub repository](https://github.com/olekukonko/tablewriter). |
| 1124 |
|
| 1125 |
## License |
| 1126 |
|
| 1127 |
MIT License. See the [LICENSE](LICENSE) file for details. |