1
import { indentedBy, stringOrNone } from "./util"
4
* Copyright (c) 2024 Huawei Device Co., Ltd.
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
* you may not use this file except in compliance with the License.
7
* You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
export class IndentedPrinter {
18
constructor (private output: string[] = []) {}
21
print(value: stringOrNone): void {
22
if (value != undefined) this.output.push(this.indented(value))
31
indentDepth(): number {
35
append(printer: IndentedPrinter): void {
36
this.output = [...this.output, ...printer.output]
39
private indented(input: string): string {
40
return indentedBy(input, this.indent)
43
getOutput(): string[] {
48
export class IndentedPrinterWithHeader extends IndentedPrinter {
49
header = new IndentedPrinter()
50
body = new IndentedPrinter()
56
print(value: stringOrNone) {
57
this.body.print(value)
60
printHeader(value: stringOrNone) {
61
this.header.print(value)
65
this.body.pushIndent()
72
this.header.pushIndent()
75
this.header.popIndent()
78
getOutput(): string[] {
79
return this.header.getOutput().concat(this.body.getOutput())