idlize
1import { indentedBy, stringOrNone } from "./util"2
3/*
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
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
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.
16*/
17export class IndentedPrinter {18constructor (private output: string[] = []) {}19private indent = 020
21print(value: stringOrNone) {22if (value) this.output.push(this.indented(value))23}24
25pushIndent() {26this.indent++27}28popIndent() {29this.indent--30}31
32private indented(input: string): string {33return indentedBy(input, this.indent)34}35
36getOutput(): string[] {37return this.output38}39}
40
41export class IndentedPrinterWithHeader extends IndentedPrinter {42header = new IndentedPrinter()43body = new IndentedPrinter()44
45constructor() {46super()47}48
49print(value: stringOrNone) {50this.body.print(value)51}52
53printHeader(value: stringOrNone) {54this.header.print(value)55}56
57pushIndent() {58this.body.pushIndent()59}60popIndent() {61this.body.popIndent()62}63
64pushIndentHeader() {65this.header.pushIndent()66}67popIndentHeader() {68this.header.popIndent()69}70
71getOutput(): string[] {72return this.header.getOutput().concat(this.body.getOutput())73}74}