Playing with tables.

This commit is contained in:
Rhinemann 2023-06-28 15:55:28 +03:00
parent 52065cbc37
commit aef2dc3760
2 changed files with 27 additions and 24 deletions

View File

@ -1,4 +1,4 @@
use prettytable::{Cell, Row, Table};
// use prettytable::{Cell, Row, Table};
use std::collections::VecDeque;
use std::{cmp, fmt};
@ -88,16 +88,21 @@ impl BasicRegister {
self.memory.len()
}
pub fn table(&self) -> Table {
let mut table = Table::new();
let mut memory_row: Vec<Cell> = vec![Cell::new("Memory")];
pub fn table(&self) -> String {
use tabled::{builder::Builder, settings::Style};
let mut m_row: Vec<String> = Vec::new();
self.memory
.iter()
.for_each(|value| memory_row.push(Cell::new((*value as u8).to_string().as_str())));
.for_each(|bit| m_row.push((*bit as u8).to_string()));
let mut builder: Builder = Builder::default();
builder.push_record(
self.memory
.iter()
.map(|bit| (*bit as u8).to_string())
.collect::<Vec<String>>(),
);
table.add_row(Row::new(memory_row));
table
builder.build().with(Style::modern()).to_string()
}
}
@ -124,23 +129,21 @@ impl fmt::Display for BasicRegister {
}
}
/*impl fmt::Binary for BasicRegister {
impl fmt::Binary for BasicRegister {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Memory: ")?;
for v in self.memory.iter() {
write!(f, "{}", *v as u8)?;
}
write!(f, "")
}
}*/
}
/// Converts a u8 number to a boolean.
///
/// # Returns
/// - false - if the number is zero or below.
/// - true - if the number is above zero.
/// * false - if the number is zero or below.
/// * true - if the number is above zero.
fn u8_to_bool(number: u8) -> bool {
number > 0
}

View File

@ -8,21 +8,21 @@ fn input_handler(first_register: bu::BasicRegister, second_register: bu::BasicRe
let (first_register, second_register) = bu::align_registers(first_register, second_register);
println!();
println!("{}", first_register);
println!("{}", second_register);
println!("First register:\n{}", first_register.table());
println!("Second register:\n{}", second_register.table());
loop {
print!("\nChoose the operation:\n[a]ddition, [s]ubtraction, [m]ultiplication, [d]ivision, [q]uit\n>>> ");
let input: String = read!();
match input.as_str() {
"a" => println!(
"Sum:\n{}",
bu::binary_sum(&first_register, &second_register)
),
"s" => println!(
"Subtraction:\n{}",
bu::binary_subtraction(&first_register, &second_register)
),
"a" => {
let sum: bu::BasicRegister = bu::binary_sum(&first_register, &second_register);
println!("Sum:\n{}\nResult (to copy): {sum:b}", sum.table())
}
"s" => {
let difference: bu::BasicRegister = bu::binary_subtraction(&first_register, &second_register);
println!("Difference:\n{}\nResult (to copy): {difference:b}", difference.table())
}
"m" => (),
"d" => (),
"q" => std::process::exit(0x0100),