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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | 1x 1x 1x 1x 1x 21x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 60x 1x 1x 57x 43x 14x 1x 1x 28x 28x 28x 28x 28x 28x 28x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 26x 26x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 1x | import {Array, pipe} from '#util'
import {type EndoOf, dual} from '#Function'
import {
type Part,
column as columnPart,
empty as emptyPart,
row as rowPart,
text as textPart,
} from '../part.js'
import type {DirectedPad} from './pad.js'
import type {HorizontalAlignment} from '../align.js'
/**
* A zero-width single line height part.
* @category drawing
*/
export const emptyTextPart: Part = textPart('')
const _joinText = (xs: string[], separator?: string): Part =>
pipe(xs, Array.join(separator ?? ''), textPart)
/**
* A text part that joins the given string list horizontally, separating items
* using the given separator.
*
* At the key `curried` you will find a curried version that takes the optional
* separator as first argument.
* @example
* import {Draw} from 'effect-tree'
* const {drawPart, joinText} = Draw
*
* const part = joinText(['A', 'B', 'C'], '.')
*
* expect(drawPart(part)).toEqual(['A.B.C'])
* @category drawing
* @function
*/
export const joinText: {
(xs: string[], separator?: string): Part
curried: (separator?: string) => (xs: string[]) => Part
} = Object.assign(
(xs: string[], separator?: string): Part => _joinText(xs, separator),
{
curried:
(separator?: string) =>
(xs: string[]): Part =>
_joinText(xs, separator),
},
)
const _stackText = (
xs: string[],
hAlign: HorizontalAlignment = 'center',
): Part => pipe(xs, Array.map(textPart), columnPart[hAlign])
/**
* A column part that joins the given string list vertically.
*
* At the key `curried` you will find a version that accepts the horizontal
* alignment as the only argument in its first argument list.
*
* At the key `rest` you will find a version that accepts the strings as a list
* of arguments.
* @example
* import {Draw} from 'effect-tree'
* const {drawPart, stackText} = Draw
*
* const part = stackText.rest('A', 'B', 'C')
*
* expect(drawPart(part)).toEqual([
* 'A',
* 'B',
* 'C',
* ])
* @category drawing
* @function
*/
export const stackText = Object.assign(_stackText, {
curried:
(hAlign?: HorizontalAlignment) =>
(xs: string[]): Part =>
_stackText(xs, hAlign),
rest: (...xs: string[]): Part => _stackText(xs),
})
/**
* A text part that is `width` wide composed entirely of the given string
* repeated or sliced.
* @example
* import {Draw} from 'effect-tree'
* const {drawPart, repeatText} = Draw
*
* const part = repeatText(3, 'A')
*
* expect(drawPart(part)).toEqual(['AAA'])
* @category drawing
* @function
*/
export const repeatText: {
(width: number, repeat: string): Part
(repeat: string): (width: number) => Part
} = dual(
2,
(width: number, repeat: string): Part =>
width === 0 ? emptyPart : pipe(repeat, Array.replicate(width), joinText),
)
/**
* Returns the {@link empty | empty part} if the given height is zero, else a
* zero-width column at the given height.
* @example
* import {Draw} from 'effect-tree'
* const {vIndent, drawPart} = Draw
*
* expect(drawPart(vIndent(2))).toEqual(['', ''])
* expect(drawPart(vIndent(0))).toEqual([])
* @category drawing
* @function
*/
export const vIndent = (height: number): Part =>
height === 0
? emptyPart
: pipe(emptyTextPart, Array.replicate(height), columnPart.center)
/**
* Pad a part with left and right padding filled with the optional padding, by
* default the space character.
*
* At the keys `left` and `right` you will find versions that pad only a single
* side of the given part.
*
* See {@link vPadPart} for the vertical version.
* @example
* import {Draw} from 'effect-tree'
* const {drawPart, hPadPart, text} = Draw
*
* const part = text('foo')
*
* const padded = hPadPart('←', '→')(1, 2)(part)
*
* expect(drawPart(padded)).toEqual(['←foo→→'])
* @category drawing
* @function
*/
export const hPadPart = Object.assign(
(fillLeft = ' ', fillRight = ' ') =>
(padLeft = 1, padRight = padLeft): EndoOf<Part> =>
self =>
rowPart.middle.center([
repeatText(padLeft, fillLeft),
self,
repeatText(padRight, fillRight),
]),
{
left:
(fillLeft = ' ', padLeft = 0): EndoOf<Part> =>
(self: Part): Part =>
rowPart.middle.center([repeatText(padLeft, fillLeft), self]),
right:
(fillRight = ' ', padRight = 0): EndoOf<Part> =>
(self: Part): Part =>
rowPart.middle.center([self, repeatText(padRight, fillRight)]),
},
)
/**
* Pad a part with top and bottom padding filled with spaces.
*
* At the keys `top` and `bottom` you will find versions that pad only a single
* side of the given part.
*
* See {@link hPadPart} for the horizontal version.
* @example
* import {Draw} from 'effect-tree'
* const {drawPart, vPadPart, text} = Draw
*
* const part = text('foo')
*
* const padded = vPadPart(1)(part)
*
* expect(drawPart(padded)).toEqual([
* ' ',
* 'foo',
* ' ',
* ])
* @category drawing
* @function
*/
export const vPadPart = Object.assign(
(padTop = 1, padBottom = padTop): EndoOf<Part> =>
self =>
columnPart.center([vIndent(padTop), self, vIndent(padBottom)]),
{
top: (self: Part, pad = 0): Part => columnPart.center([vIndent(pad), self]),
bottom: (self: Part, pad = 0): Part =>
columnPart.center([self, vIndent(pad)]),
},
)
/**
* Pad a part with spaces in all directions.
* @example
* import {Draw} from 'effect-tree'
* const {drawPart, padPart, text} = Draw
*
* const part = text('foo')
*
* const padded = padPart(
* part,
* {top: 1, right: 1, bottom: 1, left: 1},
* )
*
* expect(drawPart(padded)).toEqual([
* ' ',
* ' foo ',
* ' ',
* ])
* @category drawing
* @function
*/
export const padPart: {
(self: Part, pad: Partial<DirectedPad>): Part
({top, right, bottom, left}: Partial<DirectedPad>): EndoOf<Part>
} = dual(
2,
(self: Part, {top, right, bottom, left}: Partial<DirectedPad>): Part =>
pipe(self, vPadPart(top, bottom), hPadPart()(left, right)),
)
|