move print to buffer, remove useless print
This commit is contained in:
parent
ccc536ec6a
commit
1de2f2c5e0
2 changed files with 20 additions and 23 deletions
|
@ -1,3 +1,4 @@
|
|||
use crate::enums::Address;
|
||||
use std::io;
|
||||
|
||||
pub struct Buffer {
|
||||
|
@ -17,21 +18,32 @@ impl Buffer {
|
|||
}
|
||||
|
||||
// Return the actual line of the buffer
|
||||
pub fn act(&self) -> usize {
|
||||
pub fn line(&self) -> usize {
|
||||
self.current_line
|
||||
}
|
||||
|
||||
// change la ligne actuelle
|
||||
pub fn change_act(&mut self, new: usize) {
|
||||
pub fn change_line(&mut self, new: usize) {
|
||||
// TODO check si > len
|
||||
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
|
||||
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() {
|
||||
println!("?");
|
||||
} else if let Ok(line) = &self.buffer[nb] {
|
||||
self.current_line = nb;
|
||||
|
||||
if print_numbers {
|
||||
println!("{}\t{}", nb + 1, line);
|
||||
} else {
|
||||
|
@ -43,7 +55,7 @@ impl Buffer {
|
|||
}
|
||||
|
||||
// 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 {
|
||||
self.print_line(i, print_numbers);
|
||||
}
|
||||
|
|
23
src/main.rs
23
src/main.rs
|
@ -67,23 +67,14 @@ fn check_address_complete(c: char) -> bool {
|
|||
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 {
|
||||
println!("coucou {:?}", c);
|
||||
let mut ret = false;
|
||||
|
||||
match c {
|
||||
Command::Print(a) => print(buffer, a, false),
|
||||
Command::Number(a) => print(buffer, a, true),
|
||||
Command::Print(a) => buffer.print(a, false),
|
||||
Command::Number(a) => buffer.print(a, true),
|
||||
Command::Quit(_) => ret = true,
|
||||
_ => println!("coucou"),
|
||||
_ => (),
|
||||
}
|
||||
|
||||
ret
|
||||
|
@ -96,14 +87,8 @@ fn parse_line(input: &str) -> Result<Command, String> {
|
|||
//if let Ok((input, (a, c))) = alt(address, command).parse(input) {
|
||||
let tmp = (address, command).parse(input);
|
||||
if let Ok((input, (a, c))) = tmp {
|
||||
println!("Ok");
|
||||
//println!("{:?} - {:?} - {:?}", input, a, c);
|
||||
let addr = parse_address(a);
|
||||
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 {
|
||||
println!("?");
|
||||
Err(String::from("bruh moment"))
|
||||
|
@ -136,7 +121,7 @@ fn main() {
|
|||
quit = true;
|
||||
}
|
||||
} else {
|
||||
println!("coucou");
|
||||
//println!("coucou");
|
||||
}
|
||||
},
|
||||
Err(e) => println!("error"),
|
||||
|
|
Loading…
Reference in a new issue