pax_global_header00006660000000000000000000000064127005504360014514gustar00rootroot0000000000000052 comment=36ee7e946282a3fb1cfecd476ddc9b35d8847e42 golang-github-gosuri-uitable-0.0~git20170830.36ee7e94/000077500000000000000000000000001270055043600217345ustar00rootroot00000000000000golang-github-gosuri-uitable-0.0~git20170830.36ee7e94/.travis.yml000066400000000000000000000001211270055043600240370ustar00rootroot00000000000000language: go sudo: false install: - go get ./... go: - 1.4 - 1.5.2 - tip golang-github-gosuri-uitable-0.0~git20170830.36ee7e94/LICENSE000066400000000000000000000020701270055043600227400ustar00rootroot00000000000000MIT License =========== Copyright (c) 2015, Greg Osuri 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. golang-github-gosuri-uitable-0.0~git20170830.36ee7e94/Makefile000066400000000000000000000000441270055043600233720ustar00rootroot00000000000000test: @go test ./... .PHONY: test golang-github-gosuri-uitable-0.0~git20170830.36ee7e94/README.md000066400000000000000000000035721270055043600232220ustar00rootroot00000000000000# uitable [![GoDoc](https://godoc.org/github.com/gosuri/uitable?status.svg)](https://godoc.org/github.com/gosuri/uitable) [![Build Status](https://travis-ci.org/gosuri/uitable.svg?branch=master)](https://travis-ci.org/gosuri/uitable) uitable is a go library for representing data as tables for terminal applications. It provides primitives for sizing and wrapping columns to improve readability. ## Example Usage Full source code for the example is available at [example/main.go](example/main.go) ```go table := uitable.New() table.MaxColWidth = 50 table.AddRow("NAME", "BIRTHDAY", "BIO") for _, hacker := range hackers { table.AddRow(hacker.Name, hacker.Birthday, hacker.Bio) } fmt.Println(table) ``` Will render the data as: ```sh NAME BIRTHDAY BIO Ada Lovelace December 10, 1815 Ada was a British mathematician and writer, chi... Alan Turing June 23, 1912 Alan was a British pioneering computer scientis... ``` For wrapping in two columns: ```go table = uitable.New() table.MaxColWidth = 80 table.Wrap = true // wrap columns for _, hacker := range hackers { table.AddRow("Name:", hacker.Name) table.AddRow("Birthday:", hacker.Birthday) table.AddRow("Bio:", hacker.Bio) table.AddRow("") // blank } fmt.Println(table) ``` Will render the data as: ``` Name: Ada Lovelace Birthday: December 10, 1815 Bio: Ada was a British mathematician and writer, chiefly known for her work on Charles Babbage's early mechanical general-purpose computer, the Analytical Engine Name: Alan Turing Birthday: June 23, 1912 Bio: Alan was a British pioneering computer scientist, mathematician, logician, cryptanalyst and theoretical biologist ``` ## Installation ``` $ go get -v github.com/gosuri/uitable ``` [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/gosuri/uitable/trend.png)](https://bitdeli.com/free "Bitdeli Badge") golang-github-gosuri-uitable-0.0~git20170830.36ee7e94/example/000077500000000000000000000000001270055043600233675ustar00rootroot00000000000000golang-github-gosuri-uitable-0.0~git20170830.36ee7e94/example/main.go000066400000000000000000000020161270055043600246410ustar00rootroot00000000000000package main import ( "fmt" "github.com/gosuri/uitable" ) type hacker struct { Name, Birthday, Bio string } var hackers = []hacker{ {"Ada Lovelace", "December 10, 1815", "Ada was a British mathematician and writer, chiefly known for her work on Charles Babbage's early mechanical general-purpose computer, the Analytical Engine"}, {"Alan Turing", "June 23, 1912", "Alan was a British pioneering computer scientist, mathematician, logician, cryptanalyst and theoretical biologist"}, } func main() { table := uitable.New() table.MaxColWidth = 50 fmt.Println("==> List") table.AddRow("NAME", "BIRTHDAY", "BIO") for _, hacker := range hackers { table.AddRow(hacker.Name, hacker.Birthday, hacker.Bio) } fmt.Println(table) fmt.Print("\n==> Details\n") table = uitable.New() table.MaxColWidth = 80 table.Wrap = true for _, hacker := range hackers { table.AddRow("Name:", hacker.Name) table.AddRow("Birthday:", hacker.Birthday) table.AddRow("Bio:", hacker.Bio) table.AddRow("") // blank } fmt.Println(table) } golang-github-gosuri-uitable-0.0~git20170830.36ee7e94/example_test.go000066400000000000000000000020311270055043600247510ustar00rootroot00000000000000package uitable_test import ( "fmt" "github.com/gosuri/uitable" ) type hacker struct { Name, Birthday, Bio string } var hackers = []hacker{ {"Ada Lovelace", "December 10, 1815", "Ada was a British mathematician and writer, chiefly known for her work on Charles Babbage's early mechanical general-purpose computer, the Analytical Engine"}, {"Alan Turing", "June 23, 1912", "Alan was a British pioneering computer scientist, mathematician, logician, cryptanalyst and theoretical biologist"}, } func Example() { table := uitable.New() table.MaxColWidth = 50 fmt.Println("==> List") table.AddRow("NAME", "BIRTHDAY", "BIO") for _, hacker := range hackers { table.AddRow(hacker.Name, hacker.Birthday, hacker.Bio) } fmt.Println(table) fmt.Print("\n==> Details\n") table = uitable.New() table.MaxColWidth = 80 table.Wrap = true for _, hacker := range hackers { table.AddRow("Name:", hacker.Name) table.AddRow("Birthday:", hacker.Birthday) table.AddRow("Bio:", hacker.Bio) table.AddRow("") // blank } fmt.Println(table) } golang-github-gosuri-uitable-0.0~git20170830.36ee7e94/table.go000066400000000000000000000110141270055043600233470ustar00rootroot00000000000000// Package uitable provides a decorator for formating data as a table package uitable import ( "fmt" "strings" "sync" "github.com/gosuri/uitable/util/strutil" "github.com/gosuri/uitable/util/wordwrap" "github.com/mattn/go-runewidth" ) // Separator is the default column seperator var Separator = "\t" // Table represents a decorator that renders the data in formatted in a table type Table struct { // Rows is the collection of rows in the table Rows []*Row // MaxColWidth is the maximum allowed width for cells in the table MaxColWidth uint // Wrap when set to true wraps the contents of the columns when the length exceeds the MaxColWidth Wrap bool // Separator is the seperator for columns in the table. Default is "\t" Separator string mtx *sync.RWMutex rightAlign map[int]bool } // New returns a new Table with default values func New() *Table { return &Table{ Separator: Separator, mtx: new(sync.RWMutex), rightAlign: map[int]bool{}, } } // AddRow adds a new row to the table func (t *Table) AddRow(data ...interface{}) *Table { t.mtx.Lock() defer t.mtx.Unlock() r := NewRow(data...) t.Rows = append(t.Rows, r) return t } // Bytes returns the []byte value of table func (t *Table) Bytes() []byte { return []byte(t.String()) } func (t *Table) RightAlign(col int) { t.mtx.Lock() t.rightAlign[col] = true t.mtx.Unlock() } // String returns the string value of table func (t *Table) String() string { t.mtx.RLock() defer t.mtx.RUnlock() if len(t.Rows) == 0 { return "" } // determine the width for each column (cell in a row) var colwidths []uint for _, row := range t.Rows { for i, cell := range row.Cells { // resize colwidth array if i+1 > len(colwidths) { colwidths = append(colwidths, 0) } cellwidth := cell.LineWidth() if t.MaxColWidth != 0 && cellwidth > t.MaxColWidth { cellwidth = t.MaxColWidth } if cellwidth > colwidths[i] { colwidths[i] = cellwidth } } } var lines []string for _, row := range t.Rows { row.Separator = t.Separator for i, cell := range row.Cells { cell.Width = colwidths[i] cell.Wrap = t.Wrap cell.RightAlign = t.rightAlign[i] } lines = append(lines, row.String()) } return strutil.Join(lines, "\n") } // Row represents a row in a table type Row struct { // Cells is the group of cell for the row Cells []*Cell // Separator for tabular columns Separator string } // NewRow returns a new Row and adds the data to the row func NewRow(data ...interface{}) *Row { r := &Row{Cells: make([]*Cell, len(data))} for i, d := range data { r.Cells[i] = &Cell{Data: d} } return r } // String returns the string representation of the row func (r *Row) String() string { // get the max number of lines for each cell var lc int // line count for _, cell := range r.Cells { if clc := len(strings.Split(cell.String(), "\n")); clc > lc { lc = clc } } // allocate a two-dimentional array of cells for each line and add size them cells := make([][]*Cell, lc) for x := 0; x < lc; x++ { cells[x] = make([]*Cell, len(r.Cells)) for y := 0; y < len(r.Cells); y++ { cells[x][y] = &Cell{Width: r.Cells[y].Width} } } // insert each line in a cell as new cell in the cells array for y, cell := range r.Cells { lines := strings.Split(cell.String(), "\n") for x, line := range lines { cells[x][y].Data = line } } // format each line lines := make([]string, lc) for x := range lines { line := make([]string, len(cells[x])) for y := range cells[x] { line[y] = cells[x][y].String() } lines[x] = strutil.Join(line, r.Separator) } return strutil.Join(lines, "\n") } // Cell represents a column in a row type Cell struct { // Width is the width of the cell Width uint // Wrap when true wraps the contents of the cell when the lenght exceeds the width Wrap bool // RightAlign when true aligns contents to the right RightAlign bool // Data is the cell data Data interface{} } // LineWidth returns the max width of all the lines in a cell func (c *Cell) LineWidth() uint { width := 0 for _, s := range strings.Split(c.String(), "\n") { w := runewidth.StringWidth(s) if w > width { width = w } } return uint(width) } // String returns the string formated representation of the cell func (c *Cell) String() string { if c.Data == nil { return strutil.PadLeft(" ", int(c.Width), ' ') } s := fmt.Sprintf("%v", c.Data) if c.Width > 0 { if c.Wrap && uint(len(s)) > c.Width { return wordwrap.WrapString(s, c.Width) } else { return strutil.Resize(s, c.Width, c.RightAlign) } } return s } golang-github-gosuri-uitable-0.0~git20170830.36ee7e94/table_test.go000066400000000000000000000025621270055043600244160ustar00rootroot00000000000000package uitable import ( "sync" "testing" ) func TestCell(t *testing.T) { c := &Cell{ Data: "foo bar", Width: 5, } got := c.String() if got != "fo..." { t.Fatal("need", "fo...", "got", got) } if c.LineWidth() != 5 { t.Fatal("need", 5, "got", c.LineWidth()) } c.Wrap = true got = c.String() if got != "foo\nbar" { t.Fatal("need", "foo\nbar", "got", got) } if c.LineWidth() != 3 { t.Fatal("need", 3, "got", c.LineWidth()) } } func TestRow(t *testing.T) { row := &Row{ Separator: "\t", Cells: []*Cell{ {Data: "foo", Width: 3, Wrap: true}, {Data: "bar baz", Width: 3, Wrap: true}, }, } got := row.String() need := "foo\tbar\n \tbaz" if got != need { t.Fatalf("need: %q | got: %q ", need, got) } } func TestAlign(t *testing.T) { table := New() table.AddRow("foo", "bar baz") table.Rows = []*Row{{ Separator: "\t", Cells: []*Cell{ {Data: "foo", Width: 5, Wrap: true}, {Data: "bar baz", Width: 10, Wrap: true}, }, }} table.RightAlign(1) got := table.String() need := "foo \t bar baz" if got != need { t.Fatalf("need: %q | got: %q ", need, got) } } func TestAddRow(t *testing.T) { var wg sync.WaitGroup table := New() for i := 0; i < 100; i++ { wg.Add(1) go func() { defer wg.Done() table.AddRow("foo") }() } wg.Wait() if len(table.Rows) != 100 { t.Fatal("want", 100, "got", len(table.Rows)) } } golang-github-gosuri-uitable-0.0~git20170830.36ee7e94/util/000077500000000000000000000000001270055043600227115ustar00rootroot00000000000000golang-github-gosuri-uitable-0.0~git20170830.36ee7e94/util/strutil/000077500000000000000000000000001270055043600244175ustar00rootroot00000000000000golang-github-gosuri-uitable-0.0~git20170830.36ee7e94/util/strutil/strutil.go000066400000000000000000000036671270055043600264700ustar00rootroot00000000000000// Package strutil provides various utilities for manipulating strings package strutil import ( "bytes" "github.com/mattn/go-runewidth" ) // PadRight returns a new string of a specified length in which the end of the current string is padded with spaces or with a specified Unicode character. func PadRight(str string, length int, pad byte) string { slen := runewidth.StringWidth(str) if slen >= length { return str } buf := bytes.NewBufferString(str) for i := 0; i < length-slen; i++ { buf.WriteByte(pad) } return buf.String() } // PadLeft returns a new string of a specified length in which the beginning of the current string is padded with spaces or with a specified Unicode character. func PadLeft(str string, length int, pad byte) string { slen := runewidth.StringWidth(str) if slen >= length { return str } var buf bytes.Buffer for i := 0; i < length-slen; i++ { buf.WriteByte(pad) } buf.WriteString(str) return buf.String() } // Resize resizes the string with the given length. It ellipses with '...' when the string's length exceeds // the desired length or pads spaces to the right of the string when length is smaller than desired func Resize(s string, length uint, rightAlign bool) string { slen := runewidth.StringWidth(s) n := int(length) if slen == n { return s } // Pads only when length of the string smaller than len needed if rightAlign { s = PadLeft(s, n, ' ') } else { s = PadRight(s, n, ' ') } if slen > n { rs := []rune(s) var buf bytes.Buffer w := 0 for _, r := range rs { buf.WriteRune(r) rw := runewidth.RuneWidth(r) if w+rw >= n-3 { break } w += rw } buf.WriteString("...") s = buf.String() } return s } // Join joins the list of the string with the delim provided func Join(list []string, delim string) string { var buf bytes.Buffer for i := 0; i < len(list)-1; i++ { buf.WriteString(list[i] + delim) } buf.WriteString(list[len(list)-1]) return buf.String() } golang-github-gosuri-uitable-0.0~git20170830.36ee7e94/util/strutil/strutil_test.go000066400000000000000000000016161270055043600275170ustar00rootroot00000000000000package strutil import ( "testing" ) func TestResize(t *testing.T) { s := "foo" got := Resize(s, 5, false) if len(got) != 5 { t.Fatal("want", 5, "got", len(got)) } s = "foobar" got = Resize(s, 5, false) if got != "fo..." { t.Fatal("want", "fo...", "got", got) } } func TestAlign(t *testing.T) { s := "foo" got := Resize(s, 5, false) if got != "foo " { t.Fatal("want", "foo ", "got", got) } got = Resize(s, 5, true) if got != " foo" { t.Fatal("want", " foo", "got", got) } } func TestJoin(t *testing.T) { got := Join([]string{"foo", "bar"}, ",") if got != "foo,bar" { t.Fatal("want", "foo,bar", "got", got) } } func TestPadRight(t *testing.T) { got := PadRight("foo", 5, '-') if got != "foo--" { t.Fatal("want", "foo--", "got", got) } } func TestPadLeft(t *testing.T) { got := PadLeft("foo", 5, '-') if got != "--foo" { t.Fatal("want", "--foo", "got", got) } } golang-github-gosuri-uitable-0.0~git20170830.36ee7e94/util/wordwrap/000077500000000000000000000000001270055043600245565ustar00rootroot00000000000000golang-github-gosuri-uitable-0.0~git20170830.36ee7e94/util/wordwrap/LICENSE.md000066400000000000000000000020751270055043600261660ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2014 Mitchell Hashimoto 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. golang-github-gosuri-uitable-0.0~git20170830.36ee7e94/util/wordwrap/README.md000066400000000000000000000020471270055043600260400ustar00rootroot00000000000000# go-wordwrap `go-wordwrap` (Golang package: `wordwrap`) is a package for Go that automatically wraps words into multiple lines. The primary use case for this is in formatting CLI output, but of course word wrapping is a generally useful thing to do. ## Installation and Usage Install using `go get github.com/mitchellh/go-wordwrap`. Full documentation is available at http://godoc.org/github.com/mitchellh/go-wordwrap Below is an example of its usage ignoring errors: ```go wrapped := wordwrap.WrapString("foo bar baz", 3) fmt.Println(wrapped) ``` Would output: ``` foo bar baz ``` ## Word Wrap Algorithm This library doesn't use any clever algorithm for word wrapping. The wrapping is actually very naive: whenever there is whitespace or an explicit linebreak. The goal of this library is for word wrapping CLI output, so the input is typically pretty well controlled human language. Because of this, the naive approach typically works just fine. In the future, we'd like to make the algorithm more advanced. We would do so without breaking the API. golang-github-gosuri-uitable-0.0~git20170830.36ee7e94/util/wordwrap/wordwrap.go000066400000000000000000000037551270055043600267640ustar00rootroot00000000000000// Package wordwrap provides methods for wrapping the contents of a string package wordwrap import ( "bytes" "github.com/mattn/go-runewidth" "unicode" ) // WrapString wraps the given string within lim width in characters. // // Wrapping is currently naive and only happens at white-space. A future // version of the library will implement smarter wrapping. This means that // pathological cases can dramatically reach past the limit, such as a very // long word. func WrapString(s string, lim uint) string { // Initialize a buffer with a slightly larger size to account for breaks init := make([]byte, 0, len(s)) buf := bytes.NewBuffer(init) var current uint var wordBuf, spaceBuf bytes.Buffer var wordWidth, spaceWidth int for _, char := range s { if char == '\n' { if wordBuf.Len() == 0 { if current+uint(spaceWidth) > lim { current = 0 } else { current += uint(spaceWidth) spaceBuf.WriteTo(buf) spaceWidth += runewidth.StringWidth(buf.String()) } spaceBuf.Reset() spaceWidth = 0 } else { current += uint(spaceWidth + wordWidth) spaceBuf.WriteTo(buf) spaceBuf.Reset() wordBuf.WriteTo(buf) wordBuf.Reset() spaceWidth = 0 wordWidth = 0 } buf.WriteRune(char) current = 0 } else if unicode.IsSpace(char) { if spaceBuf.Len() == 0 || wordBuf.Len() > 0 { current += uint(spaceWidth + wordWidth) spaceBuf.WriteTo(buf) spaceBuf.Reset() wordBuf.WriteTo(buf) wordBuf.Reset() spaceWidth = 0 wordWidth = 0 } spaceBuf.WriteRune(char) spaceWidth += runewidth.RuneWidth(char) } else { wordBuf.WriteRune(char) wordWidth += runewidth.RuneWidth(char) if current+uint(spaceWidth+wordWidth) > lim && uint(wordWidth) < lim { buf.WriteRune('\n') current = 0 spaceBuf.Reset() spaceWidth = 0 } } } if wordBuf.Len() == 0 { if current+uint(spaceWidth) <= lim { spaceBuf.WriteTo(buf) } } else { spaceBuf.WriteTo(buf) wordBuf.WriteTo(buf) } return buf.String() } golang-github-gosuri-uitable-0.0~git20170830.36ee7e94/util/wordwrap/wordwrap_test.go000066400000000000000000000032361270055043600300150ustar00rootroot00000000000000package wordwrap import ( "testing" ) func TestWrapString(t *testing.T) { cases := []struct { Input, Output string Lim uint }{ // A simple word passes through. { "foo", "foo", 4, }, // A single word that is too long passes through. // We do not break words. { "foobarbaz", "foobarbaz", 4, }, // Lines are broken at whitespace. { "foo bar baz", "foo\nbar\nbaz", 4, }, // Lines are broken at whitespace, even if words // are too long. We do not break words. { "foo bars bazzes", "foo\nbars\nbazzes", 4, }, // A word that would run beyond the width is wrapped. { "fo sop", "fo\nsop", 4, }, // Whitespace that trails a line and fits the width // passes through, as does whitespace prefixing an // explicit line break. A tab counts as one character. { "foo\nb\t r\n baz", "foo\nb\t r\n baz", 4, }, // Trailing whitespace is removed if it doesn't fit the width. // Runs of whitespace on which a line is broken are removed. { "foo \nb ar ", "foo\nb\nar", 4, }, // An explicit line break at the end of the input is preserved. { "foo bar baz\n", "foo\nbar\nbaz\n", 4, }, // Explicit break are always preserved. { "\nfoo bar\n\n\nbaz\n", "\nfoo\nbar\n\n\nbaz\n", 4, }, // Complete example: { " This is a list: \n\n\t* foo\n\t* bar\n\n\n\t* baz \nBAM ", " This\nis a\nlist: \n\n\t* foo\n\t* bar\n\n\n\t* baz\nBAM", 6, }, } for i, tc := range cases { actual := WrapString(tc.Input, tc.Lim) if actual != tc.Output { t.Fatalf("Case %d Input:\n\n`%s`\n\nActual Output:\n\n`%s`", i, tc.Input, actual) } } }