All files / src/draw/tree draw.ts

100% Statements 45/45
100% Branches 10/10
100% Functions 7/7
100% Lines 45/45

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 2301x 1x 1x   1x 1x 1x                                                                                                                                                                                                                 1x 70x                 1x       1x 1x 1x 1x 1x 1x   1x 69x                     1x       1x 1x 1x 1x                                                                                               1x 1x 1x 1x           1x 3458x 3458x 3458x 3458x   6916x 6916x 6916x 6916x 6916x 6916x 6916x 6916x   13832x 13832x 13832x 13832x 13832x 13832x  
import {map, type Tree} from '#tree'
import {Function, String} from '#util'
import {flow, identity, pipe} from 'effect'
import type {NonEmptyArray} from 'effect/Array'
import {drawPart, type Part} from '../part.js'
import {treeLayout} from './layout.js'
import {
  formatNodes,
  getTheme,
  mapThemes,
  type Theme,
  type ThemeName,
} from './theme.js'
 
/**
 * Type of the base draw function: draw a tree into some output format.
 *
 * A function of the type:
 *
 * ```ts
 *{ (self: Tree<A>): Out }
 * ```
 * @typeParam A Underlying type of the tree being printed.
 * @typeParam Out Output type of drawing function. Usually multiline string or * array of string lines for terminal output.
 * @category drawing
 */
export interface BaseDraw<A, Out> {
  (self: Tree<A>): Out
}
 
/**
 * A function that draws string trees into a non-empty array of string rows.
 * Defined as {@link BaseDraw} fixed on `string` input and on output of
 * `NonEmptyArray<string>`.
 *
 * A function of the type:
 *
 * ```ts
 * { (self: Tree<string>): NonEmptyArray<string> }
 * ```
 * @category drawing
 */
export interface StringDraw extends BaseDraw<string, NonEmptyArray<string>> {}
 
/**
 * Type of the unlines draw function: a {@link BaseDraw} that outputs to a
 * non-empty array of strings, and has a variant under the `unlines` key.
 *
 * At the key `unlines` you will find a version that draws to a `string` instead
 * of a `NonEmptyArray<string>` by joining the rows with newlines.
 *
 * A function of the type:
 *
 * ```ts
 * {
 *   (self: Tree<A>): NonEmptyArray<string>
 *   unlines: (self: Tree<A>) ⇒ NonEmptyArray<string>
 * }
 * ```
 * @typeParam A Underlying type of the tree being printed.
 * @category drawing
 */
export interface UnlinesDraw<A> extends BaseDraw<A, NonEmptyArray<string>> {
  unlines: BaseDraw<A, string>
}
 
/**
 * Type of the enriched draw function: an {@link UnlinesDraw} for strings, with
 * another version of the very same {@link UnlinesDraw} at the key `number`
 * specialized for numeric trees.
 *
 * A function of the type:
 *
 * ```ts
 * {
 *   (self: Tree<string>): NonEmptyArray<string>
 *   unlines: (self: Tree<string>) ⇒ string
 *   number: {
 *     (self: Tree<number>) ⇒ NonEmptyArray<string>
 *     unlines: (self: Tree<number>) ⇒ string
 *   }
 * }
 * ```
 * @category drawing
 */
export interface EnrichedDraw extends UnlinesDraw<string> {
  number: UnlinesDraw<number>
}
 
/**
 * Type of the {@link drawTree} function: exactly like {@link EnrichedDraw} but
 * adds a key per theme name with another {@link EnrichedDraw} as the value.
 *
 * A function of the type:
 *
 * ```ts
 * {
 *     (self: Tree<string>): NonEmptyArray<string>
 *     unlines: (self: Tree<string>) ⇒ string
 *     number: {
 *       (self: Tree<number>) ⇒ NonEmptyArray<string>
 *       unlines: (self: Tree<number>) ⇒ string
 *     }
 * } & Record<ThemeName, EnrichedDraw>
 * ```
 * @category drawing
 */
export interface DrawTree
  extends EnrichedDraw,
    Record<ThemeName, EnrichedDraw> {}
 
const _treeToPart = (self: Tree<string>, theme: Theme): Part =>
  pipe(self, formatNodes(theme), treeLayout.flip(theme))
 
/**
 * Draw a themed tree to a part.
 *
 * At the key `number` you will find a version that draws numeric trees.
 * @category drawing
 * @function
 */
export const treeToPart: {
  (self: Tree<string>, theme: Theme): Part
  (theme: Theme): (self: Tree<string>) => Part
  number: (theme: Theme) => (self: Tree<number>) => Part
} = Object.assign(Function.dual(2, _treeToPart), {
  number:
    (theme: Theme) =>
    (self: Tree<number>): Part =>
      _treeToPart(map(self, String.fromNumber), theme),
})
 
const _themedTree = (self: Tree<string>, theme: Theme) =>
  pipe(treeToPart(self, theme), drawPart) as NonEmptyArray<string>
 
/**
 * Draw a tree as a 2D array of glyphs in the given theme.
 *
 * At the key `unlines` you will find a version that joins output into a string.
 *
 * You can get themes by name using the {@link getTheme | getTheme function}.
 * @category drawing
 * @function
 */
export const themedTree: {
  (self: Tree<string>, theme: Theme): NonEmptyArray<string>
  (theme: Theme): (self: Tree<string>) => NonEmptyArray<string>
  unlines: (theme: Theme) => (self: Tree<string>) => string
} = Object.assign(Function.dual(2, _themedTree), {
  unlines: (theme: Theme) => (self: Tree<string>) =>
    String.unlines(_themedTree(self, theme)),
})
 
/**
 * Draw a string or numeric tree into a non-empty array of string rows.
 *
 * At the key `unlines` you will find a version the returns a string of the
 * joined rows.
 *
 * At the key `numeric` you will find a version that draws _numeric_ trees, and
 * it too has a key `unlines` with a version that draws to strings rather than
 * an array of rows.
 *
 * At each key named after a {@link themeNames | theme name}, you will find a
 * version of the function with its `numeric` and `unlines` variants, that draws
 * the tree at the given theme.
 * @example
 * import {of, drawTree} from 'effect-tree'
 *
 * const stringTree = of('foo')
 * const numericTree = of(42)
 *
 * // Draw a string tree into arrays of rows using default theme.
 * expect(drawTree(stringTree)).toMatchInlineSnapshot()
 *
 * // Draw a numeric tree into arrays of rows using default theme.
 * expect(drawTree.number(numericTree)).toMatchInlineSnapshot()
 *
 * // Draw string trees to a string using default theme, when you must use
 * // `console.log`, for example.
 * expect(drawTree.unlines(stringTree)).toMatchInlineSnapshot()
 *
 * // Draw numeric trees to a string using default theme.
 * expect(drawTree.number.unlines(numericTree)).toMatchInlineSnapshot()
 *
 * // Draw string trees using a specific theme into rows.
 * expect(drawTree.ascii(stringTree)).toMatchInlineSnapshot()
 *
 * // Draw string trees using a specific theme into a string.
 * expect(drawTree.ascii.unlines(stringTree)).toMatchInlineSnapshot()
 *
 * // Draw numeric trees using a specific theme into rows.
 * expect(drawTree.ascii.number(numericTree)).toMatchInlineSnapshot()
 *
 * // Draw numeric trees using a specific theme into a string.
 * expect(drawTree.ascii.number.unlines(numericTree)).toMatchInlineSnapshot()
 * @category drawing
 * @function
 */
export const drawTree: DrawTree = Object.assign(
  enrichedDraw('thin'),
  mapThemes((_, name) => enrichedDraw(name)),
)
 
/**
 * @category internal
 * @function
 */
export function enrichedDraw(themeName: ThemeName): EnrichedDraw {
  return Object.assign(unlinesDraw<string>(themeName, identity), {
    number: unlinesDraw<number>(themeName, String.fromNumber),
  })
}
 
function unlinesDraw<A>(
  themeName: ThemeName,
  converter: (a: A) => string,
): UnlinesDraw<A> {
  return Object.assign(baseDraw(themeName, converter), {
    unlines: flow(baseDraw(themeName, converter), String.unlines),
  })
}
 
function baseDraw<A>(
  themeName: ThemeName,
  converter: (a: A) => string,
): BaseDraw<A, NonEmptyArray<string>> {
  return flow(map(converter), pipe(themeName, getTheme, themedTree))
}