All files / src/draw/tree/theme ops.ts

100% Statements 40/40
100% Branches 13/13
100% Functions 6/6
100% Lines 40/40

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 901x 1x 1x 1x 1x 1x 1x             1x 70x                   1x 1x   1x   1x 1x                   1x 1x   1x   1x 1x                   1x 1x 421x 421x 421x 421x 421x 421x 421x 421x 421x               1x 1x 269x 420x           1x 541x 541x 541x 541x 541x  
import {map, type Tree} from '#tree'
import {Array, String, flow, pipe} from '#util'
import {type EndoOf} from '#Function'
import {text} from '../../part.js'
import {getGlyph} from './data.js'
import {type GlyphRole} from './glyph.js'
import {type Theme, type ThemedPart} from './themes.js'
 
/**
 * Format tree nodes using the tree theme formatter.
 * @category drawing
 * @function
 */
export const formatNodes = ({formatter}: Theme): EndoOf<Tree<string>> =>
  map(formatter)
 
/**
 * Given a {@link Theme} and a {@link GlyphRole}, returns a function that will
 * prefix a string with the correct glyph.
 * @param theme The {@link Theme} to use.
 * @returns The prefixed string.
 * @category drawing
 * @function
 */
export const prefixGlyph =
  (theme: Theme) =>
  /** The {@link GlyphRole} required. */
  (role: GlyphRole): EndoOf<string> =>
  /** The string to prefix with the glyph. */
  suffix =>
    pipe(role, getGlyph(theme), String.suffix(suffix))
 
/**
 * Given a {@link Theme} and a {@link GlyphRole}, returns a function that will
 * suffix a string with the correct glyph.
 * @param theme The {@link Theme} to use.
 * @returns The suffixed string.
 * @category drawing
 * @function
 */
export const suffixGlyph =
  (theme: Theme) =>
  /** The {@link GlyphRole} required. */
  (role: GlyphRole): EndoOf<string> =>
  /** The string to prefix with the glyph. */
  prefix =>
    pipe(role, getGlyph(theme), String.prefix(prefix))
 
/**
 * Given a {@link GlyphRole} as a _prefix_, and another one for _indents_,
 * returns a string that starts with the glyph from the prefix role,
 * and fills the size of the {@link Theme} `indents` field with the
 * given glyph role.
 * @category drawing
 * @function
 */
export const indentGlyph =
  (theme: Theme) =>
  (prefixRole: GlyphRole, indentRole: GlyphRole): string => {
    const glyph = getGlyph(theme)
    return pipe(
      indentRole,
      glyph,
      String.repeat(theme.indents),
      String.prefix(glyph(prefixRole)),
    )
  }
 
/**
 * A version of {@link indentGlyph} that returns its result in a {@link Text}
 * part instead of a string.
 * @category drawing
 * @function
 */
export const indentGlyphPart =
  (prefixRole: GlyphRole, indentRole: GlyphRole): ThemedPart =>
  theme =>
    flow(indentGlyph(theme), text)(prefixRole, indentRole)
 
/**
 * @category drawing
 * @function
 */
export function addSpacingAfter(label: string) {
  return (theme: Theme): Array.NonEmptyArray<string> =>
    (label + pipe('\n', String.repeat(theme.spacing))).split(
      /\n/,
    ) as Array.NonEmptyArray<string>
}