| 1 |
package tblfmt |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"strings" |
| 7 |
"unicode" |
| 8 |
) |
| 9 |
|
| 10 |
// Transformer is the interface for column transformers. |
| 11 |
type Transformer interface { |
| 12 |
Transform(string) string |
| 13 |
} |
| 14 |
|
| 15 |
// TransformStyle is a column/header transform style. |
| 16 |
type TransformStyle int |
| 17 |
|
| 18 |
// Transform styles. |
| 19 |
const ( |
| 20 |
TransformNone TransformStyle = iota |
| 21 |
TransformForceLower |
| 22 |
TransformForceUpper |
| 23 |
TransformUpperToLower |
| 24 |
TransformLowerToUpper |
| 25 |
) |
| 26 |
|
| 27 |
// Transform transforms style. Satisifies the [Transformer] interface. |
| 28 |
func (style TransformStyle) Transform(s string) string { |
| 29 |
switch style { |
| 30 |
case TransformForceLower: |
| 31 |
return strings.ToLower(s) |
| 32 |
case TransformForceUpper: |
| 33 |
return strings.ToUpper(s) |
| 34 |
case TransformUpperToLower: |
| 35 |
if j := strings.IndexFunc(s, func(r rune) bool { |
| 36 |
return unicode.IsLetter(r) && unicode.IsLower(r) |
| 37 |
}); j == -1 { |
| 38 |
return strings.ToLower(s) |
| 39 |
} |
| 40 |
case TransformLowerToUpper: |
| 41 |
if j := strings.IndexFunc(s, func(r rune) bool { |
| 42 |
return unicode.IsLetter(r) && unicode.IsUpper(r) |
| 43 |
}); j == -1 { |
| 44 |
return strings.ToUpper(s) |
| 45 |
} |
| 46 |
} |
| 47 |
return s |
| 48 |
} |
| 49 |
|
| 50 |
// LineStyle is a table line style. |
| 51 |
// |
| 52 |
// See the ASCII, OldASCII, and Unicode styles below for predefined table |
| 53 |
// styles. |
| 54 |
// |
| 55 |
// Tables generally look like the following: |
| 56 |
// |
| 57 |
// +-----------+---------------------------+---+ |
| 58 |
// | author_id | name | z | |
| 59 |
// +-----------+---------------------------+---+ |
| 60 |
// | 14 | a b c d | | |
| 61 |
// | 15 | aoeu +| | |
| 62 |
// | | test +| | |
| 63 |
// | | | | |
| 64 |
// +-----------+---------------------------+---+ |
| 65 |
// |
| 66 |
// When border is 0, then no surrounding borders will be shown: |
| 67 |
// |
| 68 |
// author_id name z |
| 69 |
// --------- ------------------------- - |
| 70 |
// 14 a b c d |
| 71 |
// 15 aoeu + |
| 72 |
// test + |
| 73 |
// |
| 74 |
// When border is 1, then a border between columns will be shown: |
| 75 |
// |
| 76 |
// author_id | name | z |
| 77 |
// -----------+---------------------------+--- |
| 78 |
// 14 | a b c d | |
| 79 |
// 15 | aoeu +| |
| 80 |
// | test +| |
| 81 |
// | | |
| 82 |
type LineStyle struct { |
| 83 |
Top [4]rune |
| 84 |
Mid [4]rune |
| 85 |
Row [4]rune |
| 86 |
Wrap [4]rune |
| 87 |
End [4]rune |
| 88 |
} |
| 89 |
|
| 90 |
// TableLineStyle is the table line style for tables. |
| 91 |
// |
| 92 |
// Tables using this style will look like the following: |
| 93 |
// |
| 94 |
// AUTHOR_ID NAME Z |
| 95 |
// 14 a b c d |
| 96 |
// 15 aoeu |
| 97 |
// test |
| 98 |
func TableLineStyle() LineStyle { |
| 99 |
return LineStyle{ |
| 100 |
// left char sep right |
| 101 |
Top: [4]rune{0, 0, 0, 0}, |
| 102 |
Mid: [4]rune{0, 0, 0, 0}, |
| 103 |
Row: [4]rune{0, ' ', 0, 0}, |
| 104 |
Wrap: [4]rune{0, ' ', 0, 0}, |
| 105 |
End: [4]rune{0, 0, 0, 0}, |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// ASCIILineStyle is the ASCII line style for tables. |
| 110 |
// |
| 111 |
// Tables using this style will look like the following: |
| 112 |
// |
| 113 |
// +-----------+---------------------------+---+ |
| 114 |
// | author_id | name | z | |
| 115 |
// +-----------+---------------------------+---+ |
| 116 |
// | 14 | a b c d | | |
| 117 |
// | 15 | aoeu +| | |
| 118 |
// | | test +| | |
| 119 |
// | | | | |
| 120 |
// +-----------+---------------------------+---+ |
| 121 |
func ASCIILineStyle() LineStyle { |
| 122 |
return LineStyle{ |
| 123 |
// left char sep right |
| 124 |
Top: [4]rune{'+', '-', '+', '+'}, |
| 125 |
Mid: [4]rune{'+', '-', '+', '+'}, |
| 126 |
Row: [4]rune{'|', ' ', '|', '|'}, |
| 127 |
Wrap: [4]rune{'|', '+', '|', '|'}, |
| 128 |
End: [4]rune{'+', '-', '+', '+'}, |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
// OldASCIILineStyle is the old ASCII line style for tables. |
| 133 |
// |
| 134 |
// Tables using this style will look like the following: |
| 135 |
// |
| 136 |
// +-----------+---------------------------+---+ |
| 137 |
// | author_id | name | z | |
| 138 |
// +-----------+---------------------------+---+ |
| 139 |
// | 14 | a b c d | | |
| 140 |
// | 15 | aoeu | | |
| 141 |
// | : test | |
| 142 |
// | : | |
| 143 |
// +-----------+---------------------------+---+ |
| 144 |
func OldASCIILineStyle() LineStyle { |
| 145 |
s := ASCIILineStyle() |
| 146 |
s.Wrap[1], s.Wrap[2] = ' ', ':' |
| 147 |
return s |
| 148 |
} |
| 149 |
|
| 150 |
// UnicodeLineStyle is the Unicode line style for tables. |
| 151 |
// |
| 152 |
// Tables using this style will look like the following: |
| 153 |
// |
| 154 |
// ┌───────────┬───────────────────────────┬───┐ |
| 155 |
// │ author_id │ name │ z │ |
| 156 |
// ├───────────┼───────────────────────────┼───┤ |
| 157 |
// │ 14 │ a b c d │ │ |
| 158 |
// │ 15 │ aoeu ↵│ │ |
| 159 |
// │ │ test ↵│ │ |
| 160 |
// │ │ │ │ |
| 161 |
// └───────────┴───────────────────────────┴───┘ |
| 162 |
func UnicodeLineStyle() LineStyle { |
| 163 |
return LineStyle{ |
| 164 |
// left char sep right |
| 165 |
Top: [4]rune{'┌', '─', '┬', '┐'}, |
| 166 |
Mid: [4]rune{'├', '─', '┼', '┤'}, |
| 167 |
Row: [4]rune{'│', ' ', '│', '│'}, |
| 168 |
Wrap: [4]rune{'│', '↵', '│', '│'}, |
| 169 |
End: [4]rune{'└', '─', '┴', '┘'}, |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
// UnicodeDoubleLineStyle is the Unicode double line style for tables. |
| 174 |
// |
| 175 |
// Tables using this style will look like the following: |
| 176 |
// |
| 177 |
// ╔═══════════╦═══════════════════════════╦═══╗ |
| 178 |
// ║ author_id ║ name ║ z ║ |
| 179 |
// ╠═══════════╬═══════════════════════════╬═══╣ |
| 180 |
// ║ 14 ║ a b c d ║ ║ |
| 181 |
// ║ 15 ║ aoeu ↵║ ║ |
| 182 |
// ║ ║ test ↵║ ║ |
| 183 |
// ║ ║ ║ ║ |
| 184 |
// ╚═══════════╩═══════════════════════════╩═══╝ |
| 185 |
func UnicodeDoubleLineStyle() LineStyle { |
| 186 |
return LineStyle{ |
| 187 |
// left char sep right |
| 188 |
Top: [4]rune{'╔', '═', '╦', '╗'}, |
| 189 |
Mid: [4]rune{'╠', '═', '╬', '╣'}, |
| 190 |
Row: [4]rune{'║', ' ', '║', '║'}, |
| 191 |
Wrap: [4]rune{'║', '↵', '║', '║'}, |
| 192 |
End: [4]rune{'╚', '═', '╩', '╝'}, |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
// DefaultTableSummary is the default table summary. |
| 197 |
// |
| 198 |
// Default table summaries look like the following: |
| 199 |
// |
| 200 |
// (3 rows) |
| 201 |
func DefaultTableSummary() Summary { |
| 202 |
return map[int]func(io.Writer, int) (int, error){ |
| 203 |
1: func(w io.Writer, count int) (int, error) { |
| 204 |
return fmt.Fprintf(w, "(%d row)", count) |
| 205 |
}, |
| 206 |
-1: func(w io.Writer, count int) (int, error) { |
| 207 |
return fmt.Fprintf(w, "(%d rows)", count) |
| 208 |
}, |
| 209 |
} |
| 210 |
} |