1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
# reflow
[](https://github.com/muesli/reflow/releases)
[](https://github.com/muesli/reflow/actions)
[](https://coveralls.io/github/muesli/reflow?branch=master)
[](http://goreportcard.com/report/muesli/reflow)
[](https://pkg.go.dev/github.com/muesli/reflow)
A collection of ANSI-aware methods and `io.Writers` helping you to transform
blocks of text. This means you can still style your terminal output with ANSI
escape sequences without them affecting the reflow operations & algorithms.
## Word-Wrapping
The `wordwrap` package lets you word-wrap strings or entire blocks of text.
```go
import "github.com/muesli/reflow/wordwrap"
s := wordwrap.String("Hello World!", 5)
fmt.Println(s)
```
Result:
```
Hello
World!
```
The word-wrapping Writer is compatible with the `io.Writer` / `io.WriteCloser` interfaces:
```go
f := wordwrap.NewWriter(limit)
f.Write(b)
f.Close()
fmt.Println(f.String())
```
Customize word-wrapping behavior:
```go
f := wordwrap.NewWriter(limit)
f.Breakpoints = []rune{':', ','}
f.Newline = []rune{'\r'}
```
## Unconditional Wrapping
The `wrap` package lets you unconditionally wrap strings or entire blocks of text.
```go
import "github.com/muesli/reflow/wrap"
s := wrap.String("Hello World!", 7)
fmt.Println(s)
```
Result:
```
Hello W
orld!
```
The unconditional wrapping Writer is compatible with the `io.Writer` interfaces:
```go
f := wrap.NewWriter(limit)
f.Write(b)
fmt.Println(f.String())
```
Customize word-wrapping behavior:
```go
f := wrap.NewWriter(limit)
f.Newline = []rune{'\r'}
f.KeepNewlines = false
f.reserveSpace = true
f.TabWidth = 2
```
**Tip:** This wrapping method can be used in conjunction with word-wrapping when word-wrapping is preferred but a line limit has to be enforced:
```go
wrapped := wrap.String(wordwrap.String("Just an example", 5), 5)
fmt.Println(wrapped)
```
Result:
```
Just
an
examp
le
```
### ANSI Example
```go
s := wordwrap.String("I really \x1B[38;2;249;38;114mlove\x1B[0m Go!", 8)
fmt.Println(s)
```
Result:

## Indentation
The `indent` package lets you indent strings or entire blocks of text.
```go
import "github.com/muesli/reflow/indent"
s := indent.String("Hello World!", 4)
fmt.Println(s)
```
Result: ` Hello World!`
There is also an indenting Writer, which is compatible with the `io.Writer`
interface:
```go
// indent uses spaces per default:
f := indent.NewWriter(width, nil)
// but you can also use a custom indentation function:
f = indent.NewWriter(width, func(w io.Writer) {
w.Write([]byte("."))
})
f.Write(b)
f.Close()
fmt.Println(f.String())
```
## Dedentation
The `dedent` package lets you dedent strings or entire blocks of text.
```go
import "github.com/muesli/reflow/dedent"
input := ` Hello World!
Hello World!
`
s := dedent.String(input)
fmt.Println(s)
```
Result:
```
Hello World!
Hello World!
```
## Padding
The `padding` package lets you pad strings or entire blocks of text.
```go
import "github.com/muesli/reflow/padding"
s := padding.String("Hello", 8)
fmt.Println(s)
```
Result: `Hello___` (the underlined portion represents 3 spaces)
There is also a padding Writer, which is compatible with the `io.WriteCloser`
interface:
```go
// padding uses spaces per default:
f := padding.NewWriter(width, nil)
// but you can also use a custom padding function:
f = padding.NewWriter(width, func(w io.Writer) {
w.Write([]byte("."))
})
f.Write(b)
f.Close()
fmt.Println(f.String())
```
|