8
vendor/github.com/gosuri/uitable/.travis.yml
generated
vendored
Normal file
8
vendor/github.com/gosuri/uitable/.travis.yml
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
language: go
|
||||
sudo: false
|
||||
install:
|
||||
- go get ./...
|
||||
go:
|
||||
- 1.4
|
||||
- 1.5.2
|
||||
- tip
|
||||
10
vendor/github.com/gosuri/uitable/LICENSE
generated
vendored
Normal file
10
vendor/github.com/gosuri/uitable/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
MIT 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.
|
||||
4
vendor/github.com/gosuri/uitable/Makefile
generated
vendored
Normal file
4
vendor/github.com/gosuri/uitable/Makefile
generated
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
test:
|
||||
@go test ./...
|
||||
|
||||
.PHONY: test
|
||||
67
vendor/github.com/gosuri/uitable/README.md
generated
vendored
Normal file
67
vendor/github.com/gosuri/uitable/README.md
generated
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
# uitable [](https://godoc.org/github.com/gosuri/uitable) [](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
|
||||
```
|
||||
|
||||
|
||||
[](https://bitdeli.com/free "Bitdeli Badge")
|
||||
|
||||
203
vendor/github.com/gosuri/uitable/table.go
generated
vendored
Normal file
203
vendor/github.com/gosuri/uitable/table.go
generated
vendored
Normal file
@@ -0,0 +1,203 @@
|
||||
// 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
|
||||
}
|
||||
76
vendor/github.com/gosuri/uitable/util/strutil/strutil.go
generated
vendored
Normal file
76
vendor/github.com/gosuri/uitable/util/strutil/strutil.go
generated
vendored
Normal file
@@ -0,0 +1,76 @@
|
||||
// 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()
|
||||
}
|
||||
21
vendor/github.com/gosuri/uitable/util/wordwrap/LICENSE.md
generated
vendored
Normal file
21
vendor/github.com/gosuri/uitable/util/wordwrap/LICENSE.md
generated
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
The 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.
|
||||
39
vendor/github.com/gosuri/uitable/util/wordwrap/README.md
generated
vendored
Normal file
39
vendor/github.com/gosuri/uitable/util/wordwrap/README.md
generated
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
# 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.
|
||||
84
vendor/github.com/gosuri/uitable/util/wordwrap/wordwrap.go
generated
vendored
Normal file
84
vendor/github.com/gosuri/uitable/util/wordwrap/wordwrap.go
generated
vendored
Normal file
@@ -0,0 +1,84 @@
|
||||
// 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()
|
||||
}
|
||||
Reference in New Issue
Block a user