forked from mars/breed
better command parsing with file name argument for :w
This commit is contained in:
parent
aee17365c6
commit
0ff694c6c1
46
src/main.rs
46
src/main.rs
|
@ -433,20 +433,40 @@ impl State {
|
|||
}
|
||||
|
||||
fn execute_command(&mut self, command: &str) -> std::result::Result<(), String> {
|
||||
match command {
|
||||
"q" => self.quit = true,
|
||||
"w" => {
|
||||
let handle = self.file.clone().unwrap_or_else(|| {
|
||||
OsString::from("file")
|
||||
});
|
||||
let command_parts = command.split(' ').collect::<Vec<&str>>();
|
||||
|
||||
self.write_buffer(handle)?;
|
||||
},
|
||||
command => {
|
||||
return Err(
|
||||
format!("{:?}: Unrecognized command.", command)
|
||||
);
|
||||
},
|
||||
let commands = match command_parts.get(0) {
|
||||
Some(parts) => parts.chars().collect::<Vec<char>>(),
|
||||
None => return Ok(()),
|
||||
};
|
||||
|
||||
for command in commands.clone().iter() {
|
||||
match command {
|
||||
'q' => self.quit = true,
|
||||
'w' => {
|
||||
let handle: OsString;
|
||||
|
||||
if let Some(part) = self.file.clone() {
|
||||
handle = part;
|
||||
} else {
|
||||
handle = match command_parts.get(1) {
|
||||
Some(part) => OsString::from(part),
|
||||
None => {
|
||||
return Err(
|
||||
format!("{}: No file name.", command)
|
||||
);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
self.write_buffer(handle)?;
|
||||
},
|
||||
command => {
|
||||
return Err(
|
||||
format!("{}: Unrecognized command.", command)
|
||||
);
|
||||
},
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
Reference in New Issue