From eb4c1828f2311630dc0b8fd5c0be1c90f4a48bd0 Mon Sep 17 00:00:00 2001 From: Andrew Morris Date: Sun, 5 Nov 2023 15:12:32 +1000 Subject: [PATCH] Provide screen dimensions --- concept-code/console/ConsoleApp.ts | 14 ++++++++++++-- concept-code/console/Counter.ts | 10 ++++++++-- vstc/src/console_command.rs | 18 +++++++++++++++++- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/concept-code/console/ConsoleApp.ts b/concept-code/console/ConsoleApp.ts index 8566c42..bc9c08f 100644 --- a/concept-code/console/ConsoleApp.ts +++ b/concept-code/console/ConsoleApp.ts @@ -1,7 +1,17 @@ +export type RenderInfo = { + screenWidth: number; + screenHeight: number; +}; + type ConsoleApp = { createView(): View; - render: (this: { db: Db, view: View }) => string; - onKeyDown: (this: { db: Db, view: View }, key: string) => void; + + render: ( + this: { db: Db; view: View }, + info: RenderInfo, + ) => string; + + onKeyDown: (this: { db: Db; view: View }, key: string) => void; }; export default ConsoleApp; diff --git a/concept-code/console/Counter.ts b/concept-code/console/Counter.ts index 63c7b4b..400737b 100644 --- a/concept-code/console/Counter.ts +++ b/concept-code/console/Counter.ts @@ -1,4 +1,5 @@ import type ConsoleApp from "./ConsoleApp.ts"; +import type { RenderInfo } from "./ConsoleApp.ts"; type View = { offset: number; @@ -12,8 +13,13 @@ export default class ConsoleAppDemo return { offset: 0 }; } - render = function (this: { db: ConsoleAppDemo; view: View }) { - return `${" ".repeat(this.view.offset)}${this.db.value}`; + render = function ( + this: { db: ConsoleAppDemo; view: View }, + { screenWidth, screenHeight }: RenderInfo, + ) { + return `${ + " ".repeat(this.view.offset) + }${this.db.value}\n${screenWidth}x${screenHeight}`; }; onKeyDown = function (this: { db: ConsoleAppDemo; view: View }, key: string) { diff --git a/vstc/src/console_command.rs b/vstc/src/console_command.rs index b20cb83..bb504ca 100644 --- a/vstc/src/console_command.rs +++ b/vstc/src/console_command.rs @@ -12,6 +12,7 @@ use termion::{ event::Key, input::{MouseTerminal, TermRead}, raw::{IntoRawMode, RawTerminal}, + terminal_size, }; use valuescript_compiler::{assemble, compile_str}; use valuescript_vm::{ @@ -180,8 +181,23 @@ impl ConsoleApp { .sub(&"render".to_val()) .or_exit_uncaught(); + let (width, height) = terminal_size().unwrap(); + + let info = VsObject { + string_map: [ + ("screenWidth".to_string(), (width as f64).to_val()), + ("screenHeight".to_string(), (height as f64).to_val()), + ] + .iter() + .cloned() + .collect(), + symbol_map: Default::default(), + prototype: Val::Void, + } + .to_val(); + match vm - .run(None, &mut self.ctx, render, vec![]) + .run(None, &mut self.ctx, render, vec![info]) .or_exit_uncaught() { Val::String(s) => {