public printTree()

in packages/oo-ascii-tree/src/ascii-tree.ts [30:87]


  public printTree(output: Printer = process.stdout) {
    let ancestorsPrefix = '';

    for (const parent of this.ancestors) {
      // -1 represents a "hidden" root, and so it's children
      // will all appear as roots (level 0).
      if (parent.level <= 0) {
        continue;
      }

      if (parent.last) {
        ancestorsPrefix += '  ';
      } else {
        ancestorsPrefix += ' │';
      }
    }

    let myPrefix = '';
    let multilinePrefix = '';
    if (this.level > 0) {
      if (this.last) {
        if (!this.empty) {
          myPrefix += ' └─┬ ';
          multilinePrefix += ' └─┬ ';
        } else {
          myPrefix += ' └── ';
          multilinePrefix = '     ';
        }
      } else {
        if (!this.empty) {
          myPrefix += ' ├─┬ ';
          multilinePrefix += ' │ │ ';
        } else {
          myPrefix += ' ├── ';
          multilinePrefix += ' │   ';
        }
      }
    }

    if (this.text) {
      output.write(ancestorsPrefix);
      output.write(myPrefix);
      const lines = this.text.split('\n');
      output.write(lines[0]);
      output.write('\n');

      for (const line of lines.splice(1)) {
        output.write(ancestorsPrefix);
        output.write(multilinePrefix);
        output.write(line);
        output.write('\n');
      }
    }

    for (const child of this._children) {
      child.printTree(output);
    }
  }