-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
168 lines (145 loc) · 4.74 KB
/
table.go
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
/*
Fuery, is a small and simple library for querying files using SQL.
Copyright (C) 2016 log₃() <contact@logbase3.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
For more information visit https://logbase3.com/fuery
or send an e-mail to contact@logbase3.com
*/
// Package fuery is a small and simple library for querying files using SQL.
package fuery // import "logbase3.com/fuery"
import (
"bytes"
"container/list"
"fmt"
"io"
"strings"
"unicode/utf8"
)
// Constants for output configuration
const (
separator = " | "
lineSeparator = "-+-"
lineCharacter = '-'
defaultCellFormat = "%%-%ds"
numericCellFormat = "%%%ds"
defaultColumnNameTemplate = "Column %d"
)
type Record struct {
ParentTable *Table
Cells []DataType
}
type Table struct {
names []string
types []Type
Records *list.List
}
func NewTable(dataTypes ...Type) *Table {
names := make([]string, len(dataTypes))
for i := range dataTypes {
names[i] = fmt.Sprintf(defaultColumnNameTemplate, i)
}
table := &Table{names, dataTypes, list.New()}
return table
}
func (t Table) Names() []string {
return t.names
}
func (t Table) Types() []Type {
return t.types
}
func (t *Table) SetNames(names []string) {
if len(names) != len(t.types) {
panic(fmt.Sprintf("Invalid name assignment: Names must be a slice with length %d", len(t.types)))
}
t.names = names
}
func (t Table) Insert(values ...DataType) {
if len(values) > 0 {
cells := make([]DataType, 0, len(t.types))
cells = append(cells, values...)
t.InsertRecords(Record{&t, cells})
}
}
func (t Table) InsertRecords(records ...Record) {
for _, record := range records {
t.Records.PushBack(record)
}
}
func (t Table) maxCellLength() []int {
// Bug(Roberto Lapuente): Should use column names instead of numbers where available
// Initialize slice with the lenght of the column names
lengths := make([]int, 0, len(t.types))
for colNumber := range t.types {
lengths = append(lengths, len(t.Names()[colNumber]))
}
for e := t.Records.Front(); e != nil; e = e.Next() {
for i, cell := range e.Value.(Record).Cells {
colLength := utf8.RuneCountInString(cell.String())
if colLength > lengths[i] {
lengths[i] = colLength
}
}
}
return lengths
}
func (t Table) String() string {
var buff bytes.Buffer
t.Write(&buff)
return buff.String()
}
func (t Table) Write(buff io.Writer) {
// Construct format string with column sizes
formatSlice := make([]string, 0, len(t.types))
separatorFormatSlice := make([]string, 0, len(t.types))
for colName, length := range t.maxCellLength() {
if t.types[colName] == INT {
formatSlice = append(formatSlice, fmt.Sprintf(numericCellFormat, length))
} else if t.types[colName] == TEXT {
formatSlice = append(formatSlice, fmt.Sprintf(defaultCellFormat, length))
}
separatorFormatSlice = append(separatorFormatSlice, fmt.Sprintf(defaultCellFormat, length))
}
separatorFormatString := fmt.Sprintf(" %s ", strings.Join(separatorFormatSlice, separator))
headerFormatString := fmt.Sprintf("-%s-", strings.Join(formatSlice, lineSeparator))
formatString := fmt.Sprintf(" %s ", strings.Join(formatSlice, separator))
// Start appending results
var row []interface{}
// Build header
for colName, length := range t.maxCellLength() {
diff := length - len(t.Names()[colName])
headerFormatGap := fmt.Sprintf(defaultCellFormat, diff/2)
headerFormatGap = fmt.Sprintf(headerFormatGap, "")
row = append(row, fmt.Sprintf("%s%s", headerFormatGap, t.Names()[colName]))
}
buff.Write([]byte(fmt.Sprintf(separatorFormatString, row...)))
buff.Write([]byte("\n"))
// Build header/body separator
row = make([]interface{}, 0, len(t.types))
for _, length := range t.maxCellLength() {
column := make([]byte, length)
for i := 0; i < length; i++ {
column[i] += lineCharacter
}
row = append(row, string(column))
}
buff.Write([]byte(fmt.Sprintf(headerFormatString, row...)))
buff.Write([]byte("\n"))
// Build rows
for e := t.Records.Front(); e != nil; e = e.Next() {
row = make([]interface{}, 0, len(t.types))
for _, cell := range e.Value.(Record).Cells {
row = append(row, cell)
}
buff.Write([]byte(fmt.Sprintf(formatString, row...)))
buff.Write([]byte("\n"))
}
}