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

Parse commands as strings + wq

This commit is contained in:
mars 2023-04-12 17:27:01 -04:00
parent 5bf1212b2a
commit 1f33402a42
1 changed files with 27 additions and 24 deletions

View File

@ -333,39 +333,42 @@ impl State {
Ok(()) Ok(())
} }
fn write_command(&mut self, command: &str, args: &[&str]) -> std::result::Result<(), String> {
let handle = match self.file.clone() {
Some(handle) => handle,
None => match args.get(0) {
Some(part) => OsString::from(part),
None => {
return Err(format!("{}: No file name.", command));
}
},
};
self.write_buffer(handle).map_err(|err| format!("{}", err))
}
fn execute_command(&mut self, command: &str) -> std::result::Result<(), String> { fn execute_command(&mut self, command: &str) -> std::result::Result<(), String> {
let command_parts = command.split(' ').collect::<Vec<&str>>(); let command_parts = command.split(' ').collect::<Vec<&str>>();
let commands = match command_parts.get(0) { let command = match command_parts.get(0) {
Some(parts) => parts.chars().collect::<Vec<char>>(), Some(command) => command,
None => return Ok(()), None => return Ok(()),
}; };
for command in commands.clone().iter() { let args = &command_parts[1..];
match command {
'q' => self.quit = true,
'w' => {
let handle: OsString;
if let Some(part) = self.file.clone() { match *command {
handle = part; "q" => self.quit = true,
} else { "w" => return self.write_command(command, args),
handle = match command_parts.get(1) { "wq" => {
Some(part) => OsString::from(part), self.write_command(command, args)?;
None => { self.quit = true;
return Err(format!("{}: No file name.", command)); }
} command => {
}; return Err(format!("{}: Unrecognized command.", command));
}
self.write_buffer(handle)
.map_err(|err| format!("{}", err))?;
}
command => {
return Err(format!("{}: Unrecognized command.", command));
}
} }
} }
Ok(()) Ok(())
} }