Skip to content

Commit

Permalink
Use lines array, undefined means, stop, update example
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Nov 5, 2023
1 parent eb4c182 commit a1e1b48
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
2 changes: 1 addition & 1 deletion concept-code/console/ConsoleApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type ConsoleApp<Db, View> = {
render: (
this: { db: Db; view: View },
info: RenderInfo,
) => string;
) => string[] | undefined;

onKeyDown: (this: { db: Db; view: View }, key: string) => void;
};
Expand Down
39 changes: 30 additions & 9 deletions concept-code/console/Counter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,50 @@ import type { RenderInfo } from "./ConsoleApp.ts";

type View = {
offset: number;
};
} | undefined;

export default class ConsoleAppDemo
implements ConsoleApp<ConsoleAppDemo, View> {
export default class Counter implements ConsoleApp<Counter, View> {
value = 0;

createView(): View {
return { offset: 0 };
}

render = function (
this: { db: ConsoleAppDemo; view: View },
this: { db: Counter; view: View },
{ screenWidth, screenHeight }: RenderInfo,
) {
return `${
" ".repeat(this.view.offset)
}${this.db.value}\n${screenWidth}x${screenHeight}`;
if (this.view === undefined) {
return undefined;
}

let wCenter = Math.floor(screenWidth / 2);
let hCenter = Math.floor(screenHeight / 2);

let lines = [];

for (let i = 0; i < hCenter; i++) {
lines.push("");
}

lines.push(" ".repeat(wCenter + this.view.offset) + this.db.value);

return lines;
};

onKeyDown = function (this: { db: ConsoleAppDemo; view: View }, key: string) {
onKeyDown = function (this: { db: Counter; view: View }, key: string) {
if (this.view === undefined) {
return;
}

switch (key) {
case "q": {
this.view = undefined;
break;
}

case "ArrowLeft": {
this.view.offset = Math.max(0, this.view.offset - 1);
this.view.offset--;
break;
}

Expand Down
27 changes: 23 additions & 4 deletions vstc/src/console_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ impl ConsoleApp {
Key::Right => "ArrowRight".to_string(),
Key::Up => "ArrowUp".to_string(),
Key::Down => "ArrowDown".to_string(),
Key::Char(c) => c.to_string(),
_ => continue,
};

Expand Down Expand Up @@ -200,20 +201,38 @@ impl ConsoleApp {
.run(None, &mut self.ctx, render, vec![info])
.or_exit_uncaught()
{
Val::String(s) => {
Val::Array(arr) => {
write!(
self.stdout,
"{}{}{}",
"{}{}",
termion::clear::All,
termion::cursor::Goto(1, 1),
s
)
.unwrap();

for (i, line) in arr.elements.iter().enumerate() {
match line {
Val::String(s) => {
write!(
self.stdout,
"{}{}",
termion::cursor::Goto(1, (i as u16) + 1),
s
)
.unwrap();
}
line => {
println!("ERROR: Non-string line: {}", line.pretty());
exit(1);
}
}
}

self.stdout.flush().unwrap();
}
Val::Undefined => exit(0),
non_str => {
println!("ERROR: Non-string render: {}", non_str.pretty());
println!("ERROR: Non-array render: {}", non_str.pretty());
exit(1);
}
}
Expand Down

0 comments on commit a1e1b48

Please sign in to comment.