File: README.md

package info (click to toggle)
golang-github-shenwei356-stable 0.1.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 176 kB
  • sloc: makefile: 2
file content (361 lines) | stat: -rwxr-xr-x 21,437 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# stable - streaming pretty text table

[![Go Reference](https://pkg.go.dev/badge/github.com/shenwei356/stable.svg)](https://pkg.go.dev/github.com/shenwei356/stable)

If you just need an executable tool to format tables, please use `csvtk pretty`
([code](https://github.com/shenwei356/csvtk), [usage and example](https://bioinf.shenwei.me/csvtk/usage/#pretty)),
which uses this package.

Table of Contents

* [Features](#features)
* [Install](#install)
* [Examples](#examples)
* [Styles](#styles)
* [Support](#support)
* [License](#license)
* [Alternate packages](#alternate-packages)

## Features

- **Supporting streaming output** (optional).

  When a writer is configured, a newly added row is formatted and written to the writer immediately.
  It is memory-effective for a large number of rows.
  And it is helpful to pipe the data in shell.

- **Supporting wrapping text or clipping text**.

  The minimum and maximum width of the column can be configured for each column or globally.

- **Configured table styles**.

  Some [preset styles](#styles) are also provided.

- **Unicode supported**


Not-supported features:
- Row/column span
- Colorful text

## Install

    go get -u github.com/shenwei356/table

## Examples

**Note that the output is well-formatted in the terminal.
However, rows containing Unicode are not displayed appropriately in text editors and browsers.**

1. Basic usages.

        tbl := New().HumanizeNumbers().MaxWidth(40)

        tbl.Header([]string{
            "id",
            "name",
            "sentence",
        })
        tbl.AddRow([]interface{}{100, "Donec Vitae", "Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse."})
        tbl.AddRow([]interface{}{2000, "Quaerat Voluptatem", "At vero eos et accusamus et iusto odio."})
        tbl.AddRow([]interface{}{3000000, "Aliquam lorem", "Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero."})

        fmt.Printf("%s\n", tbl.Render(StyleGrid))

        +-----------+--------------------+------------------------------------------+
        | id        | name               | sentence                                 |
        +===========+====================+==========================================+
        | 100       | Donec Vitae        | Quis autem vel eum iure reprehenderit    |
        |           |                    | qui in ea voluptate velit esse.          |
        +-----------+--------------------+------------------------------------------+
        | 2,000     | Quaerat Voluptatem | At vero eos et accusamus et iusto odio.  |
        +-----------+--------------------+------------------------------------------+
        | 3,000,000 | Aliquam lorem      | Curabitur ullamcorper ultricies nisi.    |
        |           |                    | Nam eget dui. Etiam rhoncus. Maecenas    |
        |           |                    | tempus, tellus eget condimentum          |
        |           |                    | rhoncus, sem quam semper libero.         |
        +-----------+--------------------+------------------------------------------+

1. Unicode.

        tbl := New().HumanizeNumbers().MaxWidth(20) //.ClipCell("...")

        tbl.Header([]string{
            "id",
            "name",
            "sentence",
        })
        tbl.AddRow([]interface{}{100, "Wei Shen", "How are you?"})
        tbl.AddRow([]interface{}{1000, "沈 伟", "I'm fine, thank you. And you?"})
        tbl.AddRow([]interface{}{100000, "沈伟", "谢谢,我很好,你呢?"})

        fmt.Printf("%s\n", tbl.Render(StyleGrid))

        style: grid
        +---------+----------+----------------------+
        | id      | name     | sentence             |
        +=========+==========+======================+
        | 100     | Wei Shen | How are you?         |
        +---------+----------+----------------------+
        | 1,000   | 沈 伟    | I'm fine, thank      |
        |         |          | you. And you?        |
        +---------+----------+----------------------+
        | 100,000 | 沈伟     | 谢谢,我很好         |
        |         |          | ,你呢?             |
        +---------+----------+----------------------+

     ![](screenshot1.png)

        // clipping text instead of wrapping

        fmt.Printf("%s\n", tbl.ClipCell("...").Render(StyleGrid))

        +---------+----------+----------------------+
        | id      | name     | sentence             |
        +=========+==========+======================+
        | 100     | Wei Shen | How are you?         |
        +---------+----------+----------------------+
        | 1,000   | 沈 伟    | I'm fine, thank y... |
        +---------+----------+----------------------+
        | 100,000 | 沈伟     | 谢谢,我很好,你呢? |
        +---------+----------+----------------------+

    ![](screenshot2.png)


1. Custom columns format.

        tbl := New()

        tbl.HeaderWithFormat([]Column{
            {Header: "number", MinWidth: 5, MaxWidth: 10, HumanizeNumbers: true, Align: AlignRight},
            {Header: "name", MinWidth: 10, MaxWidth: 16, Align: AlignCenter},
            {Header: "sentence", MaxWidth: 40, Align: AlignLeft},
        })
        tbl.AddRow([]interface{}{100, "Donec Vitae", "Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse."})
        tbl.AddRow([]interface{}{2000, "Quaerat Voluptatem", "At vero eos et accusamus et iusto odio."})
        tbl.AddRow([]interface{}{3000000, "Aliquam lorem", "Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero."})

        fmt.Printf("%s\n", tbl.Render(StyleGrid))

        +------------+-----------------+------------------------------------------+
        |     number |      name       | sentence                                 |
        +============+=================+==========================================+
        |        100 |   Donec Vitae   | Quis autem vel eum iure reprehenderit    |
        |            |                 | qui in ea voluptate velit esse.          |
        +------------+-----------------+------------------------------------------+
        |      2,000 |    Quaerat      | At vero eos et accusamus et iusto odio.  |
        |            |   Voluptatem    |                                          |
        +------------+-----------------+------------------------------------------+
        |  3,000,000 |  Aliquam lorem  | Curabitur ullamcorper ultricies nisi.    |
        |            |                 | Nam eget dui. Etiam rhoncus. Maecenas    |
        |            |                 | tempus, tellus eget condimentum          |
        |            |                 | rhoncus, sem quam semper libero.         |
        +------------+-----------------+------------------------------------------+



1. Streaming the output, i.e., a newly added row is formatted and written to the configured writer immediately.

        tbl := New().MinWidth(10)

        // write to stdout, and determine the max width according to the first row
        tbl.Writer(os.Stdout, 1)
        tbl.Style(StyleGrid)

        tbl.Header([]string{
            "number",
            "name",
            "sentence",
        })

        // when a new row is added, it writes to stdout immediately.
        tbl.AddRow([]interface{}{100, "Donec Vitae", "Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse."})
        tbl.AddRow([]interface{}{2000, "Quaerat Voluptatem", "At vero eos et accusamus et iusto odio."})
        tbl.AddRow([]interface{}{3000000, "Aliquam lorem", "Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero."})

        // flush the remaining data
        tbl.Flush()


        +------------+-------------+-----------------------------------------------------------------------+
        | number     | name        | sentence                                                              |
        +============+=============+=======================================================================+
        | 100        | Donec Vitae | Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse. |
        +------------+-------------+-----------------------------------------------------------------------+
        | 2000       | Quaerat     | At vero eos et accusamus et iusto odio.                               |
        |            | Voluptatem  |                                                                       |
        +------------+-------------+-----------------------------------------------------------------------+
        | 3000000    | Aliquam     | Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus.    |
        |            | lorem       | Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper     |
        |            |             | libero.                                                               |
        +------------+-------------+-----------------------------------------------------------------------+


1. Custom delimiter for wrapping text.
  In this example, `complete lineage` contains a list of words joined with `;`.

        tbl := New()

        tbl.SetHeader([]string{
            "taxid",
            "name",
            "complete lineage",
        })
        tbl.AddRow([]interface{}{
            9606,
            "Homo sapiens",
            "cellular organisms;Eukaryota;Opisthokonta;Metazoa;Eumetazoa;Bilateria;Deuterostomia;Chordata;Craniata;Vertebrata;Gnathostomata;Teleostomi;Euteleostomi;Sarcopterygii;Dipnotetrapodomorpha;Tetrapoda;Amniota;Mammalia;Theria;Eutheria;Boreoeutheria;Euarchontoglires;Primates;Haplorrhini;Simiiformes;Catarrhini;Hominoidea;Hominidae;Homininae;Homo;Homo sapiens",
        })
        tbl.AddRow([]interface{}{
            562, "Escherichia coli",
            "cellular organisms;Bacteria;Pseudomonadota;Gammaproteobacteria;Enterobacterales;Enterobacteriaceae;Escherichia;Escherichia coli",
        })

        fmt.Printf("%s\n", tbl.WrapDelimiter(';').AlignLeft().MaxWidth(50).Render(StyleGrid))

        +-------+------------------+----------------------------------------------------+
        | taxid | name             | complete lineage                                   |
        +=======+==================+====================================================+
        | 9606  | Homo sapiens     | cellular organisms;Eukaryota;Opisthokonta;Metazoa; |
        |       |                  | Eumetazoa;Bilateria;Deuterostomia;Chordata;        |
        |       |                  | Craniata;Vertebrata;Gnathostomata;Teleostomi;      |
        |       |                  | Euteleostomi;Sarcopterygii;Dipnotetrapodomorpha;   |
        |       |                  | Tetrapoda;Amniota;Mammalia;Theria;Eutheria;        |
        |       |                  | Boreoeutheria;Euarchontoglires;Primates;           |
        |       |                  | Haplorrhini;Simiiformes;Catarrhini;Hominoidea;     |
        |       |                  | Hominidae;Homininae;Homo;Homo sapiens              |
        +-------+------------------+----------------------------------------------------+
        | 562   | Escherichia coli | cellular organisms;Bacteria;Pseudomonadota;        |
        |       |                  | Gammaproteobacteria;Enterobacterales;              |
        |       |                  | Enterobacteriaceae;Escherichia;Escherichia coli    |
        +-------+------------------+----------------------------------------------------+


## Styles

**Note that the output is well-formatted in the terminal.
However, rows containing Unicode are not displayed appropriately in text editors and browsers.**

    style: plain
    id          name                 sentence
    100         Donec Vitae          Quis autem vel eum iure reprehenderit
                                     qui in ea voluptate velit esse.
    2,000       Quaerat Voluptatem   At vero eos et accusamus et iusto odio.
    3,000,000   Aliquam lorem        Curabitur ullamcorper ultricies nisi.
                                     Nam eget dui. Etiam rhoncus. Maecenas
                                     tempus, tellus eget condimentum
                                     rhoncus, sem quam semper libero.

    style: simple
    ---------------------------------------------------------------------------
    id          name                 sentence
    ---------------------------------------------------------------------------
    100         Donec Vitae          Quis autem vel eum iure reprehenderit
                                    qui in ea voluptate velit esse.
    2,000       Quaerat Voluptatem   At vero eos et accusamus et iusto odio.
    3,000,000   Aliquam lorem        Curabitur ullamcorper ultricies nisi.
                                    Nam eget dui. Etiam rhoncus. Maecenas
                                    tempus, tellus eget condimentum
                                    rhoncus, sem quam semper libero.
    ---------------------------------------------------------------------------


    style: 3line
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    id          name                 sentence
    ---------------------------------------------------------------------------
    100         Donec Vitae          Quis autem vel eum iure reprehenderit
                                    qui in ea voluptate velit esse.
    2,000       Quaerat Voluptatem   At vero eos et accusamus et iusto odio.
    3,000,000   Aliquam lorem        Curabitur ullamcorper ultricies nisi.
                                    Nam eget dui. Etiam rhoncus. Maecenas
                                    tempus, tellus eget condimentum
                                    rhoncus, sem quam semper libero.
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━


    style: grid
    +-----------+--------------------+------------------------------------------+
    | id        | name               | sentence                                 |
    +===========+====================+==========================================+
    | 100       | Donec Vitae        | Quis autem vel eum iure reprehenderit    |
    |           |                    | qui in ea voluptate velit esse.          |
    +-----------+--------------------+------------------------------------------+
    | 2,000     | Quaerat Voluptatem | At vero eos et accusamus et iusto odio.  |
    +-----------+--------------------+------------------------------------------+
    | 3,000,000 | Aliquam lorem      | Curabitur ullamcorper ultricies nisi.    |
    |           |                    | Nam eget dui. Etiam rhoncus. Maecenas    |
    |           |                    | tempus, tellus eget condimentum          |
    |           |                    | rhoncus, sem quam semper libero.         |
    +-----------+--------------------+------------------------------------------+

    style: light
    ┌-----------┬--------------------┬------------------------------------------┐
    | id        | name               | sentence                                 |
    ├===========┼====================┼==========================================┤
    | 100       | Donec Vitae        | Quis autem vel eum iure reprehenderit    |
    |           |                    | qui in ea voluptate velit esse.          |
    ├-----------┼--------------------┼------------------------------------------┤
    | 2,000     | Quaerat Voluptatem | At vero eos et accusamus et iusto odio.  |
    ├-----------┼--------------------┼------------------------------------------┤
    | 3,000,000 | Aliquam lorem      | Curabitur ullamcorper ultricies nisi.    |
    |           |                    | Nam eget dui. Etiam rhoncus. Maecenas    |
    |           |                    | tempus, tellus eget condimentum          |
    |           |                    | rhoncus, sem quam semper libero.         |
    └-----------┴--------------------┴------------------------------------------┘

    style: bold
    ┏━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
    ┃ id        ┃ name               ┃ sentence                                 ┃
    ┣━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
    ┃ 100       ┃ Donec Vitae        ┃ Quis autem vel eum iure reprehenderit    ┃
    ┃           ┃                    ┃ qui in ea voluptate velit esse.          ┃
    ┣━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
    ┃ 2,000     ┃ Quaerat Voluptatem ┃ At vero eos et accusamus et iusto odio.  ┃
    ┣━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━╋━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
    ┃ 3,000,000 ┃ Aliquam lorem      ┃ Curabitur ullamcorper ultricies nisi.    ┃
    ┃           ┃                    ┃ Nam eget dui. Etiam rhoncus. Maecenas    ┃
    ┃           ┃                    ┃ tempus, tellus eget condimentum          ┃
    ┃           ┃                    ┃ rhoncus, sem quam semper libero.         ┃
    ┗━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━┻━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

    style: double
    ╔═══════════╦════════════════════╦══════════════════════════════════════════╗
    ║ id        ║ name               ║ sentence                                 ║
    ╠═══════════╬════════════════════╬══════════════════════════════════════════╣
    ║ 100       ║ Donec Vitae        ║ Quis autem vel eum iure reprehenderit    ║
    ║           ║                    ║ qui in ea voluptate velit esse.          ║
    ╠═══════════╬════════════════════╬══════════════════════════════════════════╣
    ║ 2,000     ║ Quaerat Voluptatem ║ At vero eos et accusamus et iusto odio.  ║
    ╠═══════════╬════════════════════╬══════════════════════════════════════════╣
    ║ 3,000,000 ║ Aliquam lorem      ║ Curabitur ullamcorper ultricies nisi.    ║
    ║           ║                    ║ Nam eget dui. Etiam rhoncus. Maecenas    ║
    ║           ║                    ║ tempus, tellus eget condimentum          ║
    ║           ║                    ║ rhoncus, sem quam semper libero.         ║
    ╚═══════════╩════════════════════╩══════════════════════════════════════════╝


## Support

Please [open an issue](https://github.com/shenwei356/stable/issues) to report bugs,
propose new functions or ask for help.

## License

Copyright (c) 2023, Wei Shen (shenwei356@gmail.com)

[MIT License](https://github.com/shenwei356/stable/blob/master/LICENSE)

## Alternate packages

- [go-prettytable](https://github.com/tatsushid/go-prettytable),
  it does not support wrapping cells and it's not flexible to add rows that the number of columns is dynamic.
- [gotabulate](https://github.com/bndr/gotabulate),
  it supports wrapping cells, but it has to read all data in memory before outputing the result.
  We followed the configuration of table styles from this package.
- [go-pretty](https://github.com/jedib0t/go-pretty),
  it supports wrapping cells, but it has to read all data in memory before outputing the result.
  We used some table styles with minor differences in this package.
- [table](https://github.com/clinaresl/table), it renders colorful texts, and supports Multicolumns and nested tables.