move print to buffer, remove useless print

This commit is contained in:
rick 2023-04-10 23:15:33 +02:00
parent ccc536ec6a
commit 1de2f2c5e0
Signed by: Rick
GPG key ID: 5CBE8779CD27BCBA
2 changed files with 20 additions and 23 deletions

View file

@ -1,3 +1,4 @@
use crate::enums::Address;
use std::io; use std::io;
pub struct Buffer { pub struct Buffer {
@ -17,21 +18,32 @@ impl Buffer {
} }
// Return the actual line of the buffer // Return the actual line of the buffer
pub fn act(&self) -> usize { pub fn line(&self) -> usize {
self.current_line self.current_line
} }
// change la ligne actuelle // change la ligne actuelle
pub fn change_act(&mut self, new: usize) { pub fn change_line(&mut self, new: usize) {
// TODO check si > len // TODO check si > len
self.current_line = new; self.current_line = new;
} }
pub fn print(&mut self, addr: Address, print_numbers: bool) {
match addr {
Address::Actual => self.print_line(self.current_line, print_numbers),
Address::Line(l) => self.print_line(l.saturating_sub(1), print_numbers),
Address::Null => println!("nul !"),
_ => println!("coucou2"),
}
}
// affiche une ligne // affiche une ligne
pub fn print_line(&self, nb: usize, print_numbers: bool) { fn print_line(&mut self, nb: usize, print_numbers: bool) {
if nb >= self.buffer.len() { if nb >= self.buffer.len() {
println!("?"); println!("?");
} else if let Ok(line) = &self.buffer[nb] { } else if let Ok(line) = &self.buffer[nb] {
self.current_line = nb;
if print_numbers { if print_numbers {
println!("{}\t{}", nb + 1, line); println!("{}\t{}", nb + 1, line);
} else { } else {
@ -43,7 +55,7 @@ impl Buffer {
} }
// affiche plusieurs lignes // affiche plusieurs lignes
pub fn print_range(&self, first: usize, last: usize, print_numbers: bool) { fn print_range(&mut self, first: usize, last: usize, print_numbers: bool) {
for i in first..last { for i in first..last {
self.print_line(i, print_numbers); self.print_line(i, print_numbers);
} }

View file

@ -67,23 +67,14 @@ fn check_address_complete(c: char) -> bool {
check_address(c) || c == ',' check_address(c) || c == ','
} }
fn print(buffer: &Buffer, addr: Address, print_numbers: bool) {
match addr {
Address::Actual => buffer.print_line(buffer.act(), print_numbers),
Address::Null => println!("nul !"),
_ => println!("coucou2"),
}
}
fn execute_command(buffer: &mut Buffer, c: Command) -> bool { fn execute_command(buffer: &mut Buffer, c: Command) -> bool {
println!("coucou {:?}", c);
let mut ret = false; let mut ret = false;
match c { match c {
Command::Print(a) => print(buffer, a, false), Command::Print(a) => buffer.print(a, false),
Command::Number(a) => print(buffer, a, true), Command::Number(a) => buffer.print(a, true),
Command::Quit(_) => ret = true, Command::Quit(_) => ret = true,
_ => println!("coucou"), _ => (),
} }
ret ret
@ -96,14 +87,8 @@ fn parse_line(input: &str) -> Result<Command, String> {
//if let Ok((input, (a, c))) = alt(address, command).parse(input) { //if let Ok((input, (a, c))) = alt(address, command).parse(input) {
let tmp = (address, command).parse(input); let tmp = (address, command).parse(input);
if let Ok((input, (a, c))) = tmp { if let Ok((input, (a, c))) = tmp {
println!("Ok");
//println!("{:?} - {:?} - {:?}", input, a, c);
let addr = parse_address(a); let addr = parse_address(a);
Ok(parse_command(c, addr)) Ok(parse_command(c, addr))
} else if let Err(Error(("bc", ErrorKind::OneOf))) = tmp {
//println!("{:?}", e);
println!("?");
Err(String::from("bruh moment"))
} else if let Err(Error((_, t))) = tmp { } else if let Err(Error((_, t))) = tmp {
println!("?"); println!("?");
Err(String::from("bruh moment")) Err(String::from("bruh moment"))
@ -136,7 +121,7 @@ fn main() {
quit = true; quit = true;
} }
} else { } else {
println!("coucou"); //println!("coucou");
} }
}, },
Err(e) => println!("error"), Err(e) => println!("error"),