From f540b537e32798c5d820e58acb0b44d6cd137f93 Mon Sep 17 00:00:00 2001 From: rtmigo Date: Sat, 20 Mar 2021 20:29:12 +0300 Subject: [PATCH] prepared 0.1.0 --- CHANGELOG.md | 3 +++ LICENSE | 21 ++++++++++++++++++ README.md | 8 +++---- example/example.dart | 36 ++++++++++++++++++++++++++++++ lib/src/inner.dart | 4 ++-- pubpub.sh | 32 +++++++++++++++++++++++++++ pubspec.yaml | 6 ++--- test/common.dart | 14 ++++++++++++ test/samples.dart | 50 ------------------------------------------ test/sorting_test.dart | 10 ++++----- 10 files changed, 119 insertions(+), 65 deletions(-) create mode 100644 CHANGELOG.md create mode 100644 LICENSE create mode 100644 example/example.dart create mode 100755 pubpub.sh delete mode 100644 test/samples.dart diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..0b7b834 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +# 0.1.0-alpha + +- Initial release \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..1e3bc37 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Art Galkin + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index a46d85e..1ca602b 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,8 @@ Dart library for easily displaying tabular data in a visually appealing ASCII table format. -Tabular is specifically designed to create tables in the Markdown format that Github understands. +Tabular is specifically designed to create tables in the Markdown format that +GitHub understands. The library is inspired by python's [tabulate](https://pypi.org/project/tabulate/) and [pretty_table](https://pypi.org/project/prettytable/). @@ -57,9 +58,6 @@ Winter | 12 | December | 31 | 258 ``` - - - # Formatting ### Add border @@ -135,7 +133,7 @@ Winter | 12 | December | 31 | 258 ### Sort by 'Days' descending, and then by 'Sun' ascending ``` dart -tabular(data, sort: [Sort('Days', false), Sort('Sun')]) +tabular(data, sort: [Sort('Days', ascending: false), Sort('Sun')]) ``` ``` text diff --git a/example/example.dart b/example/example.dart new file mode 100644 index 0000000..25c12ba --- /dev/null +++ b/example/example.dart @@ -0,0 +1,36 @@ +import 'package:tabular/tabular.dart'; + +void main() { + var data = [ + ['Season', '#', 'Name', 'Days', 'Sun'], + ['Winter', 1, 'January', 31, 94], + ['Winter', 2, 'February', 28, 123], + ['Spring', 3, 'March', 31, 42], + ['Spring', 4, 'April', 30, 243], + ['Spring', 5, 'May', 31, 5523], + ['Summer', 6, 'June', 30, 11251], + ['Summer', 7, 'July', 31, 17451], + ['Summer', 8, 'August', 31, 18707], + ['Autumn', 9, 'September', 30, 7025], + ['Autumn', 10, 'October', 31, 5041], + ['Autumn', 11, 'November', 30, 2302], + ['Winter', 12, 'December', 31, 258], + ]; + + String title(s) => '\n\n$s\n'; + + print(title('JUST TABLE')); + print(tabular(data)); + + print(title('WITH BORDER')); + print(tabular(data, outerBorder: true)); + + print(title('WITH MARKDOWN ":"')); + print(tabular(data, markdownAlign: true)); + + print(title('SORTED BY FIRST COLUMN')); + print(tabular(data, sort: [Sort(0)])); + + print(title('SORTED BY TWO COLUMNS "DAYS" AND "SUN"')); + print(tabular(data, sort: [Sort('Days', ascending: false), Sort('Sun')])); +} diff --git a/lib/src/inner.dart b/lib/src/inner.dart index 9f29242..491f96d 100644 --- a/lib/src/inner.dart +++ b/lib/src/inner.dart @@ -15,7 +15,7 @@ class Align { } class Sort { - Sort(this.column, [this.ascending = true]); + Sort(this.column, {this.ascending = true}); dynamic column; bool ascending; @@ -275,7 +275,7 @@ Iterable enumerateColumn(List> rows, int colIndex) sync* } } -/// @param sorting Determines the sorting order. +/// @param sort Determines the sorting order. String tabular(List> rows, {List? headerAlign, List? rowAlign, diff --git a/pubpub.sh b/pubpub.sh new file mode 100755 index 0000000..5bafabd --- /dev/null +++ b/pubpub.sh @@ -0,0 +1,32 @@ +#!/bin/bash +set -e && cd "${0%/*}" + +# creates a copy of the project in temporary directory +# and prepares the copy to be published + +temp_pub_dir=$(mktemp -d -t pub-XXXXXXX) + +echo "$temp_pub_dir" + +rsync -Rrv ./ "$temp_pub_dir" \ + --exclude=".git" \ + --exclude="pubpub.sh" \ + --exclude="todo.txt" \ + --exclude="benchmark/" \ + --exclude="reference/" \ + --exclude="README.md" \ + --exclude=".github" \ + --exclude="labuda/" \ + --exclude="draft/" \ + --exclude="experiments" + +# removing everything before "\n# ", the first header +old_readme=$(cat README.md | tr '\n' '\r') +new_readme=$(echo $old_readme | perl -p0e 's|^.*?\r# |# \1|' | tr '\r' '\n') +echo "$new_readme" > "$temp_pub_dir/README.md" + +cd "$temp_pub_dir" +dartfmt -w . +dart pub publish --dry-run +#dart pub publish +#open "$temp_pub_dir" \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index 2ed5af0..4d9a7f9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,7 +1,7 @@ name: tabular -description: Library for table formatting. -# version: 1.0.0 -# homepage: https://www.example.com +description: Dart library for easily displaying tabular data in a visually appealing ASCII table format. +version: 0.1.0-alpha +repository: https://github.com/rtmigo/tabular environment: sdk: ">=2.12.0 <3.0.0" diff --git a/test/common.dart b/test/common.dart index ecdb1ba..ead3bed 100644 --- a/test/common.dart +++ b/test/common.dart @@ -1,3 +1,6 @@ +// SPDX-FileCopyrightText: (c) 2021 Art Galkin +// SPDX-License-Identifier: MIT + String trimLR(String s, {onlyRight = false}) { final strippedRows = s.split('\n').map((s) => onlyRight ? s.trimRight() : s.trim()).toList(); while (strippedRows.isNotEmpty && strippedRows.first.isEmpty) { @@ -15,6 +18,17 @@ String trimR(String s) { return trimLR(s, onlyRight: true); } +final zdata = [ + ['Continent', 'Country', 'Islands', 'Population', 'Per island'], + ['Europe', 'Norway', 55000, 5421241, 98.6], + ['North America', 'Canada', 52455, 37742154, 719.5], + ['Asia', 'Indonesia', 17508, 273523615, 15622.8], + ['Europe', 'Finland', 188000, 5540720, 29.5], + ['Asia', 'Japan', 6853, 126476461, 18455.6], + ['Europe', 'Sweden', 221800, 10099265, 45.5], +]; + + final months = [ ['Winter', 'Spring', 'Summer', 'Autumn'], ['December', 'March', 'June', 'September'], diff --git a/test/samples.dart b/test/samples.dart deleted file mode 100644 index 7adeead..0000000 --- a/test/samples.dart +++ /dev/null @@ -1,50 +0,0 @@ -import 'package:tabular/tabular.dart'; - -void main() { - final zdata = [ - ['Continent', 'Country', 'Islands', 'Population', 'Per island'], - ['Europe', 'Norway', 55000, 5421241, 98.6], - ['North America', 'Canada', 52455, 37742154, 719.5], - ['Asia', 'Indonesia', 17508, 273523615, 15622.8], - ['Europe', 'Finland', 188000, 5540720, 29.5], - ['Asia', 'Japan', 6853, 126476461, 18455.6], - ['Europe', 'Sweden', 221800, 10099265, 45.5], - ]; - - var data = [ - ['Season', '#', 'Name', 'Days', 'Sun'], - ['Winter', 1, 'January', 31, 94], - ['Winter', 2, 'February', 28, 123], - ['Spring', 3, 'March', 31, 42], - ['Spring', 4, 'April', 30, 243], - ['Spring', 5, 'May', 31, 5523], - ['Summer', 6, 'June', 30, 11251], - ['Summer', 7, 'July', 31, 17451], - ['Summer', 8, 'August', 31, 18707], - ['Autumn', 9, 'September', 30, 7025], - ['Autumn', 10, 'October', 31, 5041], - ['Autumn', 11, 'November', 30, 2302], - ['Winter', 12, 'December', 31, 258], - ]; - - print(tabular(data)); - print(''); - - print(tabular(data, markdownAlign: true)); - print(''); - print(tabular(data, sort: [Sort(0)])); - print(''); - print(tabular(data, sort: [Sort('Days', false), Sort('Sun')])); - print(''); - - print(tabular(data, outerBorder: true)); - - // print(''); - // - // print(tabular(data, sort: [Sort('Per island')])); - // print(''); - // print(tabular(data, sort: [Sort('Per island', false)])); - // print(''); - // print(tabular(data, sort: [Sort('Continent'), Sort('Country')])); - -} diff --git a/test/sorting_test.dart b/test/sorting_test.dart index ad9b07e..2cfef8b 100644 --- a/test/sorting_test.dart +++ b/test/sorting_test.dart @@ -23,7 +23,7 @@ void main() { }); test('by indices [1, 0] [asc, asc]', () { - final t = tabular(numbers, sort: [Sort(1, true), Sort(0, true)], outerBorder: true); + final t = tabular(numbers, sort: [Sort(1), Sort(0)], outerBorder: true); //print(t); expect(t, trimLR(''' | First | Second | Third | @@ -37,7 +37,7 @@ void main() { }); test('by indices [0, 1] [asc, asc]', () { - final t = tabular(numbers, sort: [Sort(0, true), Sort(1, true)], outerBorder: true); + final t = tabular(numbers, sort: [Sort(0), Sort(1)], outerBorder: true); //print(t); expect(t, trimLR(''' | First | Second | Third | @@ -51,7 +51,7 @@ void main() { }); test('by two indices, asc-desc', () { - final t = tabular(numbers, sort: [Sort(1, false), Sort(0, true)], outerBorder: true); + final t = tabular(numbers, sort: [Sort(1, ascending: false), Sort(0)], outerBorder: true); expect(t, trimLR(''' | First | Second | Third | |-------|--------|-------| @@ -64,7 +64,7 @@ void main() { }); test('by name, asc-desc', () { - final t = tabular(numbers, sort: [Sort('Second', false), Sort('First', true)], outerBorder: true); + final t = tabular(numbers, sort: [Sort('Second', ascending: false), Sort('First')], outerBorder: true); //print(t); expect(t, trimLR(''' | First | Second | Third | @@ -78,7 +78,7 @@ void main() { }); test('by name, asc-desc', () { - final t = tabular(numbers, sort: [Sort('Second', false), Sort('First', true)], outerBorder: true); + final t = tabular(numbers, sort: [Sort('Second', ascending: false), Sort('First')], outerBorder: true); //print(t); expect(t, trimLR(''' | First | Second | Third |