| Keith Rarick | 859bbfb | 2012-05-24 00:44:59 | [diff] [blame] | 1 | package text |
| 2 | |
| Keith Rarick | a130968 | 2013-05-03 00:09:58 | [diff] [blame] | 3 | import ( |
| 4 | "io" |
| 5 | ) |
| 6 | |
| Keith Rarick | e605cd5 | 2012-05-25 22:35:05 | [diff] [blame] | 7 | // Indent inserts prefix at the beginning of each non-empty line of s. The |
| 8 | // end-of-line marker is NL. |
| Keith Rarick | 859bbfb | 2012-05-24 00:44:59 | [diff] [blame] | 9 | func Indent(s, prefix string) string { |
| 10 | return string(IndentBytes([]byte(s), []byte(prefix))) |
| 11 | } |
| 12 | |
| 13 | // IndentBytes inserts prefix at the beginning of each non-empty line of b. |
| 14 | // The end-of-line marker is NL. |
| 15 | func IndentBytes(b, prefix []byte) []byte { |
| 16 | var res []byte |
| 17 | bol := true |
| 18 | for _, c := range b { |
| 19 | if bol && c != '\n' { |
| 20 | res = append(res, prefix...) |
| 21 | } |
| 22 | res = append(res, c) |
| 23 | bol = c == '\n' |
| 24 | } |
| 25 | return res |
| 26 | } |
| Keith Rarick | a130968 | 2013-05-03 00:09:58 | [diff] [blame] | 27 | |
| 28 | // Writer indents each line of its input. |
| 29 | type indentWriter struct { |
| 30 | w io.Writer |
| 31 | bol bool |
| 32 | pre [][]byte |
| 33 | sel int |
| Keith Rarick | 6b25969 | 2013-05-03 00:51:13 | [diff] [blame] | 34 | off int |
| Keith Rarick | a130968 | 2013-05-03 00:09:58 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // NewIndentWriter makes a new write filter that indents the input |
| 38 | // lines. Each line is prefixed in order with the corresponding |
| 39 | // element of pre. If there are more lines than elements, the last |
| 40 | // element of pre is repeated for each subsequent line. |
| 41 | func NewIndentWriter(w io.Writer, pre ...[]byte) io.Writer { |
| 42 | return &indentWriter{ |
| 43 | w: w, |
| 44 | pre: pre, |
| 45 | bol: true, |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // The only errors returned are from the underlying indentWriter. |
| 50 | func (w *indentWriter) Write(p []byte) (n int, err error) { |
| 51 | for _, c := range p { |
| 52 | if w.bol { |
| Keith Rarick | 6b25969 | 2013-05-03 00:51:13 | [diff] [blame] | 53 | var i int |
| 54 | i, err = w.w.Write(w.pre[w.sel][w.off:]) |
| 55 | w.off += i |
| 56 | if err != nil { |
| Keith Rarick | a130968 | 2013-05-03 00:09:58 | [diff] [blame] | 57 | return n, err |
| 58 | } |
| 59 | } |
| 60 | _, err = w.w.Write([]byte{c}) |
| 61 | if err != nil { |
| 62 | return n, err |
| 63 | } |
| 64 | n++ |
| 65 | w.bol = c == '\n' |
| Keith Rarick | 6b25969 | 2013-05-03 00:51:13 | [diff] [blame] | 66 | if w.bol { |
| 67 | w.off = 0 |
| 68 | if w.sel < len(w.pre)-1 { |
| 69 | w.sel++ |
| 70 | } |
| Keith Rarick | a130968 | 2013-05-03 00:09:58 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | return n, nil |
| 74 | } |