aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/mitchellh/go-wordwrap
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/mitchellh/go-wordwrap')
-rw-r--r--vendor/github.com/mitchellh/go-wordwrap/LICENSE.md21
-rw-r--r--vendor/github.com/mitchellh/go-wordwrap/README.md39
-rw-r--r--vendor/github.com/mitchellh/go-wordwrap/wordwrap.go73
3 files changed, 133 insertions, 0 deletions
diff --git a/vendor/github.com/mitchellh/go-wordwrap/LICENSE.md b/vendor/github.com/mitchellh/go-wordwrap/LICENSE.md
new file mode 100644
index 0000000..2298515
--- /dev/null
+++ b/vendor/github.com/mitchellh/go-wordwrap/LICENSE.md
@@ -0,0 +1,21 @@
1The MIT License (MIT)
2
3Copyright (c) 2014 Mitchell Hashimoto
4
5Permission is hereby granted, free of charge, to any person obtaining a copy
6of this software and associated documentation files (the "Software"), to deal
7in the Software without restriction, including without limitation the rights
8to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9copies of the Software, and to permit persons to whom the Software is
10furnished to do so, subject to the following conditions:
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21THE SOFTWARE.
diff --git a/vendor/github.com/mitchellh/go-wordwrap/README.md b/vendor/github.com/mitchellh/go-wordwrap/README.md
new file mode 100644
index 0000000..60ae311
--- /dev/null
+++ b/vendor/github.com/mitchellh/go-wordwrap/README.md
@@ -0,0 +1,39 @@
1# go-wordwrap
2
3`go-wordwrap` (Golang package: `wordwrap`) is a package for Go that
4automatically wraps words into multiple lines. The primary use case for this
5is in formatting CLI output, but of course word wrapping is a generally useful
6thing to do.
7
8## Installation and Usage
9
10Install using `go get github.com/mitchellh/go-wordwrap`.
11
12Full documentation is available at
13http://godoc.org/github.com/mitchellh/go-wordwrap
14
15Below is an example of its usage ignoring errors:
16
17```go
18wrapped := wordwrap.WrapString("foo bar baz", 3)
19fmt.Println(wrapped)
20```
21
22Would output:
23
24```
25foo
26bar
27baz
28```
29
30## Word Wrap Algorithm
31
32This library doesn't use any clever algorithm for word wrapping. The wrapping
33is actually very naive: whenever there is whitespace or an explicit linebreak.
34The goal of this library is for word wrapping CLI output, so the input is
35typically pretty well controlled human language. Because of this, the naive
36approach typically works just fine.
37
38In the future, we'd like to make the algorithm more advanced. We would do
39so without breaking the API.
diff --git a/vendor/github.com/mitchellh/go-wordwrap/wordwrap.go b/vendor/github.com/mitchellh/go-wordwrap/wordwrap.go
new file mode 100644
index 0000000..ac67205
--- /dev/null
+++ b/vendor/github.com/mitchellh/go-wordwrap/wordwrap.go
@@ -0,0 +1,73 @@
1package wordwrap
2
3import (
4 "bytes"
5 "unicode"
6)
7
8// WrapString wraps the given string within lim width in characters.
9//
10// Wrapping is currently naive and only happens at white-space. A future
11// version of the library will implement smarter wrapping. This means that
12// pathological cases can dramatically reach past the limit, such as a very
13// long word.
14func WrapString(s string, lim uint) string {
15 // Initialize a buffer with a slightly larger size to account for breaks
16 init := make([]byte, 0, len(s))
17 buf := bytes.NewBuffer(init)
18
19 var current uint
20 var wordBuf, spaceBuf bytes.Buffer
21
22 for _, char := range s {
23 if char == '\n' {
24 if wordBuf.Len() == 0 {
25 if current+uint(spaceBuf.Len()) > lim {
26 current = 0
27 } else {
28 current += uint(spaceBuf.Len())
29 spaceBuf.WriteTo(buf)
30 }
31 spaceBuf.Reset()
32 } else {
33 current += uint(spaceBuf.Len() + wordBuf.Len())
34 spaceBuf.WriteTo(buf)
35 spaceBuf.Reset()
36 wordBuf.WriteTo(buf)
37 wordBuf.Reset()
38 }
39 buf.WriteRune(char)
40 current = 0
41 } else if unicode.IsSpace(char) {
42 if spaceBuf.Len() == 0 || wordBuf.Len() > 0 {
43 current += uint(spaceBuf.Len() + wordBuf.Len())
44 spaceBuf.WriteTo(buf)
45 spaceBuf.Reset()
46 wordBuf.WriteTo(buf)
47 wordBuf.Reset()
48 }
49
50 spaceBuf.WriteRune(char)
51 } else {
52
53 wordBuf.WriteRune(char)
54
55 if current+uint(spaceBuf.Len()+wordBuf.Len()) > lim && uint(wordBuf.Len()) < lim {
56 buf.WriteRune('\n')
57 current = 0
58 spaceBuf.Reset()
59 }
60 }
61 }
62
63 if wordBuf.Len() == 0 {
64 if current+uint(spaceBuf.Len()) <= lim {
65 spaceBuf.WriteTo(buf)
66 }
67 } else {
68 spaceBuf.WriteTo(buf)
69 wordBuf.WriteTo(buf)
70 }
71
72 return buf.String()
73}