Compare commits
3 commits
ad5e96782a
...
a3b2f3da74
Author | SHA1 | Date | |
---|---|---|---|
a3b2f3da74 | |||
dc0e63b8e6 | |||
056d56b348 |
4 changed files with 55 additions and 9 deletions
|
@ -18,6 +18,45 @@ pub trait Command {
|
|||
fn spawn(&self) -> ExitCode;
|
||||
}
|
||||
|
||||
pub struct CommandSequence {
|
||||
command: Option<Box<dyn Command>>,
|
||||
next_command: Option<Box<dyn Command>>,
|
||||
}
|
||||
|
||||
impl CommandSequence {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
command: None,
|
||||
next_command: None,
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
pub fn add(&self, command: &dyn Command) {
|
||||
if self.command.is_none() {
|
||||
let my_box = Box::new(command.to_owned());
|
||||
self.command = Some(my_box);
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
impl Command for CommandSequence {
|
||||
fn spawn(&self) -> ExitCode {
|
||||
let mut exit_code = ExitCode::new(0);
|
||||
|
||||
if self.command.is_some() {
|
||||
exit_code = self.command.as_ref().unwrap().spawn();
|
||||
}
|
||||
|
||||
if self.next_command.is_some() {
|
||||
exit_code = self.next_command.as_ref().unwrap().spawn();
|
||||
}
|
||||
|
||||
exit_code
|
||||
}
|
||||
}
|
||||
|
||||
pub struct UnixProgram {
|
||||
argv: Vec<String>,
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use crate::command::{Command, ExitCode, UnixProgram};
|
||||
use crate::command::{Command, ExitCode};
|
||||
use crate::interface::{get_user_input, print_prompt};
|
||||
use crate::parser::parse_command_line;
|
||||
|
||||
fn exit(code: &ExitCode) {
|
||||
let code = i32::from(code.get());
|
||||
|
@ -17,15 +18,9 @@ pub fn run() {
|
|||
|
||||
if user_input.is_some() {
|
||||
let user_input = user_input.unwrap();
|
||||
let command_sequence = parse_command_line(user_input);
|
||||
|
||||
let argv: Vec<String> = user_input
|
||||
.split_whitespace()
|
||||
.map(|s| s.to_string())
|
||||
.collect();
|
||||
|
||||
if !argv.is_empty() {
|
||||
current_exit_code = UnixProgram::new().argv(argv).spawn();
|
||||
}
|
||||
current_exit_code = command_sequence.spawn();
|
||||
} else {
|
||||
println!();
|
||||
exit(¤t_exit_code);
|
||||
|
|
|
@ -2,6 +2,7 @@ mod command;
|
|||
mod control;
|
||||
mod error;
|
||||
mod interface;
|
||||
mod parser;
|
||||
|
||||
fn main() {
|
||||
control::run();
|
||||
|
|
11
src/parser.rs
Normal file
11
src/parser.rs
Normal file
|
@ -0,0 +1,11 @@
|
|||
use crate::command::{CommandSequence, UnixProgram};
|
||||
|
||||
pub fn parse_command_line(line: String) -> CommandSequence {
|
||||
let /*mut*/ command_sequence = CommandSequence::new();
|
||||
|
||||
let argv = line.split_whitespace().map(|s| s.to_string()).collect();
|
||||
let _command = UnixProgram::new().argv(argv);
|
||||
// command_sequence.add(command);
|
||||
|
||||
command_sequence
|
||||
}
|
Loading…
Reference in a new issue