Skip to content

Commit

Permalink
Provide screen dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Nov 5, 2023
1 parent 7e6e4b5 commit eb4c182
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
14 changes: 12 additions & 2 deletions concept-code/console/ConsoleApp.ts
Original file line number Diff line number Diff line change
@@ -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;
10 changes: 8 additions & 2 deletions concept-code/console/Counter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type ConsoleApp from "./ConsoleApp.ts";
import type { RenderInfo } from "./ConsoleApp.ts";

type View = {
offset: number;
Expand All @@ -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) {
Expand Down
18 changes: 17 additions & 1 deletion vstc/src/console_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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) => {
Expand Down

0 comments on commit eb4c182

Please sign in to comment.