All files / src/draw/align data.ts

100% Statements 62/62
100% Branches 20/20
100% Functions 8/8
100% Lines 62/62

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 1831x 1x   1x                   1x         1x                                                                                                 1x 1x 1343x 1343x           1x 1x 286x 286x           1x 3057x 3057x           1x 1719x 1719x           1x 1696x   1696x 1696x 1696x           1x 430x   430x 430x 430x   1x 1x 1x 1x 1x 1x 1x 1x           1x 135x 135x           1x 3x 3x 3x     3x             1x 1x 1x 1x 1x 1x             1x 1x 1x 1x 1x 1x 1x             1x 1x 1x 1x  
import {Equivalence as stringEquivalence, unwords} from '#String'
import {Equivalence as EQ, Record} from 'effect'
import type {LazyArg} from 'effect/Function'
import {
  HStrutEquivalence,
  VStrutEquivalence,
  type HStruts,
  type VStruts,
} from '../struts.js'
 
/**
 * @category drawing
 */
export const horizontalAlignments = ['left', 'center', 'right'] as const
 
/**
 * @category drawing
 */
export const verticalAlignments = ['top', 'middle', 'bottom'] as const
 
/**
 * @category drawing
 */
export type HorizontalAlignment = (typeof horizontalAlignments)[number]
 
/**
 * @category drawing
 */
export type VerticalAlignment = (typeof verticalAlignments)[number]
 
/**
 * ```ts
 * {
 *   vAlign: VerticalAlignment
 *   top: VStrut
 *   bottom: VStrut
 * }
 * ```
 * @category drawing
 */
export interface VerticallyAligned extends VStruts {
  vAlign: VerticalAlignment
}
 
/**
 * ```ts
 * {
 *   hAlign: HorizontalAlignment
 *   left: HStrut
 *   right: HStrut
 * }
 * ```
 * @category drawing
 */
export interface HorizontallyAligned extends HStruts {
  hAlign: HorizontalAlignment
}
 
/**
 * @category drawing
 */
export interface Aligned extends HorizontallyAligned, VerticallyAligned {}
 
/**
 * @category drawing
 * @function
 */
export const matchHorizontal =
  <R>(onLeft: LazyArg<R>, onCenter: LazyArg<R>, onRight: LazyArg<R>) =>
  (o: HorizontalAlignment): R =>
    o === 'left' ? onLeft() : o === 'center' ? onCenter() : onRight()
 
/**
 * @category drawing
 * @function
 */
export const matchVertical =
  <R>(onTop: LazyArg<R>, onMiddle: LazyArg<R>, onBottom: LazyArg<R>) =>
  (o: VerticalAlignment): R =>
    o === 'top' ? onTop() : o === 'middle' ? onMiddle() : onBottom()
 
/**
 * @category drawing
 * @function
 */
export const mapHorizontalAlignments = <A>(
  f: (align: HorizontalAlignment, n: number) => A,
): [A, A, A] => [f('left', 0), f('center', 1), f('right', 2)]
 
/**
 * @category drawing
 * @function
 */
export const mapVerticalAlignments = <A>(
  f: (align: VerticalAlignment, n: number) => A,
): [A, A, A] => [f('top', 0), f('middle', 1), f('bottom', 2)]
 
/**
 * @category drawing
 * @function
 */
export const forHorizontalAlignments = <R>(
  f: (align: HorizontalAlignment, n: number) => R,
) =>
  Record.fromEntries(
    mapHorizontalAlignments((hAlign, n) => [hAlign, f(hAlign, n)] as const),
  ) as Record<HorizontalAlignment, R>
 
/**
 * @category drawing
 * @function
 */
export const forVerticalAlignments = <R>(
  f: (align: VerticalAlignment, n: number) => R,
) =>
  Record.fromEntries(
    mapVerticalAlignments((vAlign, n) => [vAlign, f(vAlign, n)] as const),
  ) as Record<VerticalAlignment, R>
 
const alignMarkers = {
  left: '⮄',
  center: '⮂',
  right: '⮆',
  top: '⮅',
  middle: '⮁',
  bottom: '⮇',
} satisfies Record<HorizontalAlignment | VerticalAlignment, string>
 
/**
 * @category drawing
 * @function
 */
export const showAlignment = (
  align: HorizontalAlignment | VerticalAlignment,
): string => alignMarkers[align]
 
/**
 * @category drawing
 * @function
 */
export const showAlignments = ({
  hAlign,
  vAlign,
}: {
  hAlign: HorizontalAlignment
  vAlign: VerticalAlignment
}): string => unwords.rest(showAlignment(hAlign), showAlignment(vAlign))
 
/**
 * Equivalence for {@link HorizontallyAligned}.
 * @category drawing
 * @function
 */
export const HorizontalEquivalence: EQ.Equivalence<HorizontallyAligned> =
  EQ.struct({
    left: HStrutEquivalence,
    right: HStrutEquivalence,
    hAlign: stringEquivalence,
  })
 
/**
 * Equivalence for {@link VerticallyAligned}.
 * @category drawing
 * @function
 */
export const VerticalEquivalence: EQ.Equivalence<VerticallyAligned> = EQ.struct(
  {
    top: VStrutEquivalence,
    bottom: VStrutEquivalence,
    vAlign: stringEquivalence,
  },
)
 
/**
 * Equivalence for {@link Aligned}.
 * @category drawing
 * @function
 */
export const AlignedEquivalence: EQ.Equivalence<Aligned> = EQ.combine(
  HorizontalEquivalence as EQ.Equivalence<Aligned>,
  VerticalEquivalence as EQ.Equivalence<Aligned>,
)