64 lines
1.6 KiB
Rust
64 lines
1.6 KiB
Rust
use crate::enums::Address;
|
|
use std::io;
|
|
|
|
pub struct Buffer {
|
|
has_changed: bool,
|
|
buffer: Vec<Result<String, io::Error>>,
|
|
current_line: usize,
|
|
}
|
|
|
|
impl Buffer {
|
|
pub fn new(buffer: Vec<Result<String, io::Error>>) -> Self {
|
|
let tmp = buffer.len().saturating_sub(1);
|
|
Self {
|
|
has_changed: false,
|
|
buffer,
|
|
current_line: tmp
|
|
}
|
|
}
|
|
|
|
// Return the actual line of the buffer
|
|
pub fn line(&self) -> usize {
|
|
self.current_line
|
|
}
|
|
|
|
// change la ligne actuelle
|
|
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
|
|
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 {
|
|
println!("{}", line);
|
|
}
|
|
} else {
|
|
println!("?");
|
|
}
|
|
}
|
|
|
|
// affiche plusieurs lignes
|
|
fn print_range(&mut self, first: usize, last: usize, print_numbers: bool) {
|
|
for i in first..last {
|
|
self.print_line(i, print_numbers);
|
|
}
|
|
}
|
|
}
|