pax_global_header00006660000000000000000000000064124072675240014523gustar00rootroot0000000000000052 comment=94ff52dbe671866d4f5cd3463ecdad8ec550e578 gotabulate-1.1.2/000077500000000000000000000000001240726752400136535ustar00rootroot00000000000000gotabulate-1.1.2/.gitignore000066400000000000000000000004171240726752400156450ustar00rootroot00000000000000# Compiled Object files, Static and Dynamic libs (Shared Objects) *.o *.a *.so # Folders _obj _test # Architecture specific extensions/prefixes *.[568vq] [568vq].out *.cgo1.go *.cgo2.c _cgo_defun.c _cgo_gotypes.go _cgo_export.* _testmain.go *.exe *.test go-tabulate gotabulate-1.1.2/.travis.yml000066400000000000000000000001401240726752400157570ustar00rootroot00000000000000language: go go: - 1.2 - 1.3 - tip install: - go get github.com/stretchr/testify/assertgotabulate-1.1.2/AUTHOR000066400000000000000000000000241240726752400145740ustar00rootroot00000000000000Vadim Kravcenko 2014gotabulate-1.1.2/CHANGELOG000066400000000000000000000000001240726752400150530ustar00rootroot00000000000000gotabulate-1.1.2/CONTRIBUTORS000066400000000000000000000001321240726752400155270ustar00rootroot00000000000000# Contributors git log --format='%aN <%aE>' | sort -uf Vadim Kravcenko gotabulate-1.1.2/LICENSE000066400000000000000000000260741240726752400146710ustar00rootroot00000000000000Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright {yyyy} {name of copyright owner} Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. gotabulate-1.1.2/README.md000066400000000000000000000255421240726752400151420ustar00rootroot00000000000000# Gotabulate - Easily tabulate Data [![GoDoc](https://godoc.org/github.com/bndr/gotabulate?status.svg)](https://godoc.org/github.com/bndr/gotabulate) [![Build Status](https://travis-ci.org/bndr/gotabulate.svg?branch=master)](https://travis-ci.org/bndr/gotabulate) ## Summary Go-Tabulate - Generic Go Library for easy tabulation of your data. ## Installation go get github.com/bndr/gotabulate ## Description Supported data types: - 2D Array of Int, Int64, Float64, String, interface{} - Map of String, interface{} (Keys will be used as header) ## Usage ```go // Create Some Fake Rows row_1 := []interface{}{"john", 20, "ready"} row_2 := []interface{}{"bndr", 23, "ready"} // Create an object from 2D interface array t := gotabulate.Create([][]interface{}{row_1, row_2}) // Set the Headers (optional) t.SetHeaders([]string{"age", "status"}) // Set the Empty String (optional) t.SetEmptyString("None") // Set Align (Optional) t.SetAlign("right") // Print the result: grid, or simple fmt.Println(t.Render("grid")) +---------+--------+-----------+ | | age | status | +=========+========+===========+ | john | 20 | ready | +---------+--------+-----------+ | bndr | 23 | ready | +---------+--------+-----------+ ``` ## Example with String ``` // Some Strings string_1 := []string{"TV", "1000$", "Sold"} string_2 := []string{"PC", "50%", "on Hold"} // Create Object tabulate := gotabulate.Create([][]string{string_1, string_2}) // Set Headers tabulate.SetHeaders([]string{"Type", "Cost", "Status"}) // Render fmt.Println(tabulate.Render("simple")) --------- ---------- ------------ Type Cost Status --------- ---------- ------------ TV 1000$ Sold PC 50% on Hold --------- ---------- ------------ ``` ## Example with String Wrapping ```go tabulate := gotabulate.Create([][]string{[]string{"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus laoreet vestibulum pretium. Nulla et ornare elit. Cum sociis natoque penatibus et magnis", "Vivamus laoreet vestibulum pretium. Nulla et ornare elit. Cum sociis natoque penatibus et magnis", "zzLorem ipsum", " test", "test"}, []string{"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus laoreet vestibulum pretium. Nulla et ornare elit. Cum sociis natoque penatibus et magnis", "Vivamus laoreet vestibulum pretium. Nulla et ornare elit. Cum sociis natoque penatibus et magnis", "zzLorem ipsum", " test", "test"}, STRING_ARRAY, []string{"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus laoreet vestibulum pretium. Nulla et ornare elit. Cum sociis natoque penatibus et magnis", "Vivamus laoreet vestibulum pretium. Nulla et ornare elit. Cum sociis natoque penatibus et magnis", "zzLorem ipsum", " test", "test"}, STRING_ARRAY}) tabulate.SetHeaders([]string{"Header 1", "header 2", "header 3", "header 4"}) // Set Max Cell Size tabulate.SetMaxCellSize(16) // Turn On String Wrapping tabulate.SetWrapStrings(true) // Render the table fmt.Println(tabulate.Render("grid")) +---------------------+---------------------+----------------+-------------+-------------+ | | Header 1 | header 2 | header 3 | header 4 | +=====================+=====================+================+=============+=============+ | Lorem ipsum dolo | Vivamus laoreet | Lorem ipsum | test | test | | r sit amet, cons | vestibulum preti | | | | | ectetur adipisci | um. Nulla et orn | | | | | ng elit. Vivamus | are elit. Cum so | | | | | laoreet vestibu | ciis natoque pen | | | | | lum pretium. Nul | atibus et magnis | | | | | la et ornare eli | | | | | | t. Cum sociis na | | | | | | toque penatibus | | | | | | et magnis | | | | | +---------------------+---------------------+----------------+-------------+-------------+ | Lorem ipsum dolo | Vivamus laoreet | Lorem ipsum | test | test | | r sit amet, cons | vestibulum preti | | | | | ectetur adipisci | um. Nulla et orn | | | | | ng elit. Vivamus | are elit. Cum so | | | | | laoreet vestibu | ciis natoque pen | | | | | lum pretium. Nul | atibus et magnis | | | | | la et ornare eli | | | | | | t. Cum sociis na | | | | | | toque penatibus | | | | | | et magnis | | | | | +---------------------+---------------------+----------------+-------------+-------------+ | test string | test string 2 | test | row | bndr | +---------------------+---------------------+----------------+-------------+-------------+ | Lorem ipsum dolo | Vivamus laoreet | Lorem ipsum | test | test | | r sit amet, cons | vestibulum preti | | | | | ectetur adipisci | um. Nulla et orn | | | | | ng elit. Vivamus | are elit. Cum so | | | | | laoreet vestibu | ciis natoque pen | | | | | lum pretium. Nul | atibus et magnis | | | | | la et ornare eli | | | | | | t. Cum sociis na | | | | | | toque penatibus | | | | | | et magnis | | | | | +---------------------+---------------------+----------------+-------------+-------------+ | test string | test string 2 | test | row | bndr | +---------------------+---------------------+----------------+-------------+-------------+ ``` ## Examples ``` t := gotabulate.Create([][]string{STRING_ARRAY, STRING_ARRAY}) t.SetHeaders(HEADERS) // If not headers are set, the first row will be used. t.SetEmptyString("None") // Set what will be printed in the empty cell rendered_string := t.Render("simple") // Render() will return a string Simple Table ---------------------- ---------------------- ---------------------- ------------- ------------- Header 1 Header 2 Header 3 Header 4 Header 5 ---------------------- ---------------------- ---------------------- ------------- ------------- test string test string 2 test row bndr test string test string 2 test row bndr 4th element empty 4th element empty 4th element empty None None ---------------------- ---------------------- ---------------------- ------------- ------------- Grid Table (Align Right) +-------------+-------------+-------------+-------------+-------------+ | Header 1 | Header 2 | Header 3 | Header 4 | Header 5 | +=============+=============+=============+=============+=============+ | 10.01 | 12.002 | -123.5 | 20.00005 | 1.01 | +-------------+-------------+-------------+-------------+-------------+ | 10.01 | 12.002 | -123.5 | 20.00005 | 1.01 | +-------------+-------------+-------------+-------------+-------------+ | 10.01 | 12.002 | -123.5 | 20.00005 | None | +-------------+-------------+-------------+-------------+-------------+ Padded Headers: +----------------------+----------------------+----------------------+-------------+-------------+ | | Header 1 | header 2 | header 3 | header 4 | +======================+======================+======================+=============+=============+ | test string | test string 2 | test | row | bndr | +----------------------+----------------------+----------------------+-------------+-------------+ | test string | test string 2 | test | row | bndr | +----------------------+----------------------+----------------------+-------------+-------------+ | 4th element empty | 4th element empty | 4th element empty | None | None | +----------------------+----------------------+----------------------+-------------+-------------+ Align Center: +-------------+-------------+-------------+-------------+-------------+ | Header 1 | Header 2 | Header 3 | Header 4 | Header 5 | +=============+=============+=============+=============+=============+ | 10.01 | 12.002 | -123.5 | 20.00005 | 1.01 | +-------------+-------------+-------------+-------------+-------------+ | 10.01 | 12.002 | -123.5 | 20.00005 | 1.01 | +-------------+-------------+-------------+-------------+-------------+ | 10.01 | 12.002 | -123.5 | 20.00005 | 1.01 | +-------------+-------------+-------------+-------------+-------------+ Align Left: +-------------+-------------+-------------+-------------+-------------+ | Header 1 | Header 2 | Header 3 | Header 4 | Header 5 | +=============+=============+=============+=============+=============+ | 10.01 | 12.002 | -123.5 | 20.00005 | 1.01 | +-------------+-------------+-------------+-------------+-------------+ | 10.01 | 12.002 | -123.5 | 20.00005 | 1.01 | +-------------+-------------+-------------+-------------+-------------+ | 10.01 | 12.002 | -123.5 | 20.00005 | 1.01 | +-------------+-------------+-------------+-------------+-------------+ ``` ### Status Beta version. There may be edge cases that I have missed, so if your tables don't render properly please open up an issue. ## Contribute All Contributions are welcome. The todo list is on the bottom of this README. Feel free to send a pull request. ## License Apache License 2.0 ### TODO - Add more examples - Implement more data table formats ### Acknowledgement Inspired by Python package https://pypi.python.org/pypi/tabulategotabulate-1.1.2/_tests/000077500000000000000000000000001240726752400151545ustar00rootroot00000000000000gotabulate-1.1.2/_tests/grid_floats000066400000000000000000000012101240726752400173660ustar00rootroot00000000000000+-------------+-------------+-------------+-------------+-------------+ | Header 1 | Header 2 | Header 3 | Header 4 | Header 5 | +=============+=============+=============+=============+=============+ | 10.01 | 12.002 | -123.5 | 20.00005 | 1.01 | +-------------+-------------+-------------+-------------+-------------+ | 10.01 | 12.002 | -123.5 | 20.00005 | 1.01 | +-------------+-------------+-------------+-------------+-------------+ | 10.01 | 12.002 | -123.5 | 20.00005 | None | +-------------+-------------+-------------+-------------+-------------+ gotabulate-1.1.2/_tests/grid_ints000066400000000000000000000012101240726752400170530ustar00rootroot00000000000000+-------------+-------------+-------------+-------------+-------------+ | Header 1 | Header 2 | Header 3 | Header 4 | Header 5 | +=============+=============+=============+=============+=============+ | 1 | 2 | 3 | 1000 | 200 | +-------------+-------------+-------------+-------------+-------------+ | 1 | 2 | 3 | 1000 | 200 | +-------------+-------------+-------------+-------------+-------------+ | 1 | 2 | 3 | 1000 | None | +-------------+-------------+-------------+-------------+-------------+ gotabulate-1.1.2/_tests/grid_ints64000066400000000000000000000012101240726752400172250ustar00rootroot00000000000000+-------------+-------------+-------------+-------------+-------------+ | Header 1 | Header 2 | Header 3 | Header 4 | Header 5 | +=============+=============+=============+=============+=============+ | 100 | 500 | 600 | 1000 | 10000 | +-------------+-------------+-------------+-------------+-------------+ | 100 | 500 | 600 | 1000 | 10000 | +-------------+-------------+-------------+-------------+-------------+ | 100 | 500 | 600 | 1000 | None | +-------------+-------------+-------------+-------------+-------------+ gotabulate-1.1.2/_tests/grid_map_mixed000066400000000000000000000010421240726752400200440ustar00rootroot00000000000000+-------------+-------------+-------------+-------------------+-------------+ | Header 1 | Header 2 | Header 3 | Header 4 | Header 5 | +=============+=============+=============+===================+=============+ | string | 1 | 1.005 | another string | -2 | +-------------+-------------+-------------+-------------------+-------------+ | string | 1 | 1.005 | another string | -2 | +-------------+-------------+-------------+-------------------+-------------+ gotabulate-1.1.2/_tests/grid_mixed000066400000000000000000000010421240726752400172070ustar00rootroot00000000000000+-------------+-------------+-------------+-------------------+-------------+ | Header 1 | Header 2 | Header 3 | Header 4 | Header 5 | +=============+=============+=============+===================+=============+ | string | 1 | 1.005 | another string | -2 | +-------------+-------------+-------------+-------------------+-------------+ | string | 1 | 1.005 | another string | -2 | +-------------+-------------+-------------+-------------------+-------------+ gotabulate-1.1.2/_tests/grid_strings000066400000000000000000000015731240726752400176030ustar00rootroot00000000000000+----------------------+----------------------+----------------------+-------------+-------------+ | Header 1 | Header 2 | Header 3 | Header 4 | Header 5 | +======================+======================+======================+=============+=============+ | test string | test string 2 | test | row | bndr | +----------------------+----------------------+----------------------+-------------+-------------+ | test string | test string 2 | test | row | bndr | +----------------------+----------------------+----------------------+-------------+-------------+ | 4th element empty | 4th element empty | 4th element empty | None | None | +----------------------+----------------------+----------------------+-------------+-------------+ gotabulate-1.1.2/_tests/simple_floats000066400000000000000000000010101240726752400177300ustar00rootroot00000000000000------------- ------------- ------------- ------------- ------------- Header 1 Header 2 Header 3 Header 4 Header 5 ------------- ------------- ------------- ------------- ------------- 10.01 12.002 -123.5 20.00005 1.01 10.01 12.002 -123.5 20.00005 1.01 10.01 12.002 -123.5 20.00005 None ------------- ------------- ------------- ------------- ------------- gotabulate-1.1.2/_tests/simple_ints000066400000000000000000000010101240726752400174150ustar00rootroot00000000000000------------- ------------- ------------- ------------- ------------- Header 1 Header 2 Header 3 Header 4 Header 5 ------------- ------------- ------------- ------------- ------------- 1 2 3 1000 200 1 2 3 1000 200 1 2 3 1000 None ------------- ------------- ------------- ------------- ------------- gotabulate-1.1.2/_tests/simple_ints64000066400000000000000000000010101240726752400175670ustar00rootroot00000000000000------------- ------------- ------------- ------------- ------------- Header 1 Header 2 Header 3 Header 4 Header 5 ------------- ------------- ------------- ------------- ------------- 100 500 600 1000 10000 100 500 600 1000 10000 100 500 600 1000 None ------------- ------------- ------------- ------------- ------------- gotabulate-1.1.2/_tests/simple_map_mixed000066400000000000000000000007411240726752400204150ustar00rootroot00000000000000------------- ------------- ------------- ------------------- ------------- Header 1 Header 2 Header 3 Header 4 Header 5 ------------- ------------- ------------- ------------------- ------------- string 1 1.005 another string -2 string 1 1.005 another string -2 ------------- ------------- ------------- ------------------- ------------- gotabulate-1.1.2/_tests/simple_mixed000066400000000000000000000007411240726752400175600ustar00rootroot00000000000000------------- ------------- ------------- ------------------- ------------- Header 1 Header 2 Header 3 Header 4 Header 5 ------------- ------------- ------------- ------------------- ------------- string 1 1.005 another string -2 string 1 1.005 another string -2 ------------- ------------- ------------- ------------------- ------------- gotabulate-1.1.2/_tests/simple_strings000066400000000000000000000013051240726752400201400ustar00rootroot00000000000000---------------------- ---------------------- ---------------------- ------------- ------------- Header 1 Header 2 Header 3 Header 4 Header 5 ---------------------- ---------------------- ---------------------- ------------- ------------- test string test string 2 test row bndr test string test string 2 test row bndr 4th element empty 4th element empty 4th element empty None None ---------------------- ---------------------- ---------------------- ------------- ------------- gotabulate-1.1.2/_tests/table_float000066400000000000000000000012101240726752400173450ustar00rootroot00000000000000+-------------+-------------+-------------+-------------+-------------+ | Header 1 | Header 2 | Header 3 | Header 4 | Header 5 | +=============+=============+=============+=============+=============+ | 10.01 | 12.002 | -123.5 | 20.00005 | 1.01 | +-------------+-------------+-------------+-------------+-------------+ | 10.01 | 12.002 | -123.5 | 20.00005 | 1.01 | +-------------+-------------+-------------+-------------+-------------+ | 10.01 | 12.002 | -123.5 | 20.00005 | 1.01 | +-------------+-------------+-------------+-------------+-------------+ gotabulate-1.1.2/_tests/table_float_center_pad000066400000000000000000000012101240726752400215310ustar00rootroot00000000000000+-------------+-------------+-------------+-------------+-------------+ | Header 1 | Header 2 | Header 3 | Header 4 | Header 5 | +=============+=============+=============+=============+=============+ | 10.01 | 12.002 | -123.5 | 20.00005 | 1.01 | +-------------+-------------+-------------+-------------+-------------+ | 10.01 | 12.002 | -123.5 | 20.00005 | 1.01 | +-------------+-------------+-------------+-------------+-------------+ | 10.01 | 12.002 | -123.5 | 20.00005 | 1.01 | +-------------+-------------+-------------+-------------+-------------+ gotabulate-1.1.2/_tests/table_float_right_pad000066400000000000000000000012101240726752400213660ustar00rootroot00000000000000+-------------+-------------+-------------+-------------+-------------+ | Header 1 | Header 2 | Header 3 | Header 4 | Header 5 | +=============+=============+=============+=============+=============+ | 10.01 | 12.002 | -123.5 | 20.00005 | 1.01 | +-------------+-------------+-------------+-------------+-------------+ | 10.01 | 12.002 | -123.5 | 20.00005 | 1.01 | +-------------+-------------+-------------+-------------+-------------+ | 10.01 | 12.002 | -123.5 | 20.00005 | 1.01 | +-------------+-------------+-------------+-------------+-------------+ gotabulate-1.1.2/_tests/test_empty_element000066400000000000000000000012651240726752400210110ustar00rootroot00000000000000+----------------------+----------------------+----------------------+-------------+-------------+ | Header 1 | Header 2 | Header 3 | Header 4 | Header 5 | +======================+======================+======================+=============+=============+ | test string | test string 2 | test | row | bndr | +----------------------+----------------------+----------------------+-------------+-------------+ | 4th element empty | 4th element empty | 4th element empty | None | None | +----------------------+----------------------+----------------------+-------------+-------------+ gotabulate-1.1.2/_tests/test_first_row000066400000000000000000000005171240726752400201570ustar00rootroot00000000000000+----------------+------------------+---------+--------+---------+ | test string | test string 2 | test | row | bndr | +================+==================+=========+========+=========+ | test string | test string 2 | test | row | bndr | +----------------+------------------+---------+--------+---------+ gotabulate-1.1.2/_tests/test_headers000066400000000000000000000006321240726752400175520ustar00rootroot00000000000000---------------- ------------------ ------------- ------------- ------------- Header 1 Header 2 Header 3 Header 4 Header 5 ---------------- ------------------ ------------- ------------- ------------- test string test string 2 test row bndr ---------------- ------------------ ------------- ------------- ------------- gotabulate-1.1.2/_tests/test_hide_lines000066400000000000000000000014301240726752400202370ustar00rootroot00000000000000+----------------------+----------------------+----------------------+-------------+-------------+ | | Header 1 | header 2 | header 3 | header 4 | | test string | test string 2 | test | row | bndr | +----------------------+----------------------+----------------------+-------------+-------------+ | test string | test string 2 | test | row | bndr | +----------------------+----------------------+----------------------+-------------+-------------+ | 4th element empty | 4th element empty | 4th element empty | | | +----------------------+----------------------+----------------------+-------------+-------------+ gotabulate-1.1.2/_tests/test_padded_headers000066400000000000000000000015731240726752400210600ustar00rootroot00000000000000+----------------------+----------------------+----------------------+-------------+-------------+ | | Header 1 | header 2 | header 3 | header 4 | +======================+======================+======================+=============+=============+ | test string | test string 2 | test | row | bndr | +----------------------+----------------------+----------------------+-------------+-------------+ | test string | test string 2 | test | row | bndr | +----------------------+----------------------+----------------------+-------------+-------------+ | 4th element empty | 4th element empty | 4th element empty | None | None | +----------------------+----------------------+----------------------+-------------+-------------+ gotabulate-1.1.2/_tests/test_string_wrap000066400000000000000000000026601240726752400205010ustar00rootroot00000000000000+---------------------+---------------------+----------------+-------------+-------------+ | | Header 1 | header 2 | header 3 | header 4 | +=====================+=====================+================+=============+=============+ | Very long string | Another very lon | Lorem ipsum | test | test | | array test stri | g string just fo | | | | | array test stri | g string just fo | | | | | ng array test st | r testing | | | | | ng array test st | r testing | | | | | ring array test | | | | | +---------------------+---------------------+----------------+-------------+-------------+ | ring array test | | | | | +---------------------+---------------------+----------------+-------------+-------------+ | ring array test | | | | | +---------------------+---------------------+----------------+-------------+-------------+ | ring array test | | | | | +---------------------+---------------------+----------------+-------------+-------------+ gotabulate-1.1.2/_tests/test_string_wrap_grid000066400000000000000000000072101240726752400215020ustar00rootroot00000000000000+---------------------+---------------------+------------------+-------------+-------------+ | | Header 1 | header 2 | header 3 | header 4 | +=====================+=====================+==================+=============+=============+ | Lorem ipsum dolo | Vivamus laoreet | zzLorem ipsum | test | test | | r sit amet, cons | vestibulum preti | | | | | ectetur adipisci | um. Nulla et orn | | | | | ng elit. Vivamus | are elit. Cum so | | | | | laoreet vestibu | ciis natoque pen | | | | | lum pretium. Nul | atibus et magnis | | | | | la et ornare eli | | | | | | t. Cum sociis na | | | | | | toque penatibus | | | | | | et magnis | | | | | +---------------------+---------------------+------------------+-------------+-------------+ | Lorem ipsum dolo | Vivamus laoreet | zzLorem ipsum | test | test | | r sit amet, cons | vestibulum preti | | | | | ectetur adipisci | um. Nulla et orn | | | | | ng elit. Vivamus | are elit. Cum so | | | | | laoreet vestibu | ciis natoque pen | | | | | lum pretium. Nul | atibus et magnis | | | | | la et ornare eli | | | | | | t. Cum sociis na | | | | | | toque penatibus | | | | | | et magnis | | | | | +---------------------+---------------------+------------------+-------------+-------------+ | test string | test string 2 | test | row | bndr | +---------------------+---------------------+------------------+-------------+-------------+ | Lorem ipsum dolo | Vivamus laoreet | zzLorem ipsum | test | test | | r sit amet, cons | vestibulum preti | | | | | ectetur adipisci | um. Nulla et orn | | | | | ng elit. Vivamus | are elit. Cum so | | | | | laoreet vestibu | ciis natoque pen | | | | | lum pretium. Nul | atibus et magnis | | | | | la et ornare eli | | | | | | t. Cum sociis na | | | | | | toque penatibus | | | | | | et magnis | | | | | +---------------------+---------------------+------------------+-------------+-------------+ | test string | test string 2 | test | row | bndr | +---------------------+---------------------+------------------+-------------+-------------+ gotabulate-1.1.2/_tests/test_string_wrap_simple000066400000000000000000000065401240726752400220530ustar00rootroot00000000000000--------------------- --------------------- ------------------ ------------- ------------- Header 1 header 2 header 3 header 4 --------------------- --------------------- ------------------ ------------- ------------- Lorem ipsum dolo Vivamus laoreet zzLorem ipsum test test r sit amet, cons vestibulum preti ectetur adipisci um. Nulla et orn ng elit. Vivamus are elit. Cum so laoreet vestibu ciis natoque pen lum pretium. Nul atibus et magnis la et ornare eli t. Cum sociis na toque penatibus et magnis Lorem ipsum dolo Vivamus laoreet zzLorem ipsum test test r sit amet, cons vestibulum preti ectetur adipisci um. Nulla et orn ng elit. Vivamus are elit. Cum so laoreet vestibu ciis natoque pen lum pretium. Nul atibus et magnis la et ornare eli t. Cum sociis na toque penatibus et magnis test string test string 2 test row bndr Lorem ipsum dolo Vivamus laoreet zzLorem ipsum test test r sit amet, cons vestibulum preti ectetur adipisci um. Nulla et orn ng elit. Vivamus are elit. Cum so laoreet vestibu ciis natoque pen lum pretium. Nul atibus et magnis la et ornare eli t. Cum sociis na toque penatibus et magnis test string test string 2 test row bndr --------------------- --------------------- ------------------ ------------- ------------- gotabulate-1.1.2/tabulate.go000066400000000000000000000253001240726752400160030ustar00rootroot00000000000000package gotabulate import "fmt" import "bytes" import "math" // Basic Structure of TableFormat type TableFormat struct { LineTop Line LineBelowHeader Line LineBetweenRows Line LineBottom Line HeaderRow Row DataRow Row Padding int HeaderHide bool FitScreen bool } // Represents a Line type Line struct { begin string hline string sep string end string } // Represents a Row type Row struct { begin string sep string end string } // Table Formats that are available to the user // The user can define his own format, just by addind an entry to this map // and calling it with Render function e.g t.Render("customFormat") var TableFormats = map[string]TableFormat{ "simple": TableFormat{ LineTop: Line{"", "-", " ", ""}, LineBelowHeader: Line{"", "-", " ", ""}, LineBottom: Line{"", "-", " ", ""}, HeaderRow: Row{"", " ", ""}, DataRow: Row{"", " ", ""}, Padding: 1, }, "plain": TableFormat{ HeaderRow: Row{"", " ", ""}, DataRow: Row{"", " ", ""}, Padding: 1, }, "grid": TableFormat{ LineTop: Line{"+", "-", "+", "+"}, LineBelowHeader: Line{"+", "=", "+", "+"}, LineBetweenRows: Line{"+", "-", "+", "+"}, LineBottom: Line{"+", "-", "+", "+"}, HeaderRow: Row{"|", "|", "|"}, DataRow: Row{"|", "|", "|"}, Padding: 1, }, } // Minimum padding that will be applied var MIN_PADDING = 5 // Main Tabulate structure type Tabulate struct { Data []*TabulateRow Headers []string FloatFormat byte TableFormat TableFormat Align string EmptyVar string HideLines []string MaxSize int WrapStrings bool } // Represents normalized tabulate Row type TabulateRow struct { Elements []string Continuos bool Extra bool } // Add padding to each cell func (t *Tabulate) padRow(arr []string, padding int) []string { if len(arr) < 1 { return arr } padded := make([]string, len(arr)) for index, el := range arr { var buffer bytes.Buffer // Pad left for i := 0; i < padding; i++ { buffer.WriteString(" ") } buffer.WriteString(el) // Pad Right for i := 0; i < padding; i++ { buffer.WriteString(" ") } padded[index] = buffer.String() } return padded } // Align right (Add padding left) func (t *Tabulate) padLeft(width int, str string) string { var buffer bytes.Buffer // Pad left padding := width - len(str) for i := 0; i < padding; i++ { buffer.WriteString(" ") } buffer.WriteString(str) return buffer.String() } // Align Left (Add padding right) func (t *Tabulate) padRight(width int, str string) string { var buffer bytes.Buffer padding := width - len(str) buffer.WriteString(str) // Add Padding right for i := 0; i < padding; i++ { buffer.WriteString(" ") } return buffer.String() } // Center the element in the cell func (t *Tabulate) padCenter(width int, str string) string { var buffer bytes.Buffer length := len(str) padding := int(math.Ceil(float64((width - length)) / 2.0)) // Add padding left for i := 0; i < padding; i++ { buffer.WriteString(" ") } // Write string buffer.WriteString(str) // Calculate how much space is left current := (width - len(buffer.String())) // Add padding right for i := 0; i < current; i++ { buffer.WriteString(" ") } return buffer.String() } // Build Line based on padded_widths from t.GetWidths() func (t *Tabulate) buildLine(padded_widths []int, padding []int, l Line) string { cells := make([]string, len(padded_widths)) for i, _ := range cells { var buffer bytes.Buffer for j := 0; j < padding[i]+MIN_PADDING; j++ { buffer.WriteString(l.hline) } cells[i] = buffer.String() } var buffer bytes.Buffer // Print begin buffer.WriteString(l.begin) // Print contents for i := 0; i < len(cells); i++ { if i != len(cells)-1 { buffer.WriteString(cells[i] + l.sep) } else { buffer.WriteString(cells[i]) } } // Print end buffer.WriteString(l.end) return buffer.String() } // Build Row based on padded_widths from t.GetWidths() func (t *Tabulate) buildRow(elements []string, padded_widths []int, paddings []int, d Row) string { var buffer bytes.Buffer buffer.WriteString(d.begin) padFunc := t.getAlignFunc() // Print contents for i := 0; i < len(padded_widths); i++ { output := "" if len(elements) > i { output = padFunc(padded_widths[i], elements[i]) } else { output = padFunc(padded_widths[i], t.EmptyVar) } buffer.WriteString(output) if i != len(padded_widths)-1 { buffer.WriteString(d.sep) } } // Print end buffer.WriteString(d.end) return buffer.String() } // Render the data table func (t *Tabulate) Render(format ...interface{}) string { var lines []string // If headers are set use them, otherwise pop the first row if len(t.Headers) < 1 { t.Headers, t.Data = t.Data[0].Elements, t.Data[1:] } // Use the format that was passed as parameter, otherwise // use the format defined in the struct if len(format) > 0 { t.TableFormat = TableFormats[format[0].(string)] } // If Wrap Strings is set to True,then break up the string to multiple cells if t.WrapStrings { t.Data = t.wrapCellData() } // Check if Data is present if len(t.Data) < 1 { panic("No Data specified") } if len(t.Headers) < len(t.Data[0].Elements) { diff := len(t.Data[0].Elements) - len(t.Headers) padded_header := make([]string, diff) for _, e := range t.Headers { padded_header = append(padded_header, e) } t.Headers = padded_header } // Get Column widths for all columns cols := t.getWidths(t.Headers, t.Data) padded_widths := make([]int, len(cols)) for i, _ := range padded_widths { padded_widths[i] = cols[i] + MIN_PADDING*t.TableFormat.Padding } // Start appending lines // Append top line if not hidden if !inSlice("top", t.HideLines) { lines = append(lines, t.buildLine(padded_widths, cols, t.TableFormat.LineTop)) } // Add Header lines = append(lines, t.buildRow(t.padRow(t.Headers, t.TableFormat.Padding), padded_widths, cols, t.TableFormat.HeaderRow)) // Add Line Below Header if not hidden if !inSlice("belowheader", t.HideLines) { lines = append(lines, t.buildLine(padded_widths, cols, t.TableFormat.LineBelowHeader)) } // Add Data Rows for index, element := range t.Data { lines = append(lines, t.buildRow(t.padRow(element.Elements, t.TableFormat.Padding), padded_widths, cols, t.TableFormat.DataRow)) if index < len(t.Data)-1 { if element.Continuos != true { lines = append(lines, t.buildLine(padded_widths, cols, t.TableFormat.LineBetweenRows)) } } } if !inSlice("bottomLine", t.HideLines) { lines = append(lines, t.buildLine(padded_widths, cols, t.TableFormat.LineBottom)) } // Join lines var buffer bytes.Buffer for _, line := range lines { buffer.WriteString(line + "\n") } return buffer.String() } // Calculate the max column width for each element func (t *Tabulate) getWidths(headers []string, data []*TabulateRow) []int { widths := make([]int, len(headers)) current_max := len(t.EmptyVar) for i := 0; i < len(headers); i++ { current_max = len(headers[i]) for _, item := range data { if len(item.Elements) > i && len(widths) > i { element := item.Elements[i] if len(element) > current_max { widths[i] = len(element) current_max = len(element) } else { widths[i] = current_max } } } } return widths } // Set Headers of the table // If Headers count is less than the data row count, the headers will be padded to the right func (t *Tabulate) SetHeaders(headers []string) *Tabulate { t.Headers = headers return t } // Set Float Formatting // will be used in strconv.FormatFloat(element, format, -1, 64) func (t *Tabulate) SetFloatFormat(format byte) *Tabulate { t.FloatFormat = format return t } // Set Align Type, Available options: left, right, center func (t *Tabulate) SetAlign(align string) { t.Align = align } // Select the padding function based on the align type func (t *Tabulate) getAlignFunc() func(int, string) string { if len(t.Align) < 1 || t.Align == "right" { return t.padLeft } else if t.Align == "left" { return t.padRight } else { return t.padCenter } } // Set how an empty cell will be represented func (t *Tabulate) SetEmptyString(empty string) { t.EmptyVar = empty + " " } // Set which lines to hide. // Can be: // top - Top line of the table, // belowheader - Line below the header, // bottom - Bottom line of the table func (t *Tabulate) SetHideLines(hide []string) { t.HideLines = hide } func (t *Tabulate) SetWrapStrings(wrap bool) { t.WrapStrings = wrap } // Sets the maximum size of cell // If WrapStrings is set to true, then the string inside // the cell will be split up into multiple cell func (t *Tabulate) SetMaxCellSize(max int) { t.MaxSize = max } // If string size is larger than t.MaxSize, then split it to multiple cells (downwards) func (t *Tabulate) wrapCellData() []*TabulateRow { var arr []*TabulateRow next := t.Data[0] for index := 0; index <= len(t.Data); index++ { elements := next.Elements new_elements := make([]string, len(elements)) for i, e := range elements { if len(e) > t.MaxSize { new_elements[i] = e[t.MaxSize:] elements[i] = e[:t.MaxSize] next.Continuos = true } } if next.Continuos { arr = append(arr, next) next = &TabulateRow{Elements: new_elements, Extra: true} index-- } else if next.Extra && index+1 < len(t.Data) { arr = append(arr, next) next = t.Data[index+1] } else if index+1 < len(t.Data) { arr = append(arr, next) next = t.Data[index+1] } else if index >= len(t.Data) { arr = append(arr, next) } } return arr } // Create a new Tabulate Object // Accepts 2D String Array, 2D Int Array, 2D Int64 Array, // 2D Bool Array, 2D Float64 Array, 2D interface{} Array, // Map map[strig]string, Map map[string]interface{}, func Create(data interface{}) *Tabulate { t := &Tabulate{FloatFormat: 'f', MaxSize: 30} switch v := data.(type) { case [][]string: t.Data = createFromString(data.([][]string)) case [][]int32: t.Data = createFromInt32(data.([][]int32)) case [][]int64: t.Data = createFromInt64(data.([][]int64)) case [][]int: t.Data = createFromInt(data.([][]int)) case [][]bool: t.Data = createFromBool(data.([][]bool)) case [][]float64: t.Data = createFromFloat64(data.([][]float64), t.FloatFormat) case [][]interface{}: t.Data = createFromMixed(data.([][]interface{}), t.FloatFormat) case []string: t.Data = createFromString([][]string{data.([]string)}) case []interface{}: t.Data = createFromMixed([][]interface{}{data.([]interface{})}, t.FloatFormat) case map[string][]interface{}: t.Headers, t.Data = createFromMapMixed(data.(map[string][]interface{}), t.FloatFormat) case map[string][]string: t.Headers, t.Data = createFromMapString(data.(map[string][]string)) default: fmt.Println(v) } return t } gotabulate-1.1.2/tabulate_test.go000066400000000000000000000203321240726752400170420ustar00rootroot00000000000000package gotabulate import "testing" import "io/ioutil" import "github.com/stretchr/testify/assert" var HEADERS = []string{"Header 1", "Header 2", "Header 3", "Header 4", "Header 5"} var INT_ARRAY = []int{1, 2, 3, 1000, 200} var INT64_ARRAY = []int64{100, 500, 600, 1000, 10000} var FLOAT_ARRAY = []float64{10.01, 12.002, -123.5, 20.00005, 1.01} var STRING_ARRAY = []string{"test string", "test string 2", "test", "row", "bndr"} var MIXED_ARRAY = []interface{}{"string", 1, 1.005, "another string", -2} var EMPTY_ARRAY = []string{"4th element empty", "4th element empty", "4th element empty"} var MIXED_MAP = map[string][]interface{}{"header1": MIXED_ARRAY, "header2": MIXED_ARRAY} // Test Setters func TestSetFormat(t *testing.T) { tabulate := Create([][]float64{FLOAT_ARRAY, FLOAT_ARRAY, FLOAT_ARRAY}) tabulate.SetHeaders(HEADERS) tabulate.SetFloatFormat('f') assert.Equal(t, tabulate.Render("grid"), readTable("_tests/table_float")) } // Test Align Left, Align Right, Align Center func TestPads(t *testing.T) { tabulate := Create([][]float64{FLOAT_ARRAY, FLOAT_ARRAY, FLOAT_ARRAY}) tabulate.SetHeaders(HEADERS) tabulate.SetAlign("left") assert.Equal(t, tabulate.Render("grid"), readTable("_tests/table_float_right_pad")) tabulate.SetAlign("right") assert.Equal(t, tabulate.Render("grid"), readTable("_tests/table_float")) tabulate.SetAlign("center") assert.Equal(t, tabulate.Render("grid"), readTable("_tests/table_float_center_pad")) } func TestSetHeaders(t *testing.T) { tabulate := Create([][]string{STRING_ARRAY}) tabulate.SetHeaders(HEADERS) assert.Equal(t, tabulate.Render("simple"), readTable("_tests/test_headers")) } func TestHeadersFirstRow(t *testing.T) { tabulate := Create([][]string{STRING_ARRAY, STRING_ARRAY}) assert.Equal(t, tabulate.Render("grid"), readTable("_tests/test_first_row")) } func TestEmptyString(t *testing.T) { tabulate := Create([][]string{STRING_ARRAY, EMPTY_ARRAY}) tabulate.SetHeaders(HEADERS) tabulate.SetEmptyString("None") assert.Equal(t, tabulate.Render("grid"), readTable("_tests/test_empty_element")) } func TestMaxColWidth(t *testing.T) { // TODO } // Test Simple func TestSimpleFloats(t *testing.T) { tabulate := Create([][]float64{FLOAT_ARRAY, FLOAT_ARRAY, FLOAT_ARRAY[:len(FLOAT_ARRAY)-1]}) tabulate.SetHeaders(HEADERS) tabulate.SetEmptyString("None") assert.Equal(t, tabulate.Render("simple"), readTable("_tests/simple_floats")) } func TestSimpleInts(t *testing.T) { tabulate := Create([][]int{INT_ARRAY, INT_ARRAY, INT_ARRAY[:len(INT_ARRAY)-1]}) tabulate.SetHeaders(HEADERS) tabulate.SetEmptyString("None") assert.Equal(t, tabulate.Render("simple"), readTable("_tests/simple_ints")) } func TestSimpleInts64(t *testing.T) { tabulate := Create([][]int64{INT64_ARRAY, INT64_ARRAY, INT64_ARRAY[:len(INT64_ARRAY)-1]}) tabulate.SetHeaders(HEADERS) tabulate.SetEmptyString("None") assert.Equal(t, tabulate.Render("simple"), readTable("_tests/simple_ints64")) } func TestSimpleString(t *testing.T) { tabulate := Create([][]string{STRING_ARRAY, STRING_ARRAY, EMPTY_ARRAY}) tabulate.SetHeaders(HEADERS) tabulate.SetEmptyString("None") assert.Equal(t, tabulate.Render("simple"), readTable("_tests/simple_strings")) } func TestSimpleMixed(t *testing.T) { tabulate := Create([][]interface{}{MIXED_ARRAY, MIXED_ARRAY}) tabulate.SetHeaders(HEADERS) tabulate.SetEmptyString("None") assert.Equal(t, tabulate.Render("simple"), readTable("_tests/simple_mixed")) } func TestSimpleMapMixed(t *testing.T) { tabulate := Create(MIXED_MAP) tabulate.SetHeaders(HEADERS) tabulate.SetEmptyString("None") assert.Equal(t, tabulate.Render("simple"), readTable("_tests/simple_map_mixed")) } // Test Grid func TestGridFloats(t *testing.T) { tabulate := Create([][]float64{FLOAT_ARRAY, FLOAT_ARRAY, FLOAT_ARRAY[:len(FLOAT_ARRAY)-1]}) tabulate.SetHeaders(HEADERS) tabulate.SetEmptyString("None") assert.Equal(t, tabulate.Render("grid"), readTable("_tests/grid_floats")) } func TestGridInts(t *testing.T) { tabulate := Create([][]int{INT_ARRAY, INT_ARRAY, INT_ARRAY[:len(INT_ARRAY)-1]}) tabulate.SetHeaders(HEADERS) tabulate.SetEmptyString("None") assert.Equal(t, tabulate.Render("grid"), readTable("_tests/grid_ints")) } func TestGridInts64(t *testing.T) { tabulate := Create([][]int64{INT64_ARRAY, INT64_ARRAY, INT64_ARRAY[:len(INT64_ARRAY)-1]}) tabulate.SetHeaders(HEADERS) tabulate.SetEmptyString("None") assert.Equal(t, tabulate.Render("grid"), readTable("_tests/grid_ints64")) } func TestGridString(t *testing.T) { tabulate := Create([][]string{STRING_ARRAY, STRING_ARRAY, EMPTY_ARRAY}) tabulate.SetHeaders(HEADERS) tabulate.SetEmptyString("None") assert.Equal(t, tabulate.Render("grid"), readTable("_tests/grid_strings")) } func TestGridMixed(t *testing.T) { tabulate := Create([][]interface{}{MIXED_ARRAY, MIXED_ARRAY}) tabulate.SetHeaders(HEADERS) tabulate.SetEmptyString("None") assert.Equal(t, tabulate.Render("grid"), readTable("_tests/grid_mixed")) } func TestGridMapMixed(t *testing.T) { tabulate := Create(MIXED_MAP) tabulate.SetHeaders(HEADERS) tabulate.SetEmptyString("None") assert.Equal(t, tabulate.Render("grid"), readTable("_tests/grid_map_mixed")) } func TestPaddedHeader(t *testing.T) { tabulate := Create([][]string{STRING_ARRAY, STRING_ARRAY, EMPTY_ARRAY}) tabulate.SetHeaders([]string{"Header 1", "header 2", "header 3", "header 4"}) tabulate.SetEmptyString("None") assert.Equal(t, tabulate.Render("grid"), readTable("_tests/test_padded_headers")) } func TestHideLineBelowHeader(t *testing.T) { tabulate := Create([][]string{STRING_ARRAY, STRING_ARRAY, EMPTY_ARRAY}) tabulate.SetHeaders([]string{"Header 1", "header 2", "header 3", "header 4"}) tabulate.SetHideLines([]string{"belowheader"}) assert.Equal(t, tabulate.Render("grid"), readTable("_tests/test_hide_lines")) } func TestWrapCells(t *testing.T) { tabulate := Create([][]string{[]string{"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus laoreet vestibulum pretium. Nulla et ornare elit. Cum sociis natoque penatibus et magnis", "Vivamus laoreet vestibulum pretium. Nulla et ornare elit. Cum sociis natoque penatibus et magnis", "zzLorem ipsum", " test", "test"}, []string{"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus laoreet vestibulum pretium. Nulla et ornare elit. Cum sociis natoque penatibus et magnis", "Vivamus laoreet vestibulum pretium. Nulla et ornare elit. Cum sociis natoque penatibus et magnis", "zzLorem ipsum", " test", "test"}, STRING_ARRAY, []string{"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus laoreet vestibulum pretium. Nulla et ornare elit. Cum sociis natoque penatibus et magnis", "Vivamus laoreet vestibulum pretium. Nulla et ornare elit. Cum sociis natoque penatibus et magnis", "zzLorem ipsum", " test", "test"}, STRING_ARRAY}) tabulate.SetHeaders([]string{"Header 1", "header 2", "header 3", "header 4"}) tabulate.SetMaxCellSize(16) tabulate.SetWrapStrings(true) assert.Equal(t, tabulate.Render("grid"), readTable("_tests/test_string_wrap_grid")) } func TestWrapCellsSimple(t *testing.T) { tabulate := Create([][]string{[]string{"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus laoreet vestibulum pretium. Nulla et ornare elit. Cum sociis natoque penatibus et magnis", "Vivamus laoreet vestibulum pretium. Nulla et ornare elit. Cum sociis natoque penatibus et magnis", "zzLorem ipsum", " test", "test"}, []string{"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus laoreet vestibulum pretium. Nulla et ornare elit. Cum sociis natoque penatibus et magnis", "Vivamus laoreet vestibulum pretium. Nulla et ornare elit. Cum sociis natoque penatibus et magnis", "zzLorem ipsum", " test", "test"}, STRING_ARRAY, []string{"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus laoreet vestibulum pretium. Nulla et ornare elit. Cum sociis natoque penatibus et magnis", "Vivamus laoreet vestibulum pretium. Nulla et ornare elit. Cum sociis natoque penatibus et magnis", "zzLorem ipsum", " test", "test"}, STRING_ARRAY}) tabulate.SetHeaders([]string{"Header 1", "header 2", "header 3", "header 4"}) tabulate.SetMaxCellSize(16) tabulate.SetWrapStrings(true) assert.Equal(t, tabulate.Render("simple"), readTable("_tests/test_string_wrap_simple")) } func readTable(path string) string { buf, err := ioutil.ReadFile(path) if err != nil { panic(err) } return string(buf) } gotabulate-1.1.2/utils.go000066400000000000000000000075221240726752400153500ustar00rootroot00000000000000package gotabulate import "strconv" import "fmt" // Create normalized Array from strings func createFromString(data [][]string) []*TabulateRow { rows := make([]*TabulateRow, len(data)) for index, el := range data { rows[index] = &TabulateRow{Elements: el} } return rows } // Create normalized array of rows from mixed data (interface{}) func createFromMixed(data [][]interface{}, format byte) []*TabulateRow { rows := make([]*TabulateRow, len(data)) for index_1, element := range data { normalized := make([]string, len(element)) for index, el := range element { switch el.(type) { case int32: quoted := strconv.QuoteRuneToASCII(el.(int32)) normalized[index] = quoted[1 : len(quoted)-1] case int: normalized[index] = strconv.Itoa(el.(int)) case int64: normalized[index] = strconv.FormatInt(el.(int64), 10) case bool: normalized[index] = strconv.FormatBool(el.(bool)) case float64: normalized[index] = strconv.FormatFloat(el.(float64), format, -1, 64) case uint64: normalized[index] = strconv.FormatUint(el.(uint64), 10) default: normalized[index] = fmt.Sprintf("%s", el) } } rows[index_1] = &TabulateRow{Elements: normalized} } return rows } // Create normalized array from ints func createFromInt(data [][]int) []*TabulateRow { rows := make([]*TabulateRow, len(data)) for index_1, arr := range data { row := make([]string, len(arr)) for index, el := range arr { row[index] = strconv.Itoa(el) } rows[index_1] = &TabulateRow{Elements: row} } return rows } // Create normalized array from float64 func createFromFloat64(data [][]float64, format byte) []*TabulateRow { rows := make([]*TabulateRow, len(data)) for index_1, arr := range data { row := make([]string, len(arr)) for index, el := range arr { row[index] = strconv.FormatFloat(el, format, -1, 64) } rows[index_1] = &TabulateRow{Elements: row} } return rows } // Create normalized array from ints32 func createFromInt32(data [][]int32) []*TabulateRow { rows := make([]*TabulateRow, len(data)) for index_1, arr := range data { row := make([]string, len(arr)) for index, el := range arr { quoted := strconv.QuoteRuneToASCII(el) row[index] = quoted[1 : len(quoted)-1] } rows[index_1] = &TabulateRow{Elements: row} } return rows } // Create normalized array from ints64 func createFromInt64(data [][]int64) []*TabulateRow { rows := make([]*TabulateRow, len(data)) for index_1, arr := range data { row := make([]string, len(arr)) for index, el := range arr { row[index] = strconv.FormatInt(el, 10) } rows[index_1] = &TabulateRow{Elements: row} } return rows } // Create normalized array from bools func createFromBool(data [][]bool) []*TabulateRow { rows := make([]*TabulateRow, len(data)) for index_1, arr := range data { row := make([]string, len(arr)) for index, el := range arr { row[index] = strconv.FormatBool(el) } rows[index_1] = &TabulateRow{Elements: row} } return rows } // Create normalized array from a map of mixed elements (interface{}) // Keys will be used as header func createFromMapMixed(data map[string][]interface{}, format byte) (headers []string, tData []*TabulateRow) { var dataslice [][]interface{} for key, value := range data { headers = append(headers, key) dataslice = append(dataslice, value) } return headers, createFromMixed(dataslice, format) } // Create normalized array from Map of strings // Keys will be used as header func createFromMapString(data map[string][]string) (headers []string, tData []*TabulateRow) { var dataslice [][]string for key, value := range data { headers = append(headers, key) dataslice = append(dataslice, value) } return headers, createFromString(dataslice) } // Check if element is present in a slice. func inSlice(a string, list []string) bool { for _, b := range list { if b == a { return true } } return false }