• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import fs from 'fs';
17
18export class ArkCodeBuffer {
19    output: string[] = [];
20    indent: string = '';
21
22    constructor(indent: string = '') {
23        this.indent = indent;
24    }
25
26    public write(s: string): this {
27        this.output.push(s);
28        return this;
29    }
30
31    public writeLine(s: string): this {
32        this.write(s);
33        this.write('\n');
34        return this;
35    }
36
37    public writeSpace(s: string): this {
38        if (s.length === 0) {
39            return this;
40        }
41        this.write(s);
42        this.write(' ');
43        return this;
44    }
45
46    public writeStringLiteral(s: string): this {
47        this.write(`'${s}'`);
48        return this;
49    }
50
51    public writeIndent(): this {
52        this.write(this.indent);
53        return this;
54    }
55
56    public incIndent(): this {
57        this.indent += '  ';
58        return this;
59    }
60
61    public decIndent(): this {
62        if (this.indent.length >= 2) {
63            this.indent = this.indent.substring(0, this.indent.length - 2);
64        }
65        return this;
66    }
67
68    public getIndent(): string {
69        return this.indent;
70    }
71
72    public toString(): string {
73        return this.output.join('');
74    }
75
76    public clear(): void {
77        this.output = [];
78    }
79}
80
81export class ArkStream extends ArkCodeBuffer {
82    streamOut: fs.WriteStream;
83
84    constructor(streamOut: fs.WriteStream) {
85        super('');
86        this.streamOut = streamOut;
87    }
88
89    public write(s: string): this {
90        this.streamOut.write(s);
91        return this;
92    }
93
94    public close(): void {
95        this.streamOut.close();
96    }
97}
98