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