add exit mode to use q and Q
This commit is contained in:
parent
394232a497
commit
a265b3a890
1 changed files with 29 additions and 10 deletions
39
src/main.rs
39
src/main.rs
|
@ -17,6 +17,13 @@ use nom::Err::Error;
|
||||||
|
|
||||||
const FILE : &'static str = "test";
|
const FILE : &'static str = "test";
|
||||||
|
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
enum Exit {
|
||||||
|
Continue,
|
||||||
|
HasBeenModified,
|
||||||
|
Quit,
|
||||||
|
}
|
||||||
|
|
||||||
// TODO juste $ pour l'instant dans l'adresse
|
// TODO juste $ pour l'instant dans l'adresse
|
||||||
// TODO faire les commandes mais juste p pour l'instant
|
// 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
|
// 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 == ';'
|
check_address(c) || c == ',' || c == ';'
|
||||||
}
|
}
|
||||||
|
|
||||||
fn execute_command(buffer: &mut Buffer, c: Command) -> bool {
|
fn execute_command(buffer: &mut Buffer, c: Command) -> Exit {
|
||||||
let mut ret = false;
|
let mut ret = Exit::Continue;
|
||||||
|
|
||||||
match c {
|
match c {
|
||||||
Command::Append(a) => {
|
Command::Append(a) => {
|
||||||
|
@ -65,7 +72,15 @@ fn execute_command(buffer: &mut Buffer, c: Command) -> bool {
|
||||||
},
|
},
|
||||||
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(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);
|
let mut buffer_struct = Buffer::new(buffer);
|
||||||
|
|
||||||
println!("fichier lu !");
|
println!("fichier lu !");
|
||||||
let mut quit = false;
|
let mut pred = Exit::Continue;
|
||||||
|
|
||||||
while !quit {
|
while pred != Exit::Quit {
|
||||||
let mut input = String::new();
|
let mut input = String::new();
|
||||||
match io::stdin().read_line(&mut input) {
|
match io::stdin().read_line(&mut input) {
|
||||||
Ok(n) => {
|
Ok(n) => {
|
||||||
let result_parse = parse_line(&input);
|
let result_parse = parse_line(&input);
|
||||||
if let Ok(c) = result_parse {
|
if let Ok(c) = result_parse {
|
||||||
let tmp = execute_command(&mut buffer_struct, c);
|
pred = match execute_command(&mut buffer_struct, c) {
|
||||||
if tmp {
|
Exit::HasBeenModified => if pred == Exit::HasBeenModified {
|
||||||
quit = true;
|
Exit::Quit
|
||||||
|
} else {
|
||||||
|
Exit::HasBeenModified
|
||||||
|
},
|
||||||
|
exit => exit,
|
||||||
}
|
}
|
||||||
} else if let Err(e) = result_parse {
|
} else if let Err(e) = result_parse {
|
||||||
println!("?");
|
println!("?");
|
||||||
println!("{}", e);
|
println!("{}", e);
|
||||||
|
pred = Exit::Continue;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(e) => println!("error"),
|
Err(e) => panic!("{}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue