clean up printing

This commit is contained in:
Crispy 2024-09-30 22:00:42 +02:00
parent c3a73e3104
commit 58dbbc6d8f

View file

@ -1,4 +1,8 @@
use std::{env, fs, io::{stdin, stdout, Write}, process::exit}; use std::{
env, fs,
io::{stdin, stdout, Write},
process::exit,
};
use owo_colors::OwoColorize; use owo_colors::OwoColorize;
@ -100,7 +104,6 @@ fn main() {
.unwrap_or_default(); .unwrap_or_default();
let mut interpreter = Machine::new(&source, input_data); let mut interpreter = Machine::new(&source, input_data);
print!("\x1B[2J"); // clear screen
interpreter.show(); interpreter.show();
loop { loop {
@ -120,7 +123,7 @@ fn main() {
continue; continue;
} }
const HELP_TEXT:&str = r#"\n | step const HELP_TEXT: &str = r#"\n | step
- step once - step once
step [n] step [n]
- step n times - step n times
@ -139,7 +142,12 @@ q | quit | exit
["dbg" | "debug"] => { ["dbg" | "debug"] => {
dbg!(&interpreter); dbg!(&interpreter);
} }
["step", num] => _ = num.parse().map(|n| interpreter.step(n)), ["step", num] => {
if let Ok(n) = num.parse() {
interpreter.step(n);
interpreter.show();
}
}
["q" | "exit" | "quit"] => break, ["q" | "exit" | "quit"] => break,
["h" | "help" | "?"] => { ["h" | "help" | "?"] => {
println!("{}", HELP_TEXT.green()); println!("{}", HELP_TEXT.green());
@ -170,9 +178,8 @@ impl Machine {
} }
fn show(&self) { fn show(&self) {
// dbg!(&self);
print!("\x1B[2J"); // clear screen print!("\x1B[2J"); // clear screen
print!("\x1B[u"); // reset cursor print!("\x1B[1;1H"); // reset cursor
for y in 0..self.grid.height() { for y in 0..self.grid.height() {
let mut marbles_on_row = Vec::new(); let mut marbles_on_row = Vec::new();
for x in 0..self.grid.width() { for x in 0..self.grid.width() {
@ -243,7 +250,7 @@ impl Machine {
println!("steps: {}", self.steps); println!("steps: {}", self.steps);
} }
fn print_input(&self){ fn print_input(&self) {
if !self.input.is_empty() { if !self.input.is_empty() {
println!("input: '{}'", String::from_utf8_lossy(&self.input)); println!("input: '{}'", String::from_utf8_lossy(&self.input));
} }
@ -423,7 +430,7 @@ impl Machine {
MathOp::Div => val_a.checked_div(val_b).unwrap_or_default(), MathOp::Div => val_a.checked_div(val_b).unwrap_or_default(),
MathOp::Rem => val_a.checked_rem(val_b).unwrap_or_default(), MathOp::Rem => val_a.checked_rem(val_b).unwrap_or_default(),
}; };
println!("{op:?} a:{val_a} b:{val_b}"); // println!("{op:?} a:{val_a} b:{val_b}");
*self.grid.get_mut(front_pos) = Tile::Marble { value: result, dir }; *self.grid.get_mut(front_pos) = Tile::Marble { value: result, dir };
self.marbles.push(front_pos); self.marbles.push(front_pos);
} }