From a1e1b484fc62bdc5ad0732d1f6cfef0837f5ca77 Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Sun, 5 Nov 2023 15:52:32 +1000 Subject: [PATCH] Use lines array, undefined means, stop, update example --- concept-code/console/ConsoleApp.ts | 2 +- concept-code/console/Counter.ts | 39 +++++++++++++++++++++++------- vstc/src/console_command.rs | 27 ++++++++++++++++++--- 3 files changed, 54 insertions(+), 14 deletions(-) diff --git a/concept-code/console/ConsoleApp.ts b/concept-code/console/ConsoleApp.ts index bc9c08f..091f111 100644 --- a/concept-code/console/ConsoleApp.ts +++ b/concept-code/console/ConsoleApp.ts @@ -9,7 +9,7 @@ type ConsoleApp = { render: ( this: { db: Db; view: View }, info: RenderInfo, - ) => string; + ) => string[] | undefined; onKeyDown: (this: { db: Db; view: View }, key: string) => void; }; diff --git a/concept-code/console/Counter.ts b/concept-code/console/Counter.ts index 400737b..29ef98f 100644 --- a/concept-code/console/Counter.ts +++ b/concept-code/console/Counter.ts @@ -3,10 +3,9 @@ import type { RenderInfo } from "./ConsoleApp.ts"; type View = { offset: number; -}; +} | undefined; -export default class ConsoleAppDemo - implements ConsoleApp { +export default class Counter implements ConsoleApp { value = 0; createView(): View { @@ -14,18 +13,40 @@ export default class ConsoleAppDemo } render = function ( - this: { db: ConsoleAppDemo; view: View }, + this: { db: Counter; view: View }, { screenWidth, screenHeight }: RenderInfo, ) { - return `${ - " ".repeat(this.view.offset) - }${this.db.value}\n${screenWidth}x${screenHeight}`; + if (this.view === undefined) { + return undefined; + } + + let wCenter = Math.floor(screenWidth / 2); + let hCenter = Math.floor(screenHeight / 2); + + let lines = []; + + for (let i = 0; i < hCenter; i++) { + lines.push(""); + } + + lines.push(" ".repeat(wCenter + this.view.offset) + this.db.value); + + return lines; }; - onKeyDown = function (this: { db: ConsoleAppDemo; view: View }, key: string) { + onKeyDown = function (this: { db: Counter; view: View }, key: string) { + if (this.view === undefined) { + return; + } + switch (key) { + case "q": { + this.view = undefined; + break; + } + case "ArrowLeft": { - this.view.offset = Math.max(0, this.view.offset - 1); + this.view.offset--; break; } diff --git a/vstc/src/console_command.rs b/vstc/src/console_command.rs index bb504ca..f63de4b 100644 --- a/vstc/src/console_command.rs +++ b/vstc/src/console_command.rs @@ -141,6 +141,7 @@ impl ConsoleApp { Key::Right => "ArrowRight".to_string(), Key::Up => "ArrowUp".to_string(), Key::Down => "ArrowDown".to_string(), + Key::Char(c) => c.to_string(), _ => continue, }; @@ -200,20 +201,38 @@ impl ConsoleApp { .run(None, &mut self.ctx, render, vec![info]) .or_exit_uncaught() { - Val::String(s) => { + Val::Array(arr) => { write!( self.stdout, - "{}{}{}", + "{}{}", termion::clear::All, termion::cursor::Goto(1, 1), - s ) .unwrap(); + for (i, line) in arr.elements.iter().enumerate() { + match line { + Val::String(s) => { + write!( + self.stdout, + "{}{}", + termion::cursor::Goto(1, (i as u16) + 1), + s + ) + .unwrap(); + } + line => { + println!("ERROR: Non-string line: {}", line.pretty()); + exit(1); + } + } + } + self.stdout.flush().unwrap(); } + Val::Undefined => exit(0), non_str => { - println!("ERROR: Non-string render: {}", non_str.pretty()); + println!("ERROR: Non-array render: {}", non_str.pretty()); exit(1); } }