blob: d01a817b0f12c388733797504ffa1a2b5a24fe3e [file] [log] [blame]
Jeff Sharkey63abc372012-01-11 18:38:16 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.internal.util;
18
19import java.io.PrintWriter;
20import java.io.Writer;
21
22/**
23 * Lightweight wrapper around {@link PrintWriter} that automatically indents
Jeff Sharkey71cb4462013-01-30 16:35:04 -080024 * newlines based on internal state. It also automatically wraps long lines
25 * based on given line length.
26 * <p>
27 * Delays writing indent until first actual write on a newline, enabling indent
28 * modification after newline.
Jeff Sharkey63abc372012-01-11 18:38:16 -080029 */
30public class IndentingPrintWriter extends PrintWriter {
Jeff Sharkey71cb4462013-01-30 16:35:04 -080031 private final String mSingleIndent;
32 private final int mWrapLength;
Jeff Sharkey63abc372012-01-11 18:38:16 -080033
Jeff Sharkey71cb4462013-01-30 16:35:04 -080034 /** Mutable version of current indent */
35 private StringBuilder mIndentBuilder = new StringBuilder();
36 /** Cache of current {@link #mIndentBuilder} value */
37 private char[] mCurrentIndent;
38 /** Length of current line being built, excluding any indent */
39 private int mCurrentLength;
40
41 /**
42 * Flag indicating if we're currently sitting on an empty line, and that
43 * next write should be prefixed with the current indent.
44 */
Jeff Sharkey63abc372012-01-11 18:38:16 -080045 private boolean mEmptyLine = true;
46
Jeff Sharkey71cb4462013-01-30 16:35:04 -080047 public IndentingPrintWriter(Writer writer, String singleIndent) {
48 this(writer, singleIndent, -1);
49 }
50
51 public IndentingPrintWriter(Writer writer, String singleIndent, int wrapLength) {
Jeff Sharkey63abc372012-01-11 18:38:16 -080052 super(writer);
Jeff Sharkey71cb4462013-01-30 16:35:04 -080053 mSingleIndent = singleIndent;
54 mWrapLength = wrapLength;
Jeff Sharkey63abc372012-01-11 18:38:16 -080055 }
56
57 public void increaseIndent() {
Jeff Sharkey71cb4462013-01-30 16:35:04 -080058 mIndentBuilder.append(mSingleIndent);
59 mCurrentIndent = null;
Jeff Sharkey63abc372012-01-11 18:38:16 -080060 }
61
62 public void decreaseIndent() {
Jeff Sharkey71cb4462013-01-30 16:35:04 -080063 mIndentBuilder.delete(0, mSingleIndent.length());
64 mCurrentIndent = null;
Jeff Sharkey63abc372012-01-11 18:38:16 -080065 }
66
Jeff Sharkey2f036c52012-04-13 12:18:13 -070067 public void printPair(String key, Object value) {
68 print(key + "=" + String.valueOf(value) + " ");
69 }
70
Jeff Sharkey63abc372012-01-11 18:38:16 -080071 @Override
Jeff Browncbad9762012-09-04 21:57:59 -070072 public void write(char[] buf, int offset, int count) {
Jeff Sharkey71cb4462013-01-30 16:35:04 -080073 final int indentLength = mIndentBuilder.length();
Jeff Browncbad9762012-09-04 21:57:59 -070074 final int bufferEnd = offset + count;
75 int lineStart = offset;
76 int lineEnd = offset;
Jeff Sharkey71cb4462013-01-30 16:35:04 -080077
78 // March through incoming buffer looking for newlines
Jeff Browncbad9762012-09-04 21:57:59 -070079 while (lineEnd < bufferEnd) {
80 char ch = buf[lineEnd++];
Jeff Sharkey71cb4462013-01-30 16:35:04 -080081 mCurrentLength++;
Jeff Browncbad9762012-09-04 21:57:59 -070082 if (ch == '\n') {
Jeff Sharkey71cb4462013-01-30 16:35:04 -080083 maybeWriteIndent();
Jeff Browncbad9762012-09-04 21:57:59 -070084 super.write(buf, lineStart, lineEnd - lineStart);
85 lineStart = lineEnd;
86 mEmptyLine = true;
Jeff Sharkey71cb4462013-01-30 16:35:04 -080087 mCurrentLength = 0;
88 }
89
90 // Wrap if we've pushed beyond line length
91 if (mWrapLength > 0 && mCurrentLength >= mWrapLength - indentLength) {
92 if (!mEmptyLine) {
93 // Give ourselves a fresh line to work with
94 super.write('\n');
95 mEmptyLine = true;
96 mCurrentLength = lineEnd - lineStart;
97 } else {
98 // We need more than a dedicated line, slice it hard
99 maybeWriteIndent();
100 super.write(buf, lineStart, lineEnd - lineStart);
101 super.write('\n');
102 mEmptyLine = true;
103 lineStart = lineEnd;
104 mCurrentLength = 0;
105 }
Jeff Browncbad9762012-09-04 21:57:59 -0700106 }
107 }
108
109 if (lineStart != lineEnd) {
Jeff Sharkey71cb4462013-01-30 16:35:04 -0800110 maybeWriteIndent();
Jeff Browncbad9762012-09-04 21:57:59 -0700111 super.write(buf, lineStart, lineEnd - lineStart);
112 }
Jeff Sharkey63abc372012-01-11 18:38:16 -0800113 }
114
Jeff Sharkey71cb4462013-01-30 16:35:04 -0800115 private void maybeWriteIndent() {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800116 if (mEmptyLine) {
117 mEmptyLine = false;
Jeff Sharkey71cb4462013-01-30 16:35:04 -0800118 if (mIndentBuilder.length() != 0) {
119 if (mCurrentIndent == null) {
120 mCurrentIndent = mIndentBuilder.toString().toCharArray();
Jeff Browncbad9762012-09-04 21:57:59 -0700121 }
Jeff Sharkey71cb4462013-01-30 16:35:04 -0800122 super.write(mCurrentIndent, 0, mCurrentIndent.length);
Jeff Browncbad9762012-09-04 21:57:59 -0700123 }
Jeff Sharkey63abc372012-01-11 18:38:16 -0800124 }
Jeff Sharkey63abc372012-01-11 18:38:16 -0800125 }
126}