All files / src/draw/glyph lines.ts

100% Statements 69/69
100% Branches 6/6
100% Functions 5/5
100% Lines 69/69

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 1161x 1x 1x   1x     1x 4x 4x 4x 4x                             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   1932x 1932x 1932x 1932x   552x 552x 552x 552x  
import {Record} from '#util'
import {K, dual, type EndoOf} from '#Function'
import {segmentString} from '#String'
import type {TupleOf} from 'effect/Types'
import {directions, type Direction} from '../direction.js'
import type {LineSet} from './types.js'
 
const _replaceLine = (
  set: LineSet,
  direction: Direction,
  glyph: string,
): LineSet => Record.modify(set, direction, K(glyph))
 
/**
 * Given a direction and a glyph, replaces the line at this direction in a given
 * line set set with the given glyph and returns the new line set.
 *
 * At the key `named` you will find a version that does the same but accepts a
 * _name_ of a line set instead of a line set.
 * @param set Line set to change.
 * @param direction A {@link Direction} of the glyph to be changed.
 * @param glyph String of new glyph.
 * @returns Updated line set.
 * @category drawing
 * @function
 */
export const replaceLine: {
  (set: LineSet, direction: Direction, glyph: string): LineSet
  (direction: Direction, glyph: string): EndoOf<LineSet>
  named: (direction: Direction, glyph: string) => (name: LineSetName) => LineSet
} = Object.assign(dual(3, _replaceLine), {
  named:
    (direction: Direction, glyph: string) =>
    (name: LineSetName): LineSet =>
      _replaceLine(lineSet(name), direction, glyph),
})
 
/**
 * Names of all line sets.
 * @category drawing
 */
export const lineSetNames = [
  'ascii',
  'dashed',
  'dashedWide',
  'dotted',
  'double',
  'halfSolid',
  'halfSolidNear',
  'halfSolidFar',
  'hDouble',
  'hThick',
  'near',
  'solid',
  'space',
  'thick',
  'thickDashed',
  'thickDashedWide',
  'thickDotted',
  'thin',
  'vDouble',
  'vThick',
] as const
 
/**
 * The type of a line set name.
 * @category drawing
 */
export type LineSetName = (typeof lineSetNames)[number]
 
/**
 * A record of all line sets.
 * @category drawing
 */
export type LineSets = Record<LineSetName, LineSet>
 
const lineSets: LineSets = {
  ascii: fromPair('-|'),
  dashed: fromPair('┄┆'),
  dashedWide: fromPair('╌╎'),
  dotted: fromPair('┈┊'),
  double: fromPair('═║'),
  halfSolid: fromQuad('▀▐▄▌'),
  halfSolidNear: fromQuad('▄▌▀▐'),
  halfSolidFar: fromQuad('▀▐▄▌'),
  hDouble: fromPair('═│'),
  hThick: fromPair('━│'),
  near: fromQuad('▁▏▔▕'),
  solid: Record.monoRecord('█')(...directions),
  space: Record.monoRecord(' ')(...directions),
  thick: fromPair('━┃'),
  thickDashed: fromPair('┅┇'),
  thickDashedWide: fromPair('╍╏'),
  thickDotted: fromPair('┉┋'),
  thin: fromPair('─│'),
  vDouble: fromPair('─║'),
  vThick: fromPair('─┃'),
}
 
/**
 * Get a line set by name.
 * @category drawing
 * @function
 */
export const lineSet = (name: LineSetName): LineSet => lineSets[name]
 
function fromPair(s: string): LineSet {
  const [top, right] = segmentString(s) as TupleOf<2, string>
  return {top, right, bottom: top, left: right}
}
 
function fromQuad(s: string): LineSet {
  const [top, right, bottom, left] = segmentString(s) as TupleOf<4, string>
  return {top, right, bottom, left}
}