diff options
author | wlodekm <[email protected]> | 2024-11-15 09:46:47 +0200 |
---|---|---|
committer | wlodekm <[email protected]> | 2024-11-15 09:46:47 +0200 |
commit | d28d8333ebe71e2937660b13d9afb1d516cf14f0 (patch) | |
tree | 6c941712ec80b785a940b37aec52b2cea7861835 /v2/screen.ts | |
parent | d51dbb974d83125f4bfb017188de6a013b7f746c (diff) |
v1.0.2
Diffstat (limited to 'v2/screen.ts')
-rw-r--r-- | v2/screen.ts | 101 |
1 files changed, 0 insertions, 101 deletions
diff --git a/v2/screen.ts b/v2/screen.ts deleted file mode 100644 index 5e09d57..0000000 --- a/v2/screen.ts +++ /dev/null @@ -1,101 +0,0 @@ -import type { Element, Input, Text, Button } from "./elements.ts" - -const logs: string[] = []; - -function onexit() { - console.clear() - console.log("\nQuitting meower CL") - for (const log of logs) { - console.log(log) - } -} - -export class Screen { - elements: Map<string, Element|Input|Text|Button> = new Map(); - name: string; - focusedElementId: string = ''; - logs = logs - - constructor(name: string) { - this.name = name; - } - - call(function_name:string, ...args:any) { - for (const element of this.elements) { - element[1][function_name](...args) - } - } - - handleKeypress(chunk: any, key: any, screen: Screen) { - const focusableIDs = Object.keys(screen.getFocusable()); - const focusedIndex = focusableIDs.indexOf(screen.focusedElementId); - if (key && key.name == 'escape' || key.name == "c" && key.ctrl) { - onexit(); - process.exit(); - } - - if (['up', 'left'].includes(key.name) || key.name == "tab" && key.shift) { - // logs.push(`Got up key, moving focus upward ${focusedIndex} ${(focusedIndex - 1) % focusableIDs.length}`) - screen.focus(focusableIDs[(focusedIndex - 1) % focusableIDs.length]); - return screen.render() - } - if (['down', 'right'].includes(key.name) || key.name == "tab" && !key.shift) { - // logs.push(`Got down key, moving focus downward ${focusedIndex} ${(focusedIndex + 1) % focusableIDs.length}`) - screen.focus(focusableIDs[(focusedIndex + 1) % focusableIDs.length]); - return screen.render() - } - - // logs.push("pressed key, data: " + JSON.stringify(key)) - if (!screen.focusedElementId) return; - const focusedElement = screen.getFocusedElement(); - focusedElement?.onkeypres(key) - } - - getKeypressHandler(screen: Screen) { - return (chunk: any, key: any) => this.handleKeypress(chunk,key, screen); - } - - keypressHandler = this.getKeypressHandler(this) - - ready() { - process.stdin.on('keypress', this.keypressHandler); - this.render() - } - - off() { - process.stdin.off('keypress', this.keypressHandler) - } - - addElement(name: string, element: Element) { - if(this.elements.has(name)) throw new Error(); - element.screen = this; - this.elements.set(name, element); - } - - render() { - console.clear() - process.stdout.write("\u001b[2J") // use an ansi escape code to clear the screen if console.clear doesn't clear fully - this.elements.forEach(element => { - element.render() - }); - } - - getFocusable() { - return Object.fromEntries([...this.elements.entries()].filter(([k, v]) => v.focusable)) - } - - getElements() { - return Object.fromEntries([...this.elements.entries()]) - } - - focus(id: string) { - this.elements.forEach(e => e.focused = false); - const focusElem = this.elements.get(id) as Element - focusElem.focused = true; - this.focusedElementId = id - } - - getFocusedElement(): Element|undefined { - return this.focusedElementId ? this.elements.get(this.focusedElementId) as Element : undefined - } -} |