pax_global_header00006660000000000000000000000064125010647460014517gustar00rootroot0000000000000052 comment=ad45545899c7b13c020ea92b2072220eefad42b8 golang-github-mitchellh-go-wordwrap/000077500000000000000000000000001250106474600200275ustar00rootroot00000000000000golang-github-mitchellh-go-wordwrap/LICENSE.md000066400000000000000000000020751250106474600214370ustar00rootroot00000000000000The 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-mitchellh-go-wordwrap/README.md000066400000000000000000000020471250106474600213110ustar00rootroot00000000000000# 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-mitchellh-go-wordwrap/wordwrap.go000066400000000000000000000032171250106474600222260ustar00rootroot00000000000000package wordwrap import ( "bytes" "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 for _, char := range s { if char == '\n' { if wordBuf.Len() == 0 { if current+uint(spaceBuf.Len()) > lim { current = 0 } else { current += uint(spaceBuf.Len()) spaceBuf.WriteTo(buf) } spaceBuf.Reset() } else { current += uint(spaceBuf.Len() + wordBuf.Len()) spaceBuf.WriteTo(buf) spaceBuf.Reset() wordBuf.WriteTo(buf) wordBuf.Reset() } buf.WriteRune(char) current = 0 } else if unicode.IsSpace(char) { if spaceBuf.Len() == 0 || wordBuf.Len() > 0 { current += uint(spaceBuf.Len() + wordBuf.Len()) spaceBuf.WriteTo(buf) spaceBuf.Reset() wordBuf.WriteTo(buf) wordBuf.Reset() } spaceBuf.WriteRune(char) } else { wordBuf.WriteRune(char) if current+uint(spaceBuf.Len()+wordBuf.Len()) > lim && uint(wordBuf.Len()) < lim { buf.WriteRune('\n') current = 0 spaceBuf.Reset() } } } if wordBuf.Len() == 0 { if current+uint(spaceBuf.Len()) <= lim { spaceBuf.WriteTo(buf) } } else { spaceBuf.WriteTo(buf) wordBuf.WriteTo(buf) } return buf.String() } golang-github-mitchellh-go-wordwrap/wordwrap_test.go000066400000000000000000000032361250106474600232660ustar00rootroot00000000000000package 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) } } }