1 | /*
|
2 | * Licensed under the Apache License, Version 2.0 (the "License");
|
3 | * you may not use this file except in compliance with the License.
|
4 | * You may obtain a copy of the License at
|
5 | *
|
6 | * http://www.apache.org/licenses/LICENSE-2.0
|
7 | *
|
8 | * Unless required by applicable law or agreed to in writing, software
|
9 | * distributed under the License is distributed on an "AS IS" BASIS,
|
10 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
11 | * See the License for the specific language governing permissions and
|
12 | * limitations under the License.
|
13 | */
|
14 |
|
15 | ;
|
16 |
|
17 | /**
|
18 | * Writer buffers text to be written in memory. It handles simple
|
19 | * indentation and tracks the number of lines written.
|
20 | * @private
|
21 | * @class
|
22 | * @memberof module:concerto-core
|
23 | */
|
24 | class Writer {
|
25 | /**
|
26 | * Create a Writer.
|
27 | */
|
28 | constructor() {
|
29 | this.beforeBuffer = '';
|
30 | this.buffer = '';
|
31 | this.linesWritten = 0;
|
32 | }
|
33 |
|
34 | /**
|
35 | * Writes text to the start of the buffer
|
36 | * @param {int} tabs - the number of tabs to use
|
37 | * @param {string} text - the text to write
|
38 | */
|
39 | writeBeforeLine(tabs, text) {
|
40 | for(let n=0; n < tabs; n++) {
|
41 | this.beforeBuffer += ' ';
|
42 | }
|
43 | this.beforeBuffer += text;
|
44 | this.beforeBuffer += '\n';
|
45 | this.linesWritten++;
|
46 | }
|
47 |
|
48 | /**
|
49 | * Append text to the buffer
|
50 | * @param {int} tabs - the number of tabs to use
|
51 | * @param {string} text - the text to write
|
52 | */
|
53 | writeLine(tabs, text) {
|
54 | for(let n=0; n < tabs; n++) {
|
55 | this.write(' ');
|
56 | }
|
57 | this.write(text);
|
58 | this.write('\n');
|
59 | this.linesWritten++;
|
60 | }
|
61 |
|
62 | /**
|
63 | * Returns the number of lines that have been written to the buffer.
|
64 | * @return {int} the number of lines written to the buffer.
|
65 | */
|
66 | getLineCount() {
|
67 | return this.linesWritten;
|
68 | }
|
69 |
|
70 |
|
71 | /**
|
72 | * Append text to the buffer, prepending tabs
|
73 | * @param {int} tabs - the number of tabs to use
|
74 | * @param {string} text - the text to write
|
75 | */
|
76 | writeIndented(tabs,text) {
|
77 | for(let n=0; n < tabs; n++) {
|
78 | this.write(' ');
|
79 | }
|
80 | this.write(text);
|
81 | }
|
82 |
|
83 | /**
|
84 | * Append text to the buffer (no automatic newline). The
|
85 | * text may contain newline, and these will increment the linesWritten
|
86 | * counter.
|
87 | * @param {string} msg - the text to write
|
88 | */
|
89 | write(msg) {
|
90 | if(typeof msg !== 'string' ) {
|
91 | throw new Error('Can only append strings. Argument ' + msg + ' has type ' + typeof msg);
|
92 | }
|
93 |
|
94 | this.buffer += msg;
|
95 | this.linesWritten += msg.split(/\r\n|\r|\n/).length;
|
96 | }
|
97 |
|
98 | /**
|
99 | * Returns the text that has been buffered in this Writer.
|
100 | * @return {string} the buffered text.
|
101 | */
|
102 | getBuffer() {
|
103 | return this.beforeBuffer + this.buffer;
|
104 | }
|
105 |
|
106 | /**
|
107 | * Empties the underyling buffer and resets the line count.
|
108 | */
|
109 | clearBuffer() {
|
110 | this.beforeBuffer = '';
|
111 | this.buffer = '';
|
112 | this.linesWritten = 0;
|
113 | }
|
114 | }
|
115 |
|
116 | module.exports = Writer;
|