mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-01-10 14:08:09 -05:00
Provide screen dimensions
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user