add append and refactor function for user input
add append command fix problem where insert = append create a function for reading user input (insert and append)
This commit is contained in:
parent
fde859a48a
commit
f0f059ea32
3 changed files with 40 additions and 20 deletions
|
@ -69,6 +69,16 @@ impl Buffer {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn append(&mut self, addr:Address, lines: Vec<String>) {
|
||||
self.has_changed = true;
|
||||
match addr {
|
||||
Address::Actual | Address::Empty => self.insert_buffer(self.current_line + 1, lines),
|
||||
Address::Line(l) => self.insert_buffer(l, lines),
|
||||
Address::Last => self.insert_buffer(self.buffer.len(), lines),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, addr: Address, lines: Vec<String>) {
|
||||
self.has_changed = true;
|
||||
match addr {
|
||||
|
|
|
@ -158,13 +158,20 @@ fn parse_without_addr(c: char) -> Command {
|
|||
|
||||
fn parse(c: char, addr: Address) -> Result<Command, String> {
|
||||
match c {
|
||||
'i' => {
|
||||
'a' => {
|
||||
if addr.is_multi_line() {
|
||||
Err(String::from("Erreur d'adresse"))
|
||||
} else {
|
||||
Ok(Command::Append(addr))
|
||||
}
|
||||
}
|
||||
'i' => {
|
||||
if addr.is_multi_line() {
|
||||
Err(String::from("Erreur d'adresse"))
|
||||
} else {
|
||||
Ok(Command::Insert(addr))
|
||||
}
|
||||
}
|
||||
'p' => Ok(Command::Print(addr)),
|
||||
'n' => Ok(Command::Number(addr)),
|
||||
'q' => Ok(Command::Quit(false)),
|
||||
|
|
41
src/main.rs
41
src/main.rs
|
@ -46,28 +46,31 @@ fn check_address_complete(c: char) -> bool {
|
|||
check_address(c) || c == ',' || c == ';'
|
||||
}
|
||||
|
||||
fn read_user_input() -> Vec<String> {
|
||||
let mut buffer: 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 != "." {
|
||||
buffer.push(buffer_input.clone());
|
||||
}
|
||||
}
|
||||
Err(e) => panic!("{}", e),
|
||||
}
|
||||
}
|
||||
|
||||
buffer
|
||||
}
|
||||
|
||||
fn execute_command(buffer: &mut Buffer, c: Command) -> Exit {
|
||||
let mut ret = Exit::Continue;
|
||||
|
||||
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.insert(a, new_lines);
|
||||
}
|
||||
Command::Append(a) => buffer.append(a, read_user_input()),
|
||||
Command::Insert(a) => buffer.insert(a, read_user_input()),
|
||||
Command::Print(a) => buffer.print(a, false),
|
||||
Command::Number(a) => buffer.print(a, true),
|
||||
Command::Quit(q) => {
|
||||
|
@ -93,7 +96,7 @@ fn execute_command(buffer: &mut Buffer, c: Command) -> Exit {
|
|||
|
||||
fn parse_line(input: &str) -> Result<Command, String> {
|
||||
let address = take_while(check_address_complete);
|
||||
let command = one_of::<_, _, (&str, ErrorKind)>("ipnqQw");
|
||||
let command = one_of::<_, _, (&str, ErrorKind)>("aipnqQw");
|
||||
|
||||
//if let Ok((input, (a, c))) = alt(address, command).parse(input) {
|
||||
let tmp = (address, command).parse(input);
|
||||
|
|
Loading…
Reference in a new issue