diff --git a/treegen/src/util.ts b/treegen/src/util.ts new file mode 100644 index 000000000..55ac4e2e2 --- /dev/null +++ b/treegen/src/util.ts @@ -0,0 +1,49 @@ + +function isWhiteSpace(ch: string) { + return /[\r\t ]/.test(ch); +} + +export interface FileWriterOptions { + indentStr?: string; + startIndent?: number; + indentWidth?: number; +} + +export class FileWriter { + + public currentText = ''; + + private atBlankLine = true; + private currentIndent: number; + private indentStr: string; + private indentWidth: number; + + constructor(opts: FileWriterOptions = {}) { + this.indentStr = opts.indentStr ?? ' '; + this.indentWidth = opts.indentWidth ?? 2; + this.currentIndent = (opts.startIndent ?? 0) * this.indentWidth; + } + + public indent(count = 1) { + this.currentIndent += this.indentWidth * count; + } + + public dedent(count = 1) { + this.currentIndent -= this.indentWidth * count; + } + + public write(str: string) { + for (const ch of str) { + if (ch === '\n') { + this.atBlankLine = true; + } else if (!(this.atBlankLine && isWhiteSpace(ch))) { + if (this.atBlankLine) { + this.currentText += this.indentStr.repeat(this.currentIndent) + } + this.atBlankLine = false; + } + this.currentText += ch; + } + } + +}