|
| 1 | +// @flow strict |
| 2 | + |
| 3 | +import wrapAnsiNewLine from "wrap-ansi"; |
| 4 | +// this module is helpful for dealing with ansi characters, but it returns a |
| 5 | +// string with embedded new lines. We need it as an array, so we'll split it here |
| 6 | +const wrapAnsi = (input: string, columns: number): Array<string> => |
| 7 | + wrapAnsiNewLine(input, columns).split("\n"); |
| 8 | + |
| 9 | +class Border { |
| 10 | + vertical: ?string; |
| 11 | + horizontal: ?string; |
| 12 | + cornerTopLeft: ?string; |
| 13 | + cornerTopRight: ?string; |
| 14 | + cornerBottomLeft: ?string; |
| 15 | + cornerBottomRight: ?string; |
| 16 | + |
| 17 | + constructor({ |
| 18 | + vertical, |
| 19 | + horizontal, |
| 20 | + cornerTopLeft, |
| 21 | + cornerTopRight, |
| 22 | + cornerBottomLeft, |
| 23 | + cornerBottomRight |
| 24 | + }) { |
| 25 | + this.vertical = vertical; |
| 26 | + this.horizontal = horizontal; |
| 27 | + this.cornerTopLeft = cornerTopLeft; |
| 28 | + this.cornerTopRight = cornerTopRight; |
| 29 | + this.cornerBottomLeft = cornerBottomLeft; |
| 30 | + this.cornerBottomRight = cornerBottomRight; |
| 31 | + } |
| 32 | + |
| 33 | + horizontalWidth(): number { |
| 34 | + return ( |
| 35 | + Math.max( |
| 36 | + this.vertical ? this.vertical.length : 0, |
| 37 | + this.cornerTopLeft ? this.cornerTopLeft.length : 0, |
| 38 | + this.cornerBottomLeft ? this.cornerBottomLeft.length : 0 |
| 39 | + ) + |
| 40 | + Math.max( |
| 41 | + this.vertical ? this.vertical.length : 0, |
| 42 | + this.cornerTopRight ? this.cornerTopRight.length : 0, |
| 43 | + this.cornerBottomRight ? this.cornerBottomRight.length : 0 |
| 44 | + ) |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + verticalHeight(): number { |
| 49 | + return ( |
| 50 | + Math.max( |
| 51 | + this.horizontal ? this.horizontal.length : 0, |
| 52 | + this.cornerTopLeft ? this.cornerTopLeft.length : 0, |
| 53 | + this.cornerTopRight ? this.cornerTopRight.length : 0 |
| 54 | + ) + |
| 55 | + Math.max( |
| 56 | + this.horizontal ? this.horizontal.length : 0, |
| 57 | + this.cornerBottomLeft ? this.cornerBottomLeft.length : 0, |
| 58 | + this.cornerBottomRight ? this.cornerBottomRight.length : 0 |
| 59 | + ) |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
| 63 | +export class Section { |
| 64 | + orientation: "vertical" | "horizontal"; |
| 65 | + align: "left" | "center" | "right"; |
| 66 | + children: Array<Section | Text | Break> = []; |
| 67 | + border: Border; |
| 68 | + static type: "div" = "div"; |
| 69 | + |
| 70 | + constructor({ |
| 71 | + useHorizontalOrientation = false, |
| 72 | + align = "left", |
| 73 | + border = {} |
| 74 | + }: { |
| 75 | + useHorizontalOrientation: boolean, |
| 76 | + align: "left" | "center" | "right", |
| 77 | + border: { |
| 78 | + vertical?: string, |
| 79 | + horizontal?: string, |
| 80 | + cornerTopLeft?: string, |
| 81 | + cornerTopRight?: string, |
| 82 | + cornerBottomLeft?: string, |
| 83 | + cornerBottomRight?: string |
| 84 | + } |
| 85 | + }) { |
| 86 | + this.orientation = useHorizontalOrientation ? "horizontal" : "vertical"; |
| 87 | + this.align = align; |
| 88 | + this.border = new Border(border); |
| 89 | + } |
| 90 | + |
| 91 | + convertTextToArray(text: Text, totalWidth: number): Array<string> { |
| 92 | + return wrapAnsi(text.text, totalWidth - this.border.horizontalWidth()); |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +export class Text { |
| 97 | + text: string; |
| 98 | + |
| 99 | + constructor(text: string) { |
| 100 | + this.text = text; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +export class Break { |
| 105 | + static type: "br" = "br"; |
| 106 | +} |
0 commit comments