Skip to content

Commit

Permalink
Merge pull request #180 from kpcyrd/exit
Browse files Browse the repository at this point in the history
Make sn0int more forgiving with accidential ^C
  • Loading branch information
kpcyrd committed Jun 18, 2020
2 parents c5a23a5 + b96eea9 commit 82a2bb1
Showing 1 changed file with 46 additions and 29 deletions.
75 changes: 46 additions & 29 deletions src/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,19 @@ impl Command {
Command::Autoscope.as_str(),
Command::Back.as_str(),
Command::Delete.as_str(),
Command::Exit.as_str(),
Command::Help.as_str(),
Command::Keyring.as_str(),
Command::Mod.as_str(),
Command::Noscope.as_str(),
Command::Pkg.as_str(),
Command::Run.as_str(),
Command::Scope.as_str(),
Command::Set.as_str(),
Command::Select.as_str(),
Command::Workspace.as_str(),
Command::Target.as_str(),
Command::Use.as_str(),
Command::Quickstart.as_str(),
Command::Quit.as_str(),
Command::Workspace.as_str(),
Command::Cal.as_str(),
];
}
Expand Down Expand Up @@ -159,6 +159,7 @@ pub struct Shell<'a> {
// autonoscope: RuleSet,
options: Option<HashMap<String, String>>,
signal_register: Arc<SignalRegister>,
cancel_twice: u8,
}

impl<'a> Shell<'a> {
Expand All @@ -179,6 +180,7 @@ impl<'a> Shell<'a> {
keyring,
options: None,
signal_register: Arc::new(SignalRegister::new()),
cancel_twice: 0,
};

rl.reload_module_cache();
Expand Down Expand Up @@ -291,41 +293,56 @@ impl<'a> Shell<'a> {

pub fn readline(&mut self) -> Option<(Command, Vec<String>)> {
let readline = self.rl.readline(&self.prompt.to_string());

if readline.is_ok() {
self.cancel_twice = 0;
}

match readline {
Ok(ref line) if line.is_empty() => None,
Ok(line) => {
debug!("Readline returned {:?}", line);

self.rl.add_history_entry(line.as_str());
self.cancel_twice = 0;
if line.is_empty() {
None
} else {
debug!("Readline returned {:?}", line);

if line.starts_with('#') {
return None;
}
self.rl.add_history_entry(line.as_str());

let cmd = match shellwords::split(&line) {
Ok(cmd) => cmd,
Err(err) => {
eprintln!("Error: {:?}", err);
if line.starts_with('#') {
return None;
},
};
debug!("shellwords returned {:?}", cmd);

if cmd.is_empty() {
return None;
}

match Command::from_str(&cmd[0]) {
Ok(x) => Some((x, cmd)),
Err(err) => {
eprintln!("Error: {}", err);
None
},
}

let cmd = match shellwords::split(&line) {
Ok(cmd) => cmd,
Err(err) => {
eprintln!("Error: {:?}", err);
return None;
},
};
debug!("shellwords returned {:?}", cmd);

if cmd.is_empty() {
return None;
}

match Command::from_str(&cmd[0]) {
Ok(x) => Some((x, cmd)),
Err(err) => {
eprintln!("Error: {}", err);
None
},
}
}
}
Err(ReadlineError::Interrupted) => {
// ^C
Some((Command::Interrupt, vec![]))
self.cancel_twice += 1;

if self.cancel_twice > 1 {
Some((Command::Interrupt, vec![]))
} else {
None
}
}
Err(ReadlineError::Eof) => {
// ^D
Expand Down

0 comments on commit 82a2bb1

Please sign in to comment.