Begin re-write in proper functional style
This commit is contained in:
parent
9c09560b73
commit
e897ca7848
@ -1,47 +1,63 @@
|
|||||||
import { tryCatch } from '@maxmorozoff/try-catch-tuple';
|
import { type CursorPos } from 'node:readline'
|
||||||
import type { CursorPosition } from '../util/terminal';
|
|
||||||
|
|
||||||
const setInitialEnd = (pos: {
|
// Cursor stuffs
|
||||||
origin: CursorPosition;
|
interface CursorPosX {
|
||||||
end?: CursorPosition;
|
readonly _tag: 'X'
|
||||||
lines?: number;
|
readonly value: number
|
||||||
}) => {
|
}
|
||||||
if (!pos.end || !pos.lines)
|
|
||||||
throw TypeError("One of 'end' or 'lines' must be set");
|
|
||||||
if (Object.keys(pos).includes('end') || Object.keys(pos).includes('end'))
|
|
||||||
throw TypeError("Cannot set both 'end' and 'lines'");
|
|
||||||
if (pos.end) return pos.end;
|
|
||||||
return {
|
|
||||||
row: pos.origin.row + pos.lines,
|
|
||||||
col: pos.origin.col,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
export const createCanvas = (
|
interface CursorPosY {
|
||||||
pos: { origin: CursorPosition; end?: CursorPosition; lines?: number },
|
readonly _tag: 'Y'
|
||||||
paint: () => Promise<number>,
|
readonly value: number
|
||||||
cleanup?: () => Promise<number>
|
}
|
||||||
) => {
|
|
||||||
let currentOrigin = pos.origin;
|
|
||||||
|
|
||||||
let currentEnd;
|
const cursorPosX = (x: number): CursorPosX => ({_tag: 'X', value: x})
|
||||||
const [endVal, endError] = tryCatch(() => setInitialEnd(pos));
|
|
||||||
if (endError) throw endError;
|
|
||||||
currentEnd = endVal;
|
|
||||||
|
|
||||||
paint();
|
const cursorPosY = (x: number): CursorPosY => ({_tag: 'Y', value: x})
|
||||||
//
|
|
||||||
// Paint function
|
type IsCursorPos =
|
||||||
//
|
(x: 'X' | 'Y') => (y: CursorPosX | CursorPosY) => boolean
|
||||||
// Restore origin position
|
const isCursorPos: IsCursorPos = x => y => y._tag === x
|
||||||
// Clear to cursor end position
|
|
||||||
//
|
// Tagged types
|
||||||
// API
|
interface CanvasOrigin {
|
||||||
// ---
|
readonly _tag: 'Origin'
|
||||||
// get origin
|
readonly value: CursorPos
|
||||||
// get end
|
}
|
||||||
// move end
|
|
||||||
// get line count
|
interface CanvasEnd {
|
||||||
// clear canvas
|
readonly _tag: 'End'
|
||||||
return [1];
|
readonly value: CursorPos
|
||||||
};
|
}
|
||||||
|
|
||||||
|
// Canvas type using tagged positions
|
||||||
|
type Canvas = {
|
||||||
|
origin: CanvasOrigin,
|
||||||
|
end: CanvasEnd,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Cursor Position
|
||||||
|
// const cursorPos: CursorPos = ()
|
||||||
|
|
||||||
|
// Constructor helpers
|
||||||
|
const origin = (pos: CursorPos): CanvasOrigin => ({
|
||||||
|
_tag: 'Origin',
|
||||||
|
value: pos,
|
||||||
|
})
|
||||||
|
|
||||||
|
const end = (pos: CursorPos): CanvasEnd => ({
|
||||||
|
_tag: 'End',
|
||||||
|
value: pos,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Curried canvas creator with type safety
|
||||||
|
const canvas =
|
||||||
|
(origin: CanvasOrigin) => (end: CanvasEnd): Canvas => ({
|
||||||
|
origin,
|
||||||
|
end,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Usage with proper type safety
|
||||||
|
const myCanvas = canvas(origin({rows: 3, cols: 1}))(
|
||||||
|
end({rows: 8, cols: 1}),
|
||||||
|
)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user