62 lines
1.5 KiB
Rust
62 lines
1.5 KiB
Rust
use std::env;
|
|
use std::fs::File;
|
|
use std::io::BufReader;
|
|
use std::io::prelude::*;
|
|
use std::io;
|
|
use nom::bytes::complete::{tag, take_while, take_till};
|
|
use nom::character::complete::one_of;
|
|
use nom::sequence::Tuple;
|
|
use nom::branch::alt;
|
|
use nom::error::ErrorKind;
|
|
use nom::error::Error;
|
|
|
|
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 act(&self) -> usize {
|
|
self.current_line
|
|
}
|
|
|
|
// change la ligne actuelle
|
|
pub fn change_act(&mut self, new: usize) {
|
|
// TODO check si > len
|
|
self.current_line = new;
|
|
}
|
|
|
|
// affiche une ligne
|
|
pub fn print_line(&self, nb: usize, print_numbers: bool) {
|
|
if nb >= self.buffer.len() {
|
|
println!("?");
|
|
} else if let Ok(line) = &self.buffer[nb] {
|
|
if print_numbers {
|
|
println!("{}\t{}", nb + 1, line);
|
|
} else {
|
|
println!("{}", line);
|
|
}
|
|
} else {
|
|
println!("?");
|
|
}
|
|
}
|
|
|
|
// affiche plusieurs lignes
|
|
pub fn print_range(&self, first: usize, last: usize, print_numbers: bool) {
|
|
for i in first..last {
|
|
self.print_line(i, print_numbers);
|
|
}
|
|
}
|
|
}
|