-
Notifications
You must be signed in to change notification settings - Fork 2
/
wrap.go
58 lines (50 loc) · 1.66 KB
/
wrap.go
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
package mutators
import (
"bytes"
"fmt"
"io"
"github.com/muesli/reflow/indent"
"github.com/muesli/reflow/wordwrap"
uwrap "github.com/muesli/reflow/wrap"
)
const (
WrapMaxChars = 80
IndentChars = 4
)
func init() {
singleRegister("wrap", wordWrap, withDescription(
fmt.Sprintf("word-wrap the text (to X:%d chars maximum)", WrapMaxChars)),
withConfigBuilder(stdConfigUint64WithDefault(WrapMaxChars)))
singleRegister("wrapU", unconditionalyWrap, withDescription(
fmt.Sprintf("unconditionally wrap the text (to X:%d chars maximum)", WrapMaxChars)),
withConfigBuilder(stdConfigUint64WithDefault(WrapMaxChars)))
singleRegister("indent", singleIndent, withDescription(
fmt.Sprintf("indent the text (with X:%d chars)", IndentChars)),
withConfigBuilder(stdConfigUint64WithDefault(IndentChars)))
}
func wordWrap(w io.WriteCloser, r io.ReadCloser, config any) (int64, error) {
WrapMaxChars := int(config.(uint64))
ww := wordwrap.NewWriter(WrapMaxChars)
if _, err := io.Copy(ww, r); err != nil { // streamable?
return 0, err
}
ww.Close()
return io.Copy(w, bytes.NewReader(ww.Bytes()))
}
func unconditionalyWrap(w io.WriteCloser, r io.ReadCloser, config any) (int64, error) {
WrapMaxChars := int(config.(uint64))
ww := uwrap.NewWriter(WrapMaxChars)
if _, err := io.Copy(ww, r); err != nil { // streamable?
return 0, err
}
return io.Copy(w, bytes.NewReader(ww.Bytes()))
}
func singleIndent(w io.WriteCloser, r io.ReadCloser, config any) (int64, error) {
IndentChars := uint(config.(uint64))
f := indent.NewWriter(IndentChars, nil)
if _, err := io.Copy(f, r); err != nil { // streamable?
return 0, err
}
// f.Close()
return io.Copy(w, bytes.NewReader(f.Bytes()))
}