diff options
| Commit | c8955f48580db8067765f76c8967e44dbb06517a (patch) | |
|---|---|---|
| Author | 2025-02-14 15:34:23 +0200 | |
| Committer | 2025-02-14 15:34:23 +0200 | |
| Files | c3d21c730957dfdc9fe8a6262caa6ae030902076 /vm/src/vmm/mod.rs | |
| tonkobali-c8955f48580db8067765f76c8967e44dbb06517a.tar tonkobali-c8955f48580db8067765f76c8967e44dbb06517a.tar.gz tonkobali-c8955f48580db8067765f76c8967e44dbb06517a.zip | ||
| Parent | 991e6fdd96296bf7bcbee10c47c4f56dd1f83cf9 (diff) | |
feat(vmm): User interface commands
1 files changed, 39 insertions, 8 deletions
| -rw-r--r-- | vm/src/vmm/mod.rs | 47 |
diff --git a/vm/src/vmm/mod.rs b/vm/src/vmm/mod.rs index ac08ffe..afd8840 100644 --- a/vm/src/vmm/mod.rs +++ b/vm/src/vmm/mod.rs @@ -1,7 +1,8 @@ -use std::{ - thread, - time -}; +mod command; + +use std::{ thread, time, sync::mpsc, io::{ stdout, stdin, Write } }; + +use command::parse_and_exec; use super::vm::VirtualMachine; @@ -9,6 +10,12 @@ pub(crate) struct VirtualMachineMonitor { refresh_rate: u64, } +macro_rules! above { + ($str:expr) => { + concat!("\x1B7\x1B[1F|", $str, "|\x1B8") + }; +} + impl VirtualMachineMonitor { pub(crate) fn new(refresh_rate: u64) -> VirtualMachineMonitor { VirtualMachineMonitor { @@ -16,16 +23,40 @@ impl VirtualMachineMonitor { } } - pub(crate) fn start(&mut self, vm: VirtualMachine) { + pub(crate) fn start(&mut self, mut vm: VirtualMachine) { + // Spawn input handling thread + let (tx, rx) = mpsc::channel::<String>(); + thread::spawn(move || loop { + let mut buffer = String::new(); + stdin().read_line(&mut buffer).unwrap(); + tx.send(buffer).unwrap(); + }); + + print!("\n> "); loop { + match rx.try_recv() { + Ok(input) => { + if let Err(msg) = parse_and_exec(input, &mut vm) { + if msg.is_empty() { + break; + } + else { + println!("{}", msg); + } + } + print!("\n> "); + }, + _ => {}, + } + { let vm = (*vm).lock().unwrap(); - println!("{}", vm.get_word(1)); if vm.is_halted() { - println!("HALTED!"); - break; + print!(above!("HALTED!")); } } + + let _ = stdout().flush(); thread::sleep(time::Duration::from_millis(self.refresh_rate)); } } |
