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

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

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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 2441x 1x 1x 1x 1x                                                                                               1x 1x 1x 1x 1x 1x 1x 1x                   1x           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                     1x 1x 1x 1x 1x 1x           1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x                         1x                                                     1x 134x   134x 134x 134x 134x 134x               3376x 3376x 3376x 3376x 3376x 3376x 3376x 3376x 3376x 3376x 3376x 3376x             1x 2x 2x 2x 2x 2x 2x 2x 2x 2x   3376x 3376x 3376x 3376x 3376x 3376x 3376x             1x 1x 1x 1x 1x  
import {identity} from '#Function'
import {square} from '#Pair'
import {Array, pipe, Record} from '#util'
import {type Part} from '../../part.js'
import {glyphSet, type GlyphSet, type GlyphSetName} from './glyph.js'
 
/**
 * A _tree theme_:
 *
 * - Maps drawing roles to actual glyphs and styles. For example, a theme could map the glyph role `top-left-elbow`, used when you need an elbow shape pointing top-left, to the glyph `┌`, while another could map it to the glyph `╭`.
 * - Configures indent count. Indents used when moving from parent to child and set the horizontal spacing between adjacent tree levels.
 * - Configures vertical spacing. Vertical spacing is added between nodes.
 * - Can format nodes before drawing, for example to convert to string.
 * @category drawing
 */
export interface Theme {
  /**
   * The number of empty lines added between vertical nodes. A higher
   * number increases table vertical spacing between nodes.
   */
  spacing: number
 
  /**
   * The number of times that the theme glyphs for the role `indent`
   * will be repeated when indenting a part. Higher numbers increase
   * the horizontal space between tree levels.
   */
  indents: number
 
  /**
   * A map of glyph role to glyphs that will be used to compose
   * the tree.
   */
  glyphs: GlyphSet
 
  /** The formatting function can change the tree label before it is drawn. */
  formatter: (node: string) => string
}
 
/**
 * A function that can return a Part when given a theme.
 * @category drawing
 */
export interface ThemedPart {
  (theme: Theme): Part
}
 
/**
 * Theme constructor.
 * @category drawing
 * @function
 */
export function Theme({
  spacing = 0,
  indents = 0,
  glyphs = glyphSet('thin'),
  formatter = identity,
}: Partial<Theme>): Theme {
  return {spacing, indents, glyphs, formatter}
}
 
/**
 * Names of themes sets created with an indent count of one.
 *
 * The tree structure is a bit harder to spot in these themes
 * so we indent parent-to-child one extra indent to emphasize
 * the tree structure.
 * @category internal
 */
export const indentedThemes = ['ascii', 'bullets', 'space'] as const
 
/**
 * Names of glyph sets created with an indent count of zero.
 * @category drawing
 */
export const noIndentThemes = pipe([
  'dashed',
  'dashedWide',
  'dotted',
  'double',
  'hDouble',
  'hThick',
  'hThickDashed',
  'hThickDashedWide',
  'hThickDotted',
  'round',
  'thick',
  'thickDashed',
  'thickDashedWide',
  'thickDotted',
  'thin',
  'unix',
  'unixRound',
  'vDouble',
  'vThick',
  'vThickDashed',
  'vThickDashedWide',
  'vThickDotted',
] as const)
 
/**
 * Type of theme names.
 * @category drawing
 */
export type ThemeName = GlyphSetName
 
/**
 * Default {@link Theme} will draw trees with thin lines and minimal spacing and
 * indent.
 *
 * ```txt
 * ┬1  // default
 * ├─2 // theme
 * └─3 // example
 * ```
 * @category drawing
 */
export const defaultTheme: Theme = {
  spacing: 0,
  indents: 0,
  glyphs: glyphSet('thin'),
  formatter: identity,
}
 
/**
 * All tree theme names.
 * @category drawing
 */
export const themeNames = [...noIndentThemes, ...indentedThemes] as const
 
const entries: (readonly [ThemeName, Theme])[] = [
  ...pipe(
    noIndentThemes,
    Array.map(square.mapSecond(name => _fromNamedGlyphSet(name, 0))),
  ),
  ...pipe(
    indentedThemes,
    Array.map(square.mapSecond(name => _fromNamedGlyphSet(name, 1))),
  ),
]
 
const themes = Record.fromEntries(entries) as ThemeMap
 
/**
 * Get tree theme by name.
 * @example
 * import {Draw} from 'effect-tree'
 *
 * expect(Draw.getTheme('thin').glyphs.hLine).toBe('─')
 * @param name the theme name requested.
 * @returns The requested theme.
 * @category drawing
 * @function
 */
export const getTheme = (name: ThemeName): Theme => themes[name]
 
/**
 * Map over all themes to build a record theme `name` ⇒ `f(theme)`.
 * @example
 * import {Draw} from 'effect-tree'
 * import {Array, pipe, Record} from 'effect'
 *
 * // Place all elbows from all themes in a record with theme name
 * // as key.
 * const actual: Record<Draw.ThemeName, string> = Draw.mapThemes(
 *   Draw.getGlyph.flipped('elbow')
 * )
 *
 * const expected = pipe(
 *   Draw.themeNames,
 *   Array.map(themeName => [
 *     themeName,
 *     Draw.getTheme(themeName).glyphs.elbow,
 *   ] as const),
 *   Record.fromEntries
 * )
 *
 * expect(actual).toEqual(expected)
 * @category drawing
 * @function
 */
export const mapThemes = <A>(
  f: (theme: Theme, name: ThemeName) => A,
): Record<ThemeName, A> =>
  pipe(
    themeNames,
    Array.map(square.mapSecond(name => f(themes[name], name))),
    Record.fromEntries,
  )
 
/**
 * Type of theme registry mapping theme names to themes.
 * @category drawing
 */
export type ThemeMap = Record<ThemeName, Theme>
 
function _fromGlyphSet(
  glyphs = glyphSet('thin'),
  indents = 0,
  theme?: Omit<Theme, 'glyphs' | 'indents'>,
): Theme {
  return {
    ...defaultTheme,
    indents,
    glyphs,
    ...theme,
  }
}
 
/**
 * Build theme from {@link GlyphSet}.
 * @category drawing
 * @function
 */
Theme.fromGlyphSet = (
  glyphs = glyphSet('thin'),
  indents = 0,
  theme?: Omit<Theme, 'glyphs' | 'indents'>,
): Theme => ({
  ...defaultTheme,
  indents,
  glyphs,
  ...theme,
})
 
function _fromNamedGlyphSet(
  name: GlyphSetName = 'thin',
  indents = 0,
  theme?: Omit<Theme, 'glyphs' | 'indents'>,
): Theme {
  return _fromGlyphSet(glyphSet(name), indents, theme)
}
 
/**
 * Build theme from {@link GlyphSetName}.
 * @category drawing
 * @function
 */
Theme.fromNamedGlyphSet = (
  name?: GlyphSetName,
  indents = 0,
  theme?: Omit<Theme, 'glyphs' | 'indents'>,
): Theme => _fromNamedGlyphSet(name, indents, theme)