append become insert

This commit is contained in:
rick 2023-04-23 11:47:50 +02:00
parent 22d7ddca69
commit 112cc09732
Signed by: Rick
GPG key ID: 5CBE8779CD27BCBA
3 changed files with 15 additions and 11 deletions

View file

@ -52,17 +52,17 @@ impl Buffer {
} }
} }
pub fn append(&mut self, addr: Address, lines: Vec<String>) { pub fn insert(&mut self, addr: Address, lines: Vec<String>) {
self.has_changed = true; self.has_changed = true;
match addr { match addr {
Address::Actual => self.append_buffer(self.current_line, lines), Address::Actual | Address::Empty => self.insert_buffer(self.current_line, lines),
Address::Line(l) => self.append_buffer(l, lines), Address::Line(l) => self.insert_buffer(l, lines),
Address::Last => self.append_buffer(self.buffer.len(), lines), Address::Last => self.insert_buffer(self.buffer.len(), lines),
_ => (), _ => (),
} }
} }
fn append_buffer(&mut self, line: usize, lines: Vec<String>) { fn insert_buffer(&mut self, line: usize, lines: Vec<String>) {
for (i, l) in lines.iter().enumerate() { for (i, l) in lines.iter().enumerate() {
self.buffer.insert(i + line, Ok(l.to_string())); self.buffer.insert(i + line, Ok(l.to_string()));
self.current_line = i + line; self.current_line = i + line;
@ -72,13 +72,17 @@ impl Buffer {
pub fn print(&mut self, addr: Address, print_numbers: bool) { pub fn print(&mut self, addr: Address, print_numbers: bool) {
let last_line = self.buffer.len(); let last_line = self.buffer.len();
match addr { match addr {
Address::Actual => self.print_line(self.current_line, print_numbers), Address::Actual | Address::Empty => {
self.print_line(self.current_line, print_numbers);
}
Address::Line(l) => self.print_line(l.saturating_sub(1), print_numbers), Address::Line(l) => self.print_line(l.saturating_sub(1), print_numbers),
Address::Last => self.print_line(last_line, print_numbers), Address::Last => self.print_line(last_line, print_numbers),
Address::All => self.print_range(0, last_line, print_numbers), Address::All => self.print_range(0, last_line, print_numbers),
Address::FromTo(a, b) => self.print_range(a, b, print_numbers), Address::FromTo(a, b) => self.print_range(a.saturating_sub(1), b, print_numbers),
Address::FromStartTo(a) => self.print_range(0, a, print_numbers), Address::FromStartTo(a) => self.print_range(0, a, print_numbers),
Address::FromToEnd(a) => self.print_range(a, last_line, print_numbers), Address::FromToEnd(a) => {
self.print_range(a.saturating_sub(1), last_line, print_numbers)
}
Address::FromActTo(a) => self.print_range(self.current_line, a, print_numbers), Address::FromActTo(a) => self.print_range(self.current_line, a, print_numbers),
Address::FromActToEnd => self.print_range(self.current_line, last_line, print_numbers), Address::FromActToEnd => self.print_range(self.current_line, last_line, print_numbers),
Address::Null => println!("nul !"), Address::Null => println!("nul !"),

View file

@ -147,7 +147,7 @@ fn parse_without_addr(c: char) -> Command {
fn parse(c: char, addr: Address) -> Result<Command, String> { fn parse(c: char, addr: Address) -> Result<Command, String> {
match c { match c {
'a' => { 'i' => {
if addr.is_multi_line() { if addr.is_multi_line() {
Err(String::from("Erreur d'adresse")) Err(String::from("Erreur d'adresse"))
} else { } else {

View file

@ -66,7 +66,7 @@ fn execute_command(buffer: &mut Buffer, c: Command) -> Exit {
} }
} }
buffer.append(a, new_lines); buffer.insert(a, new_lines);
} }
Command::Print(a) => buffer.print(a, false), Command::Print(a) => buffer.print(a, false),
Command::Number(a) => buffer.print(a, true), Command::Number(a) => buffer.print(a, true),
@ -93,7 +93,7 @@ fn execute_command(buffer: &mut Buffer, c: Command) -> Exit {
fn parse_line(input: &str) -> Result<Command, String> { fn parse_line(input: &str) -> Result<Command, String> {
let address = take_while(check_address_complete); let address = take_while(check_address_complete);
let command = one_of::<_, _, (&str, ErrorKind)>("apnqQw"); let command = one_of::<_, _, (&str, ErrorKind)>("ipnqQw");
//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);