Skip to content

Commit 25630cb

Browse files
committed
added some builtincommands
1 parent 83762a6 commit 25630cb

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

src/main.rs

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,61 @@
11
#[allow(unused_imports)]
22
use std::io::{self, Write};
33

4+
const BUILT_IN_COMMANDS: [&str;3] = ["echo","exit","type"];
5+
6+
enum Command{
7+
ExitCommand,
8+
EchoCommand {display_string:String},
9+
TypeCommand {command_name: String},
10+
CommandNotFound,
11+
}
12+
13+
impl Command{
14+
fn from_input(input:&str) -> Self {
15+
let input=input.trim();
16+
if input == "exit" || input == "exit 0" {
17+
return Self::ExitCommand;
18+
};
19+
if let Some(pos) = input.find("echo ") {
20+
if pos ==0{
21+
return Self::EchoCommand{
22+
display_string: input["echo ".len()..].to_string(),
23+
};
24+
}
25+
}
26+
if let Some(pos) = input.find("type "){
27+
if pos==0 {
28+
return Self::TypeCommand{
29+
command_name: input["type ".len()..].to_string(),
30+
};
31+
}
32+
}
33+
Self::CommandNotFound // we are returning this value
34+
}
35+
36+
}
37+
438
fn main() {
539
loop{
640
print!("$ ");
741
io::stdout().flush().unwrap();
842

9-
let mut command = String::new();
10-
io::stdin().read_line(&mut command).unwrap();
11-
println!("{}: command not found",command.trim());
43+
let mut input = String::new();
44+
io::stdin().read_line(&mut input).unwrap();
45+
46+
//implementing internal built_in_commands
47+
let command = Command::from_input(&input);
48+
match command{
49+
Command::ExitCommand => break,
50+
Command::EchoCommand {display_string} => println!("{}", display_string),
51+
Command::TypeCommand {command_name} => {
52+
if BUILT_IN_COMMANDS.contains(&command_name.as_str()){
53+
println!("{} is a shell builtin",command_name);
54+
} else {
55+
println!("{}: command not found", command_name);
56+
}
57+
},
58+
Command::CommandNotFound => println!("{} bruh command not found", input.trim()),
59+
}
1260
}
1361
}

0 commit comments

Comments
 (0)