add exit mode to use q and Q

This commit is contained in:
rick 2023-04-23 02:59:28 +02:00
parent 394232a497
commit a265b3a890
Signed by: Rick
GPG key ID: 5CBE8779CD27BCBA

View file

@ -17,6 +17,13 @@ use nom::Err::Error;
const FILE : &'static str = "test";
#[derive(PartialEq)]
enum Exit {
Continue,
HasBeenModified,
Quit,
}
// TODO juste $ pour l'instant dans l'adresse
// TODO faire les commandes mais juste p pour l'instant
// TODO faire un système de retour d'erreur pour afficher ? et l'erreur si l'utilisateur le demande
@ -41,8 +48,8 @@ fn check_address_complete(c: char) -> bool {
check_address(c) || c == ',' || c == ';'
}
fn execute_command(buffer: &mut Buffer, c: Command) -> bool {
let mut ret = false;
fn execute_command(buffer: &mut Buffer, c: Command) -> Exit {
let mut ret = Exit::Continue;
match c {
Command::Append(a) => {
@ -65,7 +72,15 @@ fn execute_command(buffer: &mut Buffer, c: Command) -> bool {
},
Command::Print(a) => buffer.print(a, false),
Command::Number(a) => buffer.print(a, true),
Command::Quit(_) => ret = true,
Command::Quit(q) => if buffer.is_modified() {
if q {
ret = Exit::Quit;
} else {
ret = Exit::HasBeenModified;
}
} else {
ret = Exit::Quit;
},
_ => (),
}
@ -99,25 +114,29 @@ fn main() {
let mut buffer_struct = Buffer::new(buffer);
println!("fichier lu !");
let mut quit = false;
let mut pred = Exit::Continue;
while !quit {
while pred != Exit::Quit {
let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(n) => {
let result_parse = parse_line(&input);
if let Ok(c) = result_parse {
let tmp = execute_command(&mut buffer_struct, c);
if tmp {
quit = true;
pred = match execute_command(&mut buffer_struct, c) {
Exit::HasBeenModified => if pred == Exit::HasBeenModified {
Exit::Quit
} else {
Exit::HasBeenModified
},
exit => exit,
}
} else if let Err(e) = result_parse {
println!("?");
println!("{}", e);
pred = Exit::Continue;
}
},
Err(e) => println!("error"),
Err(e) => panic!("{}", e),
}
}
}