Skip to content

Commit eae5a3f

Browse files
Merge 57a1d4a into master
2 parents 4915bf5 + 57a1d4a commit eae5a3f

File tree

6 files changed

+272
-39
lines changed

6 files changed

+272
-39
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 0.6.2
1+
# 0.6.3
22

33
- Added `rowDividers` argument
44

README.md

+26-26
Original file line numberDiff line numberDiff line change
@@ -102,32 +102,6 @@ Autumn | 11 | November | 30 | 2302
102102
Winter | 12 | December | 31 | 258
103103
```
104104

105-
## Add vertical border
106-
107-
``` dart
108-
tabular(data, border: Border.vertical);
109-
```
110-
111-
``` text
112-
| Season | # | Name | Days | Sun |
113-
|--------|----|-----------|------|-------|
114-
| Winter | 1 | January | 31 | 94 |
115-
| Winter | 2 | February | 28 | 123 |
116-
| Spring | 3 | March | 31 | 42 |
117-
| Spring | 4 | April | 30 | 243 |
118-
| Spring | 5 | May | 31 | 5523 |
119-
| Summer | 6 | June | 30 | 11251 |
120-
| Summer | 7 | July | 31 | 17451 |
121-
| Summer | 8 | August | 31 | 18707 |
122-
| Autumn | 9 | September | 30 | 7025 |
123-
| Autumn | 10 | October | 31 | 5041 |
124-
| Autumn | 11 | November | 30 | 2302 |
125-
| Winter | 12 | December | 31 | 258 |
126-
```
127-
128-
In the same way, you can add `Border.horizontal`, but it can make the table not
129-
conform to the Markdown standard.
130-
131105
## Add horizontal dividers
132106

133107
``` dart
@@ -158,6 +132,32 @@ Winter | 12 | December | 31 | 258
158132
Please be aware that dividers can make the table not conform to the
159133
Markdown standard.
160134

135+
## Add outer border
136+
137+
``` dart
138+
tabular(data, border: Border.vertical);
139+
```
140+
141+
``` text
142+
| Season | # | Name | Days | Sun |
143+
|--------|----|-----------|------|-------|
144+
| Winter | 1 | January | 31 | 94 |
145+
| Winter | 2 | February | 28 | 123 |
146+
| Spring | 3 | March | 31 | 42 |
147+
| Spring | 4 | April | 30 | 243 |
148+
| Spring | 5 | May | 31 | 5523 |
149+
| Summer | 6 | June | 30 | 11251 |
150+
| Summer | 7 | July | 31 | 17451 |
151+
| Summer | 8 | August | 31 | 18707 |
152+
| Autumn | 9 | September | 30 | 7025 |
153+
| Autumn | 10 | October | 31 | 5041 |
154+
| Autumn | 11 | November | 30 | 2302 |
155+
| Winter | 12 | December | 31 | 258 |
156+
```
157+
158+
In the same way, you can add `Border.horizontal` or `Border.all`. But it can
159+
make the table not conform to the Markdown standard.
160+
161161
## Switch border style
162162

163163
``` dart

lib/src/inner.dart

+44-10
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ class Cell implements Comparable<Cell> {
158158

159159
class CellsColumn {
160160
CellsColumn(this.matrix, this.index) {
161-
if (cells.length <= 1) {
162-
throw ArgumentError.value(cells.length, 'cells.length');
163-
}
161+
// if (cells.length <= 1) {
162+
// throw ArgumentError.value(cells.length, 'cells.length');
163+
// }
164164
}
165165

166166
final int index;
@@ -173,8 +173,15 @@ class CellsColumn {
173173
}
174174

175175
int get textWidth {
176+
177+
int result = this.cells.map<int>((cell) => cell.toFinalString().length).reduce(max);
178+
179+
if (result<=0) {
180+
result = 1;
181+
}
182+
return result;
176183
// todo cache
177-
return this.cells.map<int>((cell) => cell.toFinalString().length).reduce(max);
184+
//return this.cells.map<int>((cell) => cell.toFinalString().length).reduce(max);
178185
}
179186

180187
Side guessAlign() {
@@ -195,10 +202,10 @@ class CellsColumn {
195202

196203
class CellsMatrix {
197204
CellsMatrix(List<List<dynamic>> rawRows, Map<dynamic, FormatCell>? format) {
198-
if (rawRows.length <= 1) {
199-
throw ArgumentError.value(rawRows.length, 'rawRows.length',
200-
'Must contain at least two items: the header and the first row.');
201-
}
205+
// if (rawRows.length <= 1) {
206+
// throw ArgumentError.value(rawRows.length, 'rawRows.length',
207+
// 'Must contain at least two items: the header and the first row.');
208+
// }
202209

203210
// determining the maximum count of cells in each row
204211
final columnsCount = rawRows.map<int>((r) => r.length).reduce(max);
@@ -410,6 +417,11 @@ String tabular(
410417
@Deprecated('Use border=Border.vertical argument') // since 2021-04-28
411418
outerBorder = false
412419
}) {
420+
421+
if (rows.isEmpty) {
422+
throw ArgumentError.value(rows, 'rows', 'Must not be empty');
423+
}
424+
413425
if (outerBorder && border==Border.none) {
414426
border = Border.vertical;
415427
}
@@ -441,28 +453,50 @@ String tabular(
441453
}
442454
for (int i = 0; i < matrix.columns.length; ++i) {
443455
final align = markdownAlign ? colToAlign[i] : null;
444-
final width = matrix.columns[i].textWidth;
456+
int width = matrix.columns[i].textWidth;
445457

458+
// if (width<=0) {
459+
// width = 1;
460+
// }
461+
462+
463+
final bool isSingleColumn = matrix.columns.length==1;
446464
final bool isFirstColumn = i == 0;
447465
final bool isLastColumn = i == matrix.columns.length - 1;
448466

449-
int extra = ((isFirstColumn || isLastColumn) && !borderV) ? -1 : 0;
467+
int extra = 0;
468+
if (!borderV) {
469+
if (isSingleColumn) {
470+
extra = -2;
471+
}
472+
else if (isFirstColumn || isLastColumn) {
473+
extra = -1;
474+
}
475+
}
476+
477+
//int extra = ((isFirstColumn || isLastColumn) && !borderV) ? -1 : 0;
478+
450479

451480
switch (align) {
452481
case null:
453482
case Side.start:
483+
//print("A $width $extra");
454484
bar += ('-' * (width + 2 + extra));
455485
break;
456486
case Side.end:
487+
//print("B");
457488
bar += ('-' * (width + 1 + extra) + ':');
458489
break;
459490
case Side.center:
491+
//print("C");
460492
bar += (':' + '-' * width + ':');
461493
}
462494

463495
if (borderV || !isLastColumn) {
464496
bar += cross;
465497
}
498+
499+
//print("width $width $bar");
466500
}
467501

468502
String dashBar() => bar; // '+'+('-'*(bar.length-2))+'+';

pubspec.yaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
name: tabular
22
description: Dart library for displaying tabular data in a visually appealing ASCII table format.
3-
version: 0.6.2
3+
version: 0.6.3
44
repository: https://github.com/rtmigo/tabular_dart
55

6+
67
environment:
78
sdk: ">=2.12.0 <3.0.0"
89

test/no_data_test.dart

+198
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
// SPDX-FileCopyrightText: (c) 2021 Artёm IG <github.com/rtmigo>
2+
// SPDX-License-Identifier: MIT
3+
4+
import 'package:tabular/tabular.dart';
5+
import 'package:test/test.dart';
6+
7+
import 'common.dart';
8+
9+
void main() {
10+
test('2x2', () {
11+
var t = tabular([
12+
['header', 'other'],
13+
['a', 'b']
14+
]);
15+
16+
//print(t.spaceToDot());
17+
18+
expect(
19+
t.spaceToDot(),
20+
testTrim('''
21+
header.|.other
22+
-------|------
23+
a......|.b....
24+
'''
25+
.trim()
26+
.noSpaces()));
27+
});
28+
29+
test('1x2 long header', () {
30+
var t = tabular([
31+
['header'],
32+
['a']
33+
]);
34+
35+
//print(t.spaceToDot());
36+
37+
expect(
38+
t.spaceToDot(),
39+
testTrim('''
40+
header
41+
------
42+
a.....
43+
'''
44+
.trim()
45+
.noSpaces()));
46+
});
47+
48+
test('1x2 long header + vertical border', () {
49+
var t = tabular([
50+
['header'],
51+
['a']
52+
], border: Border.vertical);
53+
54+
//print(t.spaceToDot());
55+
56+
expect(
57+
t.spaceToDot(),
58+
testTrim('''
59+
|.header.|
60+
|--------|
61+
|.a......|
62+
'''
63+
.trim()
64+
.noSpaces()));
65+
});
66+
67+
test('1x2 long cell', () {
68+
var t = tabular([
69+
['a'],
70+
['cell']
71+
]);
72+
73+
//print(t.spaceToDot());
74+
75+
expect(
76+
t.spaceToDot(),
77+
testTrim('''
78+
a...
79+
----
80+
cell
81+
'''
82+
.trim()
83+
.noSpaces()));
84+
});
85+
86+
test('1x2 cell=null', () {
87+
var t = tabular([
88+
['a'],
89+
[null]
90+
]);
91+
92+
//print(t.spaceToDot());
93+
94+
expect(
95+
t.spaceToDot(),
96+
testTrim('''
97+
a
98+
-
99+
.
100+
'''
101+
.trim()
102+
.noSpaces()));
103+
});
104+
105+
test('1x2, but row empty', () {
106+
var t = tabular([
107+
['a'],
108+
[]
109+
]);
110+
111+
expect(
112+
t.spaceToDot(),
113+
testTrim('''
114+
a
115+
-
116+
.
117+
'''
118+
.trim()
119+
.noSpaces()));
120+
});
121+
122+
test('1x2 both null', () {
123+
var t = tabular([
124+
[null],
125+
[null]
126+
]);
127+
128+
//print(t.spaceToDot());
129+
130+
expect(
131+
t.spaceToDot(),
132+
testTrim('''
133+
.
134+
-
135+
.
136+
'''
137+
.trim()
138+
.noSpaces()));
139+
});
140+
141+
test('1x1', () {
142+
var t = tabular([
143+
['header']
144+
]);
145+
146+
//print(t.spaceToDot());
147+
148+
expect(
149+
t.spaceToDot(),
150+
testTrim('''
151+
header
152+
'''
153+
.trim()
154+
.noSpaces()));
155+
});
156+
157+
test('1x1 borders', () {
158+
var t = tabular([
159+
['header']
160+
], border: Border.all);
161+
162+
//print(t.spaceToDot());
163+
164+
expect(
165+
t.spaceToDot(),
166+
testTrim('''
167+
|--------|
168+
|.header.|
169+
|--------|
170+
'''
171+
.trim()
172+
.noSpaces()));
173+
});
174+
175+
test('1x1 null borders', () {
176+
var t = tabular([
177+
[null]
178+
], border: Border.all);
179+
180+
//print(t.spaceToDot());
181+
182+
expect(
183+
t.spaceToDot(),
184+
testTrim('''
185+
|---|
186+
|...|
187+
|---|
188+
'''
189+
.trim()
190+
.noSpaces()));
191+
});
192+
193+
test('1x0', () {
194+
expect(() => tabular([[]], border: Border.all), throwsArgumentError);
195+
expect(() => tabular([[]]), throwsArgumentError);
196+
expect(() => tabular([]), throwsArgumentError);
197+
});
198+
}

0 commit comments

Comments
 (0)