github.com/puellanivis/breton@v0.2.16/lib/display/tables/table.go (about)

     1  // Package tables formats multi-dimensional slices of strings to be formated in formated tables.
     2  //
     3  // Defining a table is relatively easy:
     4  //
     5  //	var t tables.Table
     6  //	t = tables.Append(t, 1, 2, 3, 4)
     7  //	t = tables.Append(t)
     8  //	t = tables.Append(t, "any", "arbitrary", "data", 123)
     9  //	t = tables.Append(t, "a", "b", "c")
    10  //
    11  // tables.Empty will produce:
    12  //
    13  //	1   2         3    4
    14  //	any arbitrary data 123
    15  //	a   b         c
    16  //
    17  // N.B. the last elements of every row will not have any ending whitespace added to match other row lengths
    18  //
    19  // tables.ASCII will produce:
    20  //
    21  //	+-----+-----------+------+-----+
    22  //	| 1   | 2         | 3    | 4   |
    23  //	+-----+-----------+------+-----+
    24  //	| any | arbitrary | data | 123 |
    25  //	| a   | b         | c    |     |
    26  //	+-----+-----------+------+-----+
    27  //
    28  // tables.Unicode will produce:
    29  //
    30  //	┌─────┬───────────┬──────┬─────┐
    31  //	│ 1   │ 2         │ 3    │ 4   │
    32  //	├─────┼───────────┼──────┼─────┤
    33  //	│ any │ arbitrary │ data │ 123 │
    34  //	│ a   │ b         │ c    │     │
    35  //	└─────┴───────────┴──────┴─────┘
    36  //
    37  // tables.HTML will produce:
    38  //
    39  //	<table>
    40  //	<tr><td class="first">1</td><td>2</td><td>3</td><td>4</td></tr>
    41  //	<tr><td class="first">any</td><td>arbitrary</td><td>data</td><td>123</td></tr>
    42  //	<tr><td class="first">a</td><td>b</td><td>c</td><td></td></tr>
    43  //	</table>
    44  package tables
    45  
    46  import (
    47  	"bytes"
    48  	"fmt"
    49  )
    50  
    51  // Table defines a 2 dimensional table intended to display.
    52  type Table [][]string
    53  
    54  // Append places a row of things onto the table, each argument being converted to a string, placed in a column.
    55  func Append(table Table, a ...interface{}) Table {
    56  	var row []string
    57  
    58  	for _, val := range a {
    59  		row = append(row, fmt.Sprint(val))
    60  	}
    61  
    62  	return append(table, row)
    63  }
    64  
    65  // String converts the Table to a string in a buffer, and returns a string there of.
    66  func (t Table) String() string {
    67  	b := new(bytes.Buffer)
    68  
    69  	if err := Default.WriteSimple(b, t); err != nil {
    70  		panic(err)
    71  	}
    72  
    73  	return b.String()
    74  }
    75  
    76  func (t Table) widths(autoscale bool, fn func(string) int) []int {
    77  	if len(t) < 1 {
    78  		return nil
    79  	}
    80  
    81  	if fn == nil {
    82  		fn = func(s string) int {
    83  			return len(s)
    84  		}
    85  	}
    86  
    87  	l := 0
    88  	for _, row := range t {
    89  		if l < len(row) {
    90  			l = len(row)
    91  		}
    92  	}
    93  
    94  	widths := make([]int, l)
    95  
    96  	if !autoscale {
    97  		return widths
    98  	}
    99  
   100  	for _, row := range t {
   101  		for i, col := range row {
   102  			l := fn(col)
   103  
   104  			if widths[i] < l {
   105  				widths[i] = l
   106  			}
   107  		}
   108  	}
   109  
   110  	return widths
   111  }