wip: vstc console

This commit is contained in:
Andrew Morris
2023-11-05 10:50:12 +10:00
parent edbd27d62e
commit 81c86e1728
7 changed files with 146 additions and 0 deletions

28
Cargo.lock generated
View File

@@ -889,6 +889,12 @@ dependencies = [
"libc",
]
[[package]]
name = "numtoa"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
[[package]]
name = "object"
version = "0.28.3"
@@ -1199,6 +1205,15 @@ dependencies = [
"bitflags",
]
[[package]]
name = "redox_termios"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8440d8acb4fd3d277125b4bd01a6f38aee8d814b3b5fc09b3f2b825d37d3fe8f"
dependencies = [
"redox_syscall",
]
[[package]]
name = "regex"
version = "1.5.5"
@@ -2196,6 +2211,18 @@ dependencies = [
"winapi",
]
[[package]]
name = "termion"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "659c1f379f3408c7e5e84c7d0da6d93404e3800b6b9d063ba24436419302ec90"
dependencies = [
"libc",
"numtoa",
"redox_syscall",
"redox_termios",
]
[[package]]
name = "textwrap"
version = "0.15.0"
@@ -2453,6 +2480,7 @@ dependencies = [
"serde",
"serde_qs",
"storage",
"termion",
"url",
"valuescript_compiler",
"valuescript_vm",

View File

@@ -0,0 +1,7 @@
type ConsoleApp<Db, View> = {
createView(): View;
render: (this: { db: Db, view: View }) => string;
onKeyDown: (this: { db: Db, view: View }, key: string) => void;
};
export default ConsoleApp;

View File

@@ -0,0 +1,42 @@
import ConsoleApp from "./ConsoleApp.ts";
type View = {
offset: number;
};
export default class ConsoleAppDemo
implements ConsoleApp<ConsoleAppDemo, View> {
value = 0;
createView(): View {
return { offset: 0 };
}
render = function (this: { db: ConsoleAppDemo; view: View }) {
return `${" ".repeat(this.view.offset)}${this.db.value}`;
};
onKeyDown = function (this: { db: ConsoleAppDemo; view: View }, key: string) {
switch (key) {
case "ArrowLeft": {
this.view.offset = Math.max(0, this.view.offset - 1);
break;
}
case "ArrowRight": {
this.view.offset++;
break;
}
case "ArrowUp": {
this.db.value++;
break;
}
case "ArrowDown": {
this.db.value--;
break;
}
}
};
}

View File

@@ -12,3 +12,4 @@ storage = { path = "../storage" }
url = "2.2.1"
serde = "1.0"
serde_qs = "0.8.0"
termion = "2.0.1"

View File

@@ -0,0 +1,30 @@
// use storage::{SledBackend, Storage};
pub fn console_command(args: &[String]) {
// let mut storage = Storage::new(SledBackend::open("./state.vsdb").unwrap());
match args.get(0).map(String::as_str) {
Some("help") => show_help(),
Some("new") => todo!(),
Some("start") => todo!(),
_ => todo!(),
}
}
fn show_help() {
println!("vstc console");
println!();
println!("ValueScript console applications");
println!();
println!("USAGE:");
println!(" vstc console [COMMAND] [ARGS]");
println!();
println!("Commands:");
println!(" help, -h, --help Show this message");
println!(" new [CLASS_FILE] [ARGS] Create a new console app");
println!(" start [APP_FILE] [ARGS] Start an existing console app");
println!();
println!("Examples:");
println!(" vstc console new Tetris.ts Create a tetris game");
println!(" vstc console start tetris.vsdb Start tetris");
}

View File

@@ -1,10 +1,12 @@
mod assemble_command;
mod compile_command;
mod console_command;
mod db_command;
mod handle_diagnostics_cli;
mod parse_command_line;
mod resolve_entry_path;
mod run_command;
mod termion_test;
mod test_inputs;
mod to_bytecode;
@@ -13,8 +15,10 @@ use std::process::exit;
use assemble_command::assemble_command;
use compile_command::compile_command;
use console_command::console_command;
use db_command::db_command;
use run_command::run_command;
use termion_test::termion_test;
fn main() {
let args: Vec<String> = env::args().collect();
@@ -25,6 +29,8 @@ fn main() {
Some("run") => run_command(&args),
Some("compile") => compile_command(&args),
Some("db") => db_command(&args),
Some("termion") => termion_test(),
Some("console") => console_command(&args),
_ => {
println!("ERROR: Unrecognized command\n");
show_help();

32
vstc/src/termion_test.rs Normal file
View File

@@ -0,0 +1,32 @@
use std::io::{stdin, stdout, Write};
use termion::event::{Event, Key, MouseEvent};
use termion::input::{MouseTerminal, TermRead};
use termion::raw::IntoRawMode;
pub fn termion_test() {
let stdin = stdin();
let mut stdout = MouseTerminal::from(stdout().into_raw_mode().unwrap());
write!(
stdout,
"{}{}q to exit. Click, click, click!",
termion::clear::All,
termion::cursor::Goto(1, 1)
)
.unwrap();
stdout.flush().unwrap();
for c in stdin.events() {
let evt = c.unwrap();
match evt {
Event::Key(Key::Char('q')) => break,
Event::Mouse(me) => {
if let MouseEvent::Press(_, x, y) = me {
write!(stdout, "{}x", termion::cursor::Goto(x, y)).unwrap();
}
}
_ => {}
}
stdout.flush().unwrap();
}
}