Skip to content

Commit 852acaa

Browse files
committed
Fix weird table row limit behaviour
If a table row limit is specified that is greater than the actual size of the table, an error is created. This makes sense from a programming point of view, however it is counter intuitive when using the feature as most developers would assume that if the row limit is greater than the row count, only the available content is displayed. Remove `RowLimitExceedsTableSize` error struct and code. Change row limit condition to only set a row limit if it is in the bounds of the table rows. Fix test case to not test for an out of bounds error, but that the table is printed in its entirety even though a high row limit is set.
1 parent ede6868 commit 852acaa

File tree

5 files changed

+16
-30
lines changed

5 files changed

+16
-30
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ go 1.12
55
require (
66
github.com/gonvenience/bunt v1.1.1
77
github.com/gonvenience/wrap v1.1.0
8-
github.com/lucasb-eyer/go-colorful v1.0.2
8+
github.com/lucasb-eyer/go-colorful v1.0.3
99
github.com/onsi/ginkgo v1.10.3
1010
github.com/onsi/gomega v1.7.1
1111
github.com/pkg/errors v0.8.1
12-
gopkg.in/yaml.v2 v2.2.2
12+
gopkg.in/yaml.v2 v2.2.7
1313
)

go.sum

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ github.com/gonvenience/wrap v1.1.0 h1:d8gEZrXS/zg4BC1q0U4nHpPIh5k6muKpQ1+rQFBwpY
99
github.com/gonvenience/wrap v1.1.0/go.mod h1:L47Cm1sK1G8QmFAYQfkHcF/sQ1IBJUa0u4sjqiLqPdM=
1010
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
1111
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
12-
github.com/lucasb-eyer/go-colorful v1.0.2 h1:mCMFu6PgSozg9tDNMMK3g18oJBX7oYGrC09mS6CXfO4=
1312
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
13+
github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tWFlaaUAac=
14+
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
1415
github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3 h1:BXxTozrOU8zgC5dkpn3J6NTRdoP+hjok/e+ACr4Hibk=
1516
github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3/go.mod h1:x1uk6vxTiVuNt6S5R2UYgdhpj3oKojXvOXauHZ7dEnI=
1617
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
@@ -19,13 +20,9 @@ github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936 h1:kw1v0NlnN+GZcU8
1920
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936/go.mod h1:r1VsdOzOPt1ZSrGZWFoNhsAedKnEd6r9Np1+5blZCWk=
2021
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
2122
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
22-
github.com/onsi/ginkgo v1.10.1 h1:q/mM8GF/n0shIN8SaAZ0V+jnLPzen6WIVZdiwrRlMlo=
23-
github.com/onsi/ginkgo v1.10.1/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
2423
github.com/onsi/ginkgo v1.10.3 h1:OoxbjfXVZyod1fmWYhI7SEyaD8B00ynP3T+D5GiyHOY=
2524
github.com/onsi/ginkgo v1.10.3/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
2625
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
27-
github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
28-
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
2926
github.com/onsi/gomega v1.7.1 h1:K0jcRCwNQM3vFGh1ppMtDh/+7ApJrjldlX8fA0jDTLQ=
3027
github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY=
3128
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
@@ -50,7 +47,6 @@ gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMy
5047
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
5148
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
5249
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
53-
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
54-
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
55-
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
5650
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
51+
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
52+
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

table.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,14 +152,10 @@ func Table(table [][]string, tableOptions ...TableOption) (string, error) {
152152
rowLimit int = len(table)
153153
)
154154

155-
if options.rowLimit >= 0 {
155+
if (options.rowLimit >= 0) && (options.rowLimit < len(table)) {
156156
rowLimit = options.rowLimit
157157
}
158158

159-
if rowLimit > len(table) {
160-
return "", &RowLimitExceedsTableSize{Limit: rowLimit, Rows: len(table)}
161-
}
162-
163159
for ; idx < rowLimit; idx++ {
164160
row := table[idx]
165161

table_error.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,3 @@ type ColumnIndexIsOutOfBoundsError struct {
5454
func (e *ColumnIndexIsOutOfBoundsError) Error() string {
5555
return fmt.Sprintf("unable to render table, the provided column index %d is out of bounds", e.ColumnIdx)
5656
}
57-
58-
// RowLimitExceedsTableSize is used to describe that the specified maximum row
59-
// number exceeds the actual number of rows in the table
60-
type RowLimitExceedsTableSize struct {
61-
Limit int
62-
Rows int
63-
}
64-
65-
func (e *RowLimitExceedsTableSize) Error() string {
66-
return fmt.Sprintf("unable to render table, the provided row limit of %d exceeds the number of rows %d", e.Limit, e.Rows)
67-
}

table_test.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,16 +186,21 @@ one two three
186186
Expect(tableString).To(BeEquivalentTo(expectedResult))
187187
})
188188

189-
It("should fail when using an invalid row limit", func() {
189+
It("should not fail when using a row limit which is greater than the table length", func() {
190190
input := [][]string{
191191
{"eins", "zwei", "drei"},
192192
{"one", "two", "three"},
193193
{"un", "deux", "trois"},
194194
}
195195

196-
tableString, err := Table(input, LimitRows(4))
197-
Expect(err).Should(MatchError(&RowLimitExceedsTableSize{4, 3}))
198-
Expect(tableString).To(BeEquivalentTo(""))
196+
expectedResult := `eins zwei drei
197+
one two three
198+
un deux trois
199+
`
200+
201+
tableString, err := Table(input, LimitRows(25))
202+
Expect(err).ToNot(HaveOccurred())
203+
Expect(tableString).To(BeEquivalentTo(expectedResult))
199204
})
200205
})
201206
})

0 commit comments

Comments
 (0)