Skip to content

Commit b53a03e

Browse files
committed
docs: expand progress demos and table usage
1 parent dd2f1fd commit b53a03e

4 files changed

Lines changed: 210 additions & 2 deletions

File tree

docs/progress.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,35 @@ The default messages are `done`, `failed`, and `skipped`. `Done()` advances to t
405405
- `Len() int`
406406
- `VisibleLen() int`
407407

408+
### Runnable Demos
409+
410+
The `examples` directory contains runnable progress demos. They do not perform real network downloads; all work is simulated with in-memory data, timers, and goroutines.
411+
412+
Fixed worker slot and safe logging demo:
413+
414+
```bash
415+
go run ./examples/progress-multi-demo
416+
```
417+
418+
Render mode demo:
419+
420+
```bash
421+
go run ./examples/progress-render-mode-demo auto
422+
go run ./examples/progress-render-mode-demo dynamic
423+
go run ./examples/progress-render-mode-demo plain
424+
go run ./examples/progress-render-mode-demo disabled
425+
```
426+
427+
`auto` selects `RenderDynamic` for interactive terminals and `RenderPlain` for non-interactive writers. `plain` is useful for CI logs and redirected output. `disabled` suppresses progress rendering while keeping log output.
428+
429+
Byte tracker and concurrent writer demo:
430+
431+
```bash
432+
go run ./examples/progress-byte-tracker-demo
433+
```
434+
435+
This demo shows both `ByteTracker.Add()` from simulated chunk workers and `NewConcurrentWriterWithInterval()` with `io.Copy()` over an in-memory reader.
436+
408437
## Progress Bar
409438

410439
### Internal Widgets

docs/show.md

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ Env
156156

157157
### Table
158158

159-
Use `show/table` when you need a tabular layout:
159+
Use `show/table` when command output needs aligned columns, such as user lists, process summaries, package results, or deployment reports.
160+
161+
Basic table:
160162

161163
```go
162164
tb := table.New("Users")
@@ -178,6 +180,79 @@ Users
178180
+----+------+
179181
```
180182

183+
Rows can also be loaded in batches. `SetRows()` accepts common data shapes such as `[][]any`, `[]map[string]any`, and slices of structs:
184+
185+
```go
186+
type Package struct {
187+
Name string `json:"name"`
188+
Version string `json:"version"`
189+
Status string `json:"status"`
190+
}
191+
192+
rows := []Package{
193+
{Name: "fd", Version: "10.2.0", Status: "installed"},
194+
{Name: "bat", Version: "0.25.0", Status: "pending"},
195+
}
196+
197+
tb := table.New("Packages")
198+
tb.SetRows(rows)
199+
tb.Println()
200+
```
201+
202+
For simple two-dimensional data, set headers first and pass a slice of rows:
203+
204+
```go
205+
tb := table.New("Jobs")
206+
tb.SetHeads("Name", "Status", "Duration")
207+
tb.SetRows([][]any{
208+
{"build", "ok", "12s"},
209+
{"test", "failed", "31s"},
210+
})
211+
tb.Println()
212+
```
213+
214+
Table styles and borders are configurable:
215+
216+
```go
217+
tb := table.New("Release",
218+
table.WithStyle(table.StyleRounded),
219+
table.WithBorderFlags(table.BorderAll),
220+
table.WithShowRowNumber(true),
221+
)
222+
tb.SetHeads("Package", "Status")
223+
tb.AddRow("cliui", "ready")
224+
tb.AddRow("docs", "updated")
225+
tb.Println()
226+
```
227+
228+
Built-in styles include `StyleSimple`, `StyleMySql`, `StyleMarkdown`, `StyleBold`, `StyleBoldBorder`, `StyleRounded`, `StyleDouble`, and `StyleMinimal`.
229+
230+
For long text, configure column width and overflow behavior:
231+
232+
```go
233+
tb := table.New("Tasks")
234+
tb.SetHeads("Task", "Description")
235+
tb.AddRow("download", "Fetch archive from mirror and verify checksum")
236+
tb.AddRow("extract", "Unpack files into the selected destination")
237+
tb.WithOptions(
238+
table.WithColumnWidths(12, 32),
239+
table.WithColMaxWidth(32),
240+
table.WithOverflowFlag(table.OverflowWrap),
241+
)
242+
tb.Println()
243+
```
244+
245+
Use `OverflowCut` to truncate long content, or `OverflowWrap` to wrap it across multiple display lines. Use `WithSortColumn(index, ascending)` to sort rows by a column:
246+
247+
```go
248+
tb := table.New("Results")
249+
tb.SetHeads("Name", "Status")
250+
tb.AddRow("test", "failed")
251+
tb.AddRow("build", "ok")
252+
tb.WithOptions(table.WithSortColumn(0, true))
253+
tb.Println()
254+
```
255+
181256
### JSON
182257

183258
`JSON` prints formatted structured objects. It is useful for debugging, showing API responses, or returning machine-readable output.

docs/zh-CN/progress.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,35 @@ bar.Skip()
423423
- `Len() int`
424424
- `VisibleLen() int`
425425

426+
### 可运行 Demo
427+
428+
`examples` 目录中提供了可直接运行的 progress 示例。这些示例不会进行真实网络下载,内部通过内存数据、定时器和 goroutine 模拟任务进度。
429+
430+
固定 worker slot 和安全日志示例:
431+
432+
```bash
433+
go run ./examples/progress-multi-demo
434+
```
435+
436+
render mode 示例:
437+
438+
```bash
439+
go run ./examples/progress-render-mode-demo auto
440+
go run ./examples/progress-render-mode-demo dynamic
441+
go run ./examples/progress-render-mode-demo plain
442+
go run ./examples/progress-render-mode-demo disabled
443+
```
444+
445+
`auto` 会在交互终端中选择 `RenderDynamic`,在非交互 writer 中选择 `RenderPlain``plain` 适合 CI 日志和重定向输出,`disabled` 会关闭 progress 渲染但保留日志输出。
446+
447+
byte tracker 和 concurrent writer 示例:
448+
449+
```bash
450+
go run ./examples/progress-byte-tracker-demo
451+
```
452+
453+
该示例同时展示了多个模拟 chunk worker 调用 `ByteTracker.Add()`,以及 `NewConcurrentWriterWithInterval()` 搭配 `io.Copy()` 处理内存 reader 的用法。
454+
426455
## Progress Bar
427456

428457
### 内置 Widgets

docs/zh-CN/show.md

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ Env
156156

157157
### Table
158158

159-
需要表格布局时,可以使用 `show/table`
159+
命令输出需要按列对齐时,可以使用 `show/table`,例如用户列表、进程摘要、包处理结果或部署报告。
160+
161+
基础表格:
160162

161163
```go
162164
tb := table.New("Users")
@@ -178,6 +180,79 @@ Users
178180
+----+------+
179181
```
180182

183+
也可以批量加载行数据。`SetRows()` 支持常见数据形态,例如 `[][]any``[]map[string]any` 和结构体切片:
184+
185+
```go
186+
type Package struct {
187+
Name string `json:"name"`
188+
Version string `json:"version"`
189+
Status string `json:"status"`
190+
}
191+
192+
rows := []Package{
193+
{Name: "fd", Version: "10.2.0", Status: "installed"},
194+
{Name: "bat", Version: "0.25.0", Status: "pending"},
195+
}
196+
197+
tb := table.New("Packages")
198+
tb.SetRows(rows)
199+
tb.Println()
200+
```
201+
202+
对于简单二维数据,可以先设置表头,再传入行切片:
203+
204+
```go
205+
tb := table.New("Jobs")
206+
tb.SetHeads("Name", "Status", "Duration")
207+
tb.SetRows([][]any{
208+
{"build", "ok", "12s"},
209+
{"test", "failed", "31s"},
210+
})
211+
tb.Println()
212+
```
213+
214+
表格样式和边框可以配置:
215+
216+
```go
217+
tb := table.New("Release",
218+
table.WithStyle(table.StyleRounded),
219+
table.WithBorderFlags(table.BorderAll),
220+
table.WithShowRowNumber(true),
221+
)
222+
tb.SetHeads("Package", "Status")
223+
tb.AddRow("cliui", "ready")
224+
tb.AddRow("docs", "updated")
225+
tb.Println()
226+
```
227+
228+
内置样式包括 `StyleSimple``StyleMySql``StyleMarkdown``StyleBold``StyleBoldBorder``StyleRounded``StyleDouble``StyleMinimal`
229+
230+
长文本可以配置列宽和溢出策略:
231+
232+
```go
233+
tb := table.New("Tasks")
234+
tb.SetHeads("Task", "Description")
235+
tb.AddRow("download", "Fetch archive from mirror and verify checksum")
236+
tb.AddRow("extract", "Unpack files into the selected destination")
237+
tb.WithOptions(
238+
table.WithColumnWidths(12, 32),
239+
table.WithColMaxWidth(32),
240+
table.WithOverflowFlag(table.OverflowWrap),
241+
)
242+
tb.Println()
243+
```
244+
245+
`OverflowCut` 会截断过长内容,`OverflowWrap` 会把内容换行显示。需要按列排序时,可以使用 `WithSortColumn(index, ascending)`
246+
247+
```go
248+
tb := table.New("Results")
249+
tb.SetHeads("Name", "Status")
250+
tb.AddRow("test", "failed")
251+
tb.AddRow("build", "ok")
252+
tb.WithOptions(table.WithSortColumn(0, true))
253+
tb.Println()
254+
```
255+
181256
### JSON
182257

183258
`JSON` 用于格式化输出结构化对象,适合调试、展示 API 响应或输出机器可读的结果。

0 commit comments

Comments
 (0)