| 35 | | BoxStyle |
| 36 | |
| 37 | function embedTextInBorder( |
| 38 | borderLine: string, |
| 39 | text: string, |
| 40 | align: 'start' | 'end' | 'center', |
| 41 | offset: number = 0, |
| 42 | borderChar: string, |
| 43 | ): [before: string, text: string, after: string] { |
| 44 | const textLength = stringWidth(text) |
| 45 | const borderLength = borderLine.length |
| 46 | |
| 47 | if (textLength >= borderLength - 2) { |
| 48 | return ['', text.substring(0, borderLength), ''] |
| 49 | } |
| 50 | |
| 51 | let position: number |
| 52 | if (align === 'center') { |
| 53 | position = Math.floor((borderLength - textLength) / 2) |
| 54 | } else if (align === 'start') { |
| 55 | position = offset + 1 // +1 to account for corner character |
| 56 | } else { |
| 57 | // align === 'end' |
| 58 | position = borderLength - textLength - offset - 1 // -1 for corner character |
| 59 | } |
| 60 | |
| 61 | // Ensure position is valid |
| 62 | position = Math.max(1, Math.min(position, borderLength - textLength - 1)) |
| 63 | |
| 64 | const before = borderLine.substring(0, 1) + borderChar.repeat(position - 1) |
| 65 | const after = |
| 66 | borderChar.repeat(borderLength - position - textLength - 1) + |
| 67 | borderLine.substring(borderLength - 1) |
| 68 | |
| 69 | return [before, text, after] |
| 70 | } |
| 71 | |
| 72 | function styleBorderLine( |
| 73 | line: string, |