add append command

This commit is contained in:
rick 2023-04-23 01:11:46 +02:00
parent f9bb10d104
commit 394232a497
Signed by: Rick
GPG key ID: 5CBE8779CD27BCBA
3 changed files with 52 additions and 1 deletions

View file

@ -28,6 +28,23 @@ impl Buffer {
self.current_line = new; self.current_line = new;
} }
pub fn append(&mut self, addr: Address, lines: Vec<String>) {
self.has_changed = true;
match addr {
Address::Actual => self.append_buffer(self.current_line, lines),
Address::Line(l) => self.append_buffer(l, lines),
Address::Last => self.append_buffer(self.buffer.len(), lines),
_ => (),
}
}
fn append_buffer(&mut self, line: usize, lines: Vec<String>) {
for (i, l) in lines.iter().enumerate() {
self.buffer.insert(i + line, Ok(l.to_string()));
self.current_line = i + line;
}
}
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 {

View file

@ -15,6 +15,15 @@ pub enum Address {
Null, Null,
} }
impl Address {
pub fn is_multi_line(&self) -> bool {
match self {
Address::Null | Address::Actual | Address::Last | Address::Line(_) => false,
_ => true,
}
}
}
pub fn parse_address(addr: &str) -> Address { pub fn parse_address(addr: &str) -> Address {
let mut tmp = String::from(addr); let mut tmp = String::from(addr);
tmp.retain(|c| c != ' '); tmp.retain(|c| c != ' ');
@ -138,6 +147,13 @@ 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' => {
if addr.is_multi_line() {
Err(String::from("Erreur d'adresse"))
} else {
Ok(Command::Append(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

@ -45,6 +45,24 @@ fn execute_command(buffer: &mut Buffer, c: Command) -> bool {
let mut ret = false; let mut ret = false;
match c { match c {
Command::Append(a) => {
let mut new_lines: Vec<String> = Vec::new();
let mut buffer_input = String::new();
while buffer_input != "." {
buffer_input.clear();
match io::stdin().read_line(&mut buffer_input) {
Ok(_) => {
buffer_input.pop();
if buffer_input != "." {
new_lines.push(buffer_input.clone());
}
},
Err(e) => panic!("{}", e),
}
}
buffer.append(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),
Command::Quit(_) => ret = true, Command::Quit(_) => ret = true,
@ -56,7 +74,7 @@ fn execute_command(buffer: &mut Buffer, c: Command) -> bool {
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)>("pnqQ"); let command = one_of::<_, _, (&str, ErrorKind)>("apnqQ");
//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);