41 lines
835 B
JavaScript
41 lines
835 B
JavaScript
'use strict';
|
|
|
|
function createIndentedWriter(indent = ' ') {
|
|
let col = 0;
|
|
function width() {
|
|
return process.stdout.columns || 80;
|
|
}
|
|
function write(text) {
|
|
const str = String(text ?? '');
|
|
for (const ch of str) {
|
|
if (col === 0) {
|
|
process.stdout.write(indent);
|
|
col = indent.length;
|
|
}
|
|
if (ch === '\r') continue;
|
|
if (ch === '\n') {
|
|
process.stdout.write('\n');
|
|
col = 0;
|
|
continue;
|
|
}
|
|
process.stdout.write(ch);
|
|
col += 1;
|
|
if (col >= width()) {
|
|
process.stdout.write('\n');
|
|
col = 0;
|
|
}
|
|
}
|
|
}
|
|
function writeLine(text = '') {
|
|
write(text);
|
|
process.stdout.write('\n');
|
|
col = 0;
|
|
}
|
|
function reset() {
|
|
col = 0;
|
|
}
|
|
return { write, writeLine, reset };
|
|
}
|
|
|
|
module.exports = { createIndentedWriter };
|