diff --git a/src/lib/painter/canvas.ts b/src/lib/painter/canvas.ts index 5eff322..40b8e0a 100644 --- a/src/lib/painter/canvas.ts +++ b/src/lib/painter/canvas.ts @@ -1,6 +1,47 @@ +import { tryCatch } from '@maxmorozoff/try-catch-tuple'; +import type { CursorPosition } from '../util/terminal'; + +const setInitialEnd = (pos: { + origin: CursorPosition; + end?: CursorPosition; + lines?: 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 = ( - paint: () => Promise, - cleanup?: () => Promise + pos: { origin: CursorPosition; end?: CursorPosition; lines?: number }, + paint: () => Promise, + cleanup?: () => Promise ) => { + let currentOrigin = pos.origin; + + let currentEnd; + const [endVal, endError] = tryCatch(() => setInitialEnd(pos)); + if (endError) throw endError; + currentEnd = endVal; + + paint(); + // + // Paint function + // + // Restore origin position + // Clear to cursor end position + // + // API + // --- + // get origin + // get end + // move end + // get line count + // clear canvas return [1]; };