UNPKG

@teaui/core

Version:

A high-level terminal UI library for Node

54 lines 1.63 kB
import * as unicode from '@teaui/term'; import { View } from '../View.js'; import { Style } from '../Style.js'; import { Point, Size } from '../geometry.js'; import { Palette } from '../Palette.js'; export class Badge extends View { #text = ''; constructor(props = {}) { super(props); this.#update(props); } update(props) { this.#update(props); super.update(props); } #update({ text, purpose }) { this.#text = text ?? ''; if (purpose) { this.purpose = Palette[purpose]; } } naturalSize(_available) { if (this.#text === '') { return Size.zero; } const textWidth = unicode.lineWidth(this.#text); // 1 char left cap + text + 1 char right cap return new Size(textWidth + 2, 1); } #capStyle() { return new Style({ foreground: this.purpose.controlBackgroundColor, }); } #textStyle() { return new Style({ foreground: this.purpose.textColor, background: this.purpose.controlBackgroundColor, }); } render(viewport) { if (viewport.isEmpty || this.#text === '') { return; } const capStyle = this.#capStyle(); const textStyle = this.#textStyle(); viewport.write(LEFT_CAP, Point.zero, capStyle); viewport.write(this.#text, new Point(1, 0), textStyle); viewport.write(RIGHT_CAP, new Point(1 + unicode.lineWidth(this.#text), 0), capStyle); } } const LEFT_CAP = '▐'; const RIGHT_CAP = '▌'; //# sourceMappingURL=Badge.js.map