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 | 1x 1x 1x 1x 1x 1x 4x 1x 4x 1x 4x 1x 4x 1x 1x 1x 2x 2x 1x 1x 2x 2x 1x 1x 5x 5x 5x 5x 5x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x | import type {EndoOf} from '#Function'
import {flow, pipe} from 'effect'
import {emptyF, isPartFOf, matchPartF, textF} from '../partF.js'
import {
Column,
fixPart,
Row,
unfixPart,
type Empty,
type Part,
type Text,
} from './types.js'
/**
* The empty part takes zero width and zero height.
* @example
* import {Draw} from 'effect-tree'
*
* const part = Draw.row.centre.middle([
* Draw.text('hello-),
* Draw.empty,
* Draw.text('world),
*
* expect(Draw.drawPart(part)).toEqual(['hello-world'])
* @category drawing
*/
export const empty: Empty = fixPart(emptyF)
/**
* Build a part from a single line of text.
* @example
* import {Draw} from 'effect-tree'
*
* const part = Draw.text('hello')
*
* expect(Draw.drawPart(part)).toEqual(['hello'])
* @category drawing
* @function
*/
export const text: (show: string) => Text = flow(textF, fixPart<Text>)
/**
* Type guard for the {@link Empty} {@link Part}.
* @example
* import {Draw} from 'effect-tree'
*
* const part1 = Draw.text('hello')
* const part2 = Draw.empty
*
* expect(Draw.isEmptyPart(part1), 'not empty').toBe(false)
* expect(Draw.isEmptyPart(part2), 'empty').toBe(true)
* @category drawing
* @function
*/
export const isEmptyPart = (self: Part): self is Empty =>
pipe(self, unfixPart, isPartFOf('EmptyF'))
/**
* Type guard for the {@link Text} {@link Part}.
* @example
* import {Draw} from 'effect-tree'
*
* const part1 = Draw.text('hello')
* const part2 = Draw.empty
*
* expect(Draw.isText(part1), 'text').toBe(true)
* expect(Draw.isText(part2), 'not text').toBe(false)
* @category drawing
* @function
*/
export const isText = (self: Part): self is Text =>
pipe(self, unfixPart, isPartFOf('TextF'))
/**
* Type guard for the {@link Row} {@link Part}.
* @example
* import {Draw} from 'effect-tree'
*
* const part1 = Draw.text('hello')
* const part2 = Draw.row.top.left([part1])
*
* expect(Draw.isRow(part1), 'not a row').toBe(false)
* expect(Draw.isRow(part2), 'a row').toBe(true)
* @category drawing
* @function
*/
export const isRow = (self: Part): self is Row =>
pipe(self, unfixPart, isPartFOf('RowF'))
/**
* Type guard for the {@link Column} {@link Part}.
* @example
* import {Draw} from 'effect-tree'
*
* const part1 = Draw.column.left([Draw.text('foo')])
* const part2 = Draw.row.top.left([part1])
*
* expect(Draw.isColumn(part1), 'a column').toBe(true)
* expect(Draw.isColumn(part2), 'not a column').toBe(false)
* @category drawing
* @function
*/
export const isColumn = (self: Part): self is Column =>
pipe(self, unfixPart, isPartFOf('ColumnF'))
/**
* Get the text content of a {@link Text} part.
* @example
* import {Draw} from 'effect-tree'
*
* const part = Draw.text('foo')
*
* expect(Draw.getText(part)).toBe('foo')
* @category drawing
* @function
*/
export const getText: (text: Text) => string = ({unfixed: {show}}) => show
/**
* Combine two text parts horizontally placing the first to the right of the
* second.
* @example
* import {Draw} from 'effect-tree'
*
* const left = Draw.text('foo-')
* const right = Draw.text('bar')
*
* expect(Draw.prefixText(left)(right)).toEqual(Draw.text('foo-bar'))
* @category drawing
* @function
*/
export const prefixText: (prefix: Text) => EndoOf<Text> =
({unfixed: {show: prefix}}) =>
({unfixed: {show: suffix}}) =>
text(prefix + suffix)
/**
* Combine two text parts horizontally placing the first to the left of the
* second.
* @example
* import {Draw} from 'effect-tree'
*
* const left = Draw.text('foo-')
* const right = Draw.text('bar')
*
* expect(Draw.suffixText(right)(left)).toEqual(Draw.text('foo-bar'))
* @category drawing
* @function
*/
export const suffixText: (suffix: Text) => EndoOf<Text> =
({unfixed: {show: suffix}}) =>
({unfixed: {show: prefix}}) =>
text(prefix + suffix)
/**
* Match part by type.
* @example
* import {Draw} from 'effect-tree'
*
* const emptyPart = Draw.empty
* const textPart = Draw.text('foo')
* const rowPart = Draw.row.top.right([textPart])
* const columnPart = Draw.column.right([textPart])
*
* const match = Draw.matchPart(
* 'empty',
* s => `text=${s}`,
* () => 'row',
* () => 'column',
* )
*
* expect(match(emptyPart), 'empty').toBe('empty')
* expect(match(textPart), 'text').toBe('text=foo')
* expect(match(rowPart), 'row').toBe('row')
* expect(match(columnPart), 'column').toBe('column')
* @category drawing
* @function
*/
export const matchPart =
<R>(
onEmpty: R,
onText: (s: string) => R,
onRow: (r: Row) => R,
onColumn: (c: Column) => R,
): ((p: Part) => R) =>
part =>
pipe(
part,
unfixPart,
matchPartF(
onEmpty,
onText,
flow(fixPart<Row>, onRow),
flow(fixPart<Column>, onColumn),
),
)
|