Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | 1x 1x 1x 1x 1x 638x 638x 638x 638x 638x 638x 638x 638x 638x 1x 192x 192x 192x 192x 192x 192x 192x 192x 192x 1x 199x 199x 199x 1x 174x 174x 174x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1174x 1174x 1174x 1x 1x 1598x 1602x 63x 63x 1539x 1540x 1535x 1535x 4x 4x 4x 4x 4x 4x 4x 1x 1x 573x 577x 231x 231x 346x 355x 335x 335x 11x 11x 11x 11x 1x 1x 254x 254x 254x 1x 1x 286x 286x 286x 286x 286x 286x 286x 1x 461x 461x 461x 461x 461x 461x 461x 461x 461x 461x | import {type NonEmptyArray} from '#Array'
import type {EndoOf} from '#Function'
import {filterDefined} from '#Record'
import {
fillColumns,
fillRows,
segmentSlice,
stringWidth,
unwords,
} from '#String'
import {pipe} from '#util'
import type {
Axis,
AxisString,
HorizontalDirection,
VerticalDirection,
} from '../direction.js'
/**
* @category drawing
*/
export type HStrut = BaseStrut<'horizontal'>
/**
* @category drawing
*/
export type VStrut = BaseStrut<'vertical'>
/**
* @category drawing
*/
export type HStruts = Record<HorizontalDirection, HStrut>
/**
* @category drawing
*/
export type VStruts = Record<VerticalDirection, VStrut>
/**
* @category drawing
*/
export interface BaseStrut<A extends Axis> {
axis: A
body: NonEmptyArray<string>
prefix: AxisString<A>
suffix: AxisString<A>
}
/**
* A record of strut per direction: horizontal struts on the horizontal
* directions, and vertical ones on the vertical directions.
* @category drawing
*/
export interface Struts extends HStruts, VStruts {}
/**
* Build a horizontal strut from a non-empty array of its glyph and an
* prefix/suffix strings.
* @category drawing
* @function
*/
export const HStrut = (
body: NonEmptyArray<string> = [' '],
prefix = '',
suffix = '',
): HStrut => ({
axis: 'horizontal',
prefix,
body,
suffix,
})
/**
* @category drawing
* @function
*/
export const VStrut = (
body: NonEmptyArray<string> = [''],
prefix: string[] = [],
suffix: string[] = [],
): VStrut => ({
axis: 'vertical',
prefix,
body,
suffix,
})
/**
* Build a pair of left/right horizontal struts form the given horizontal struts.
* @category drawing
* @function
*/
export const HStruts = (left: HStrut, right = left): HStruts => ({
left,
right,
})
/**
* Build a pair of top/bottom vertical struts form the given vertical struts.
* @category drawing
* @function
*/
export const VStruts = (top: VStrut, bottom = top): VStruts => ({
top,
bottom,
})
HStrut.space = HStrut([' '])
VStrut.empty = VStrut([''])
HStruts.space = HStruts(HStrut.space)
VStruts.empty = VStruts(VStrut.empty)
export const defaultHStrut: HStrut = HStrut.space
export const defaultVStrut: VStrut = VStrut.empty
const defaultPartStruts: Struts = {
top: defaultVStrut,
right: defaultHStrut,
bottom: defaultVStrut,
left: defaultHStrut,
}
/**
* Upgrade a partial set of struts per direction into a total one.
* @category drawing
* @function
*/
export const normalizeStruts = (struts?: Partial<Struts>): Struts => ({
...defaultPartStruts,
...(filterDefined(struts ?? {}) as Struts),
})
HStrut.fill =
({prefix, body, suffix}: HStrut) =>
(available: number): string => {
if (available === 0) {
return ''
}
const forBody = available - stringWidth(prefix) - stringWidth(suffix)
if (forBody >= 0) {
return prefix + pipe(body, unwords, fillColumns(forBody)) + suffix
}
const forSuffix = Math.floor(available / 2)
const forPrefix = available - forSuffix
return unwords([
...segmentSlice(0, forPrefix)(prefix),
...segmentSlice(0, forSuffix)(suffix),
])
}
VStrut.fill =
({prefix, body, suffix}: VStrut) =>
(available: number): string[] => {
if (available === 0) {
return []
}
const forBody = available - prefix.length - suffix.length
if (forBody >= 0) {
return [...prefix, ...fillRows(forBody)(body), ...suffix]
}
const forSuffix = Math.floor(available / 2)
const forPrefix = available - forSuffix
return [...prefix.slice(0, forPrefix), ...suffix.slice(0, forSuffix)]
}
HStruts.fill =
({left, right}: HStruts) =>
(availableLeft: number, availableRight: number): EndoOf<string> =>
line =>
HStrut.fill(left)(availableLeft) + line + HStrut.fill(right)(availableRight)
VStruts.fill =
({top, bottom}: VStruts) =>
([availableTop, availableBottom]: [number, number]) =>
(lines: string[]) =>
[
...VStrut.fill(top)(availableTop),
...lines,
...VStrut.fill(bottom)(availableBottom),
] as NonEmptyArray<string>
/**
* Create {@link Struts} from horizontal and vertical struts. If only a pair
* is given it will be used for all directions. If none are given returns the
* default struts of empty line and space.
* @category drawing
* @function
*/
export const Struts = (
top: VStrut = defaultVStrut,
right: HStrut = defaultHStrut,
bottom: VStrut = top,
left: HStrut = right,
): Struts => ({
top,
right,
bottom,
left,
})
|