Skip to content

Commit

Permalink
Merge pull request #1 from nils865/main
Browse files Browse the repository at this point in the history
Added basic shell functionality
  • Loading branch information
nils865 authored Sep 4, 2023
2 parents fd9c20f + b63d8d7 commit 7575140
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 2 deletions.
164 changes: 164 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
colored = "2.0.4"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A new Shell written in rust

## Roadmap

- [ ] Add basic shell functionality
- [X] Add basic shell functionality
- [ ] Reimplement alot of basic shell features
- [ ] echo
- [ ] ls
Expand Down
57 changes: 57 additions & 0 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use std::process::Command;

fn syscalls(args: Vec<&str>) -> i8 {
let mut cmd_args: Vec<String> = vec![];

let mut i = 1;

while i < args.len() {
let mut arg: String = args[i].to_string();

if arg.starts_with("\"") {
i += 1;

for j in i..args.len() {
arg = format!("{} {}", arg, args[j]);

if arg.ends_with("\"") {
break;
}

i += 1;
}
}

cmd_args.push(arg);

i += 1;
}

let mut cmd = Command::new(args[0]);

if cmd_args.len() > 0 {
cmd.args(&cmd_args);
}

return match cmd.status() {
Ok(status) => {
if status.success() {
0
} else {
1
}
}
Err(_) => {
println!("Command not found: {}", args[0]);
1
}
};
}

pub fn input_handler(args: Vec<&str>) -> i8 {
match args[0] {
"exit" => return -1,

_ => return syscalls(args),
}
}
29 changes: 28 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
use std::io::{stdin, stdout, Write};

mod commands;
mod prompt;

use commands::input_handler;
use prompt::get_prompt;

fn main() {
println!("Hello, world!");
let running = true;
let mut exit_code: i8 = 0;

while running {
let mut command = String::new();

print!("{}", get_prompt(exit_code));
stdout().flush().expect("Failed to flush stdout");
stdin()
.read_line(&mut command)
.expect("Failed to read line");

let args: Vec<&str> = command.trim().split(" ").collect();

exit_code = input_handler(args);

if exit_code == -1 {
break;
}
}
}
13 changes: 13 additions & 0 deletions src/prompt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use colored::Colorize;

pub fn get_prompt(exit_code: i8) -> String {
let mut prompt = String::new();

if exit_code == 0 {
prompt.push_str(&"➜ ".green().to_string());
} else {
prompt.push_str(&"➜ ".red().to_string());
}

return prompt;
}

0 comments on commit 7575140

Please sign in to comment.