File: style.go

package info (click to toggle)
golang-github-xo-tblfmt 0.15.1-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 3,560 kB
  • sloc: makefile: 7
file content (149 lines) | stat: -rw-r--r-- 5,422 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
package tblfmt

import (
	"fmt"
	"io"
)

// LineStyle is a table line style.
//
// See the ASCII, OldASCII, and Unicode styles below for predefined table
// styles.
//
// Tables generally look like the following:
//
//	+-----------+---------------------------+---+
//	| author_id |           name            | z |
//	+-----------+---------------------------+---+
//	|        14 | a       b       c       d |   |
//	|        15 | aoeu                     +|   |
//	|           | test                     +|   |
//	|           |                           |   |
//	+-----------+---------------------------+---+
//
// When border is 0, then no surrounding borders will be shown:
//
//	author_id           name            z
//	--------- ------------------------- -
//	       14 a       b       c       d
//	       15 aoeu                     +
//	          test                     +
//
// When border is 1, then a border between columns will be shown:
//
//	 author_id |           name            | z
//	-----------+---------------------------+---
//	        14 | a       b       c       d |
//	        15 | aoeu                     +|
//	           | test                     +|
//	           |                           |
type LineStyle struct {
	Top  [4]rune
	Mid  [4]rune
	Row  [4]rune
	Wrap [4]rune
	End  [4]rune
}

// ASCIILineStyle is the ASCII line style for tables.
//
// Tables using this style will look like the following:
//
//	+-----------+---------------------------+---+
//	| author_id |           name            | z |
//	+-----------+---------------------------+---+
//	|        14 | a       b       c       d |   |
//	|        15 | aoeu                     +|   |
//	|           | test                     +|   |
//	|           |                           |   |
//	+-----------+---------------------------+---+
func ASCIILineStyle() LineStyle {
	return LineStyle{
		// left char sep right
		Top:  [4]rune{'+', '-', '+', '+'},
		Mid:  [4]rune{'+', '-', '+', '+'},
		Row:  [4]rune{'|', ' ', '|', '|'},
		Wrap: [4]rune{'|', '+', '|', '|'},
		End:  [4]rune{'+', '-', '+', '+'},
	}
}

// OldASCIILineStyle is the old ASCII line style for tables.
//
// Tables using this style will look like the following:
//
//	+-----------+---------------------------+---+
//	| author_id |           name            | z |
//	+-----------+---------------------------+---+
//	|        14 | a       b       c       d |   |
//	|        15 | aoeu                      |   |
//	|           : test                          |
//	|           :                               |
//	+-----------+---------------------------+---+
func OldASCIILineStyle() LineStyle {
	s := ASCIILineStyle()
	s.Wrap[1], s.Wrap[2] = ' ', ':'
	return s
}

// UnicodeLineStyle is the Unicode line style for tables.
//
// Tables using this style will look like the following:
//
//	┌───────────┬───────────────────────────┬───┐
//	│ author_id │           name            │ z │
//	├───────────┼───────────────────────────┼───┤
//	│        14 │ a       b       c       d │   │
//	│        15 │ aoeu                     ↵│   │
//	│           │ test                     ↵│   │
//	│           │                           │   │
//	└───────────┴───────────────────────────┴───┘
func UnicodeLineStyle() LineStyle {
	return LineStyle{
		// left char sep right
		Top:  [4]rune{'┌', '─', '┬', '┐'},
		Mid:  [4]rune{'├', '─', '┼', '┤'},
		Row:  [4]rune{'│', ' ', '│', '│'},
		Wrap: [4]rune{'│', '↵', '│', '│'},
		End:  [4]rune{'└', '─', '┴', '┘'},
	}
}

// UnicodeDoubleLineStyle is the Unicode double line style for tables.
//
// Tables using this style will look like the following:
//
//	╔═══════════╦═══════════════════════════╦═══╗
//	║ author_id ║           name            ║ z ║
//	╠═══════════╬═══════════════════════════╬═══╣
//	║        14 ║ a       b       c       d ║   ║
//	║        15 ║ aoeu                     ↵║   ║
//	║           ║ test                     ↵║   ║
//	║           ║                           ║   ║
//	╚═══════════╩═══════════════════════════╩═══╝
func UnicodeDoubleLineStyle() LineStyle {
	return LineStyle{
		// left char sep right
		Top:  [4]rune{'╔', '═', '╦', '╗'},
		Mid:  [4]rune{'╠', '═', '╬', '╣'},
		Row:  [4]rune{'║', ' ', '║', '║'},
		Wrap: [4]rune{'║', '↵', '║', '║'},
		End:  [4]rune{'╚', '═', '╩', '╝'},
	}
}

// DefaultTableSummary is the default table summary.
//
// Default table summaries look like the following:
//
//	(3 rows)
func DefaultTableSummary() Summary {
	return map[int]func(io.Writer, int) (int, error){
		1: func(w io.Writer, count int) (int, error) {
			return fmt.Fprintf(w, "(%d row)", count)
		},
		-1: func(w io.Writer, count int) (int, error) {
			return fmt.Fprintf(w, "(%d rows)", count)
		},
	}
}