emma
/
breed
Archived
forked from mars/breed
1
0
Fork 0

better command parsing with file name argument for :w

This commit is contained in:
Emma Tebibyte 2023-04-12 11:52:21 -04:00
parent aee17365c6
commit 0ff694c6c1
1 changed files with 33 additions and 13 deletions

View File

@ -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(())
}