Provide screen dimensions

This commit is contained in:
Andrew Morris
2023-11-05 15:12:32 +10:00
parent 7e6e4b5540
commit eb4c1828f2
3 changed files with 37 additions and 5 deletions

View File

@@ -1,7 +1,17 @@
export type RenderInfo = {
screenWidth: number;
screenHeight: number;
};
type ConsoleApp<Db, View> = {
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;

View File

@@ -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) {

View File

@@ -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) => {