blob: 6fddd09ee0f7883258029fb47589e8729f726721 [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 Sharkey328ebf22013-03-21 18:09:39 -070071 public void printHexPair(String key, int value) {
72 print(key + "=0x" + Integer.toHexString(value) + " ");
73 }
74
Jeff Sharkey63abc372012-01-11 18:38:16 -080075 @Override
Jeff Browncbad9762012-09-04 21:57:59 -070076 public void write(char[] buf, int offset, int count) {
Jeff Sharkey71cb4462013-01-30 16:35:04 -080077 final int indentLength = mIndentBuilder.length();
Jeff Browncbad9762012-09-04 21:57:59 -070078 final int bufferEnd = offset + count;
79 int lineStart = offset;
80 int lineEnd = offset;
Jeff Sharkey71cb4462013-01-30 16:35:04 -080081
82 // March through incoming buffer looking for newlines
Jeff Browncbad9762012-09-04 21:57:59 -070083 while (lineEnd < bufferEnd) {
84 char ch = buf[lineEnd++];
Jeff Sharkey71cb4462013-01-30 16:35:04 -080085 mCurrentLength++;
Jeff Browncbad9762012-09-04 21:57:59 -070086 if (ch == '\n') {
Jeff Sharkey71cb4462013-01-30 16:35:04 -080087 maybeWriteIndent();
Jeff Browncbad9762012-09-04 21:57:59 -070088 super.write(buf, lineStart, lineEnd - lineStart);
89 lineStart = lineEnd;
90 mEmptyLine = true;
Jeff Sharkey71cb4462013-01-30 16:35:04 -080091 mCurrentLength = 0;
92 }
93
94 // Wrap if we've pushed beyond line length
95 if (mWrapLength > 0 && mCurrentLength >= mWrapLength - indentLength) {
96 if (!mEmptyLine) {
97 // Give ourselves a fresh line to work with
98 super.write('\n');
99 mEmptyLine = true;
100 mCurrentLength = lineEnd - lineStart;
101 } else {
102 // We need more than a dedicated line, slice it hard
103 maybeWriteIndent();
104 super.write(buf, lineStart, lineEnd - lineStart);
105 super.write('\n');
106 mEmptyLine = true;
107 lineStart = lineEnd;
108 mCurrentLength = 0;
109 }
Jeff Browncbad9762012-09-04 21:57:59 -0700110 }
111 }
112
113 if (lineStart != lineEnd) {
Jeff Sharkey71cb4462013-01-30 16:35:04 -0800114 maybeWriteIndent();
Jeff Browncbad9762012-09-04 21:57:59 -0700115 super.write(buf, lineStart, lineEnd - lineStart);
116 }
Jeff Sharkey63abc372012-01-11 18:38:16 -0800117 }
118
Jeff Sharkey71cb4462013-01-30 16:35:04 -0800119 private void maybeWriteIndent() {
Jeff Sharkey63abc372012-01-11 18:38:16 -0800120 if (mEmptyLine) {
121 mEmptyLine = false;
Jeff Sharkey71cb4462013-01-30 16:35:04 -0800122 if (mIndentBuilder.length() != 0) {
123 if (mCurrentIndent == null) {
124 mCurrentIndent = mIndentBuilder.toString().toCharArray();
Jeff Browncbad9762012-09-04 21:57:59 -0700125 }
Jeff Sharkey71cb4462013-01-30 16:35:04 -0800126 super.write(mCurrentIndent, 0, mCurrentIndent.length);
Jeff Browncbad9762012-09-04 21:57:59 -0700127 }
Jeff Sharkey63abc372012-01-11 18:38:16 -0800128 }
Jeff Sharkey63abc372012-01-11 18:38:16 -0800129 }
130}