/**
 * @license
 * Copyright 2022-2026 Greg Lauckhart <greg@lauckhart.com>
 * SPDX-License-Identifier: Apache-2.0
 */
import { ContiguousOutputSegment } from "./contiguous-output-segment.js";
class Truncator {
  #target;
  #addNewline;
  constructor(target, options = {}) {
    const { addNewline } = options;
    this.#target = target;
    this.#addNewline = addNewline === void 0 ? true : false;
  }
  get state() {
    return this.#target.state;
  }
  write(...text) {
    const width = this.#target.state.remainingWidth;
    if (width === void 0) {
      this.#write(...text);
      return;
    }
    const segment = new ContiguousOutputSegment(...text);
    if (segment.width <= width) {
      this.#write(...segment.tokens);
    } else {
      const scanner = segment.scan();
      this.#write(...scanner.take(width - 1), { kind: "nonbreaking", str: "\u2026", width: 1 });
      for (const token of scanner) {
        if (token.kind === "breaking" || token.kind === "nonbreaking") {
          continue;
        }
        this.#write(token);
      }
    }
  }
  close() {
    this.#target.close();
  }
  #write(...text) {
    if (this.#addNewline) {
      this.#target.write(...text, { kind: "newline" });
    } else {
      this.#target.write(...text);
    }
  }
}
export {
  Truncator
};
//# sourceMappingURL=truncator.js.map
