add delete command

This commit is contained in:
rick 2023-04-23 19:17:13 +02:00
parent 4a583272d6
commit e3af34a827
Signed by: Rick
GPG key ID: 5CBE8779CD27BCBA
3 changed files with 27 additions and 1 deletions

View file

@ -71,6 +71,30 @@ impl Buffer {
} }
} }
pub fn delete(&mut self, addr:Address) {
self.has_changed = true;
let curr = self.current_line;
let last_line = self.buffer.len();
match addr {
Address::Actual | Address::Empty => self.delete_lines(curr, curr.saturating_add(1)),
Address::Line(l) => self.delete_lines(l.saturating_sub(1), l),
Address::Last => self.delete_lines(last_line.saturating_sub(1), last_line),
Address::All => self.delete_lines(0, last_line),
Address::FromTo(a, b) => self.delete_lines(a.saturating_sub(1), b),
Address::FromStartTo(a) => self.delete_lines(0, a),
Address::FromToEnd(a) => self.delete_lines(a.saturating_sub(1), last_line),
Address::FromActTo(a) => self.delete_lines(curr, a),
Address::FromActToEnd => self.delete_lines(curr, last_line),
Address::Null => println!("nul !"),
}
}
fn delete_lines(&mut self, start: usize, end: usize) {
for i in start..end {
self.buffer.remove(start);
}
}
pub fn append(&mut self, addr:Address, lines: Vec<String>) { pub fn append(&mut self, addr:Address, lines: Vec<String>) {
self.has_changed = true; self.has_changed = true;
match addr { match addr {

View file

@ -172,6 +172,7 @@ fn parse(c: char, addr: Address) -> Result<Command, String> {
Ok(Command::Insert(addr)) Ok(Command::Insert(addr))
} }
} }
'd' => Ok(Command::Delete(addr)),
'p' => Ok(Command::Print(addr)), 'p' => Ok(Command::Print(addr)),
'n' => Ok(Command::Number(addr)), 'n' => Ok(Command::Number(addr)),
'q' => Ok(Command::Quit(false)), 'q' => Ok(Command::Quit(false)),

View file

@ -71,6 +71,7 @@ fn execute_command(buffer: &mut Buffer, c: Command) -> Exit {
match c { match c {
Command::Append(a) => buffer.append(a, read_user_input()), Command::Append(a) => buffer.append(a, read_user_input()),
Command::Insert(a) => buffer.insert(a, read_user_input()), Command::Insert(a) => buffer.insert(a, read_user_input()),
Command::Delete(a) => buffer.delete(a),
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),
Command::Quit(q) => { Command::Quit(q) => {
@ -96,7 +97,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)>("aipnqQw"); let command = one_of::<_, _, (&str, ErrorKind)>("aidpnqQw");
//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);