Skip to content

Commit

Permalink
Merge pull request #2 from nils865/main
Browse files Browse the repository at this point in the history
Improved Features
  • Loading branch information
nils865 authored Sep 4, 2023
2 parents 7575140 + 97526f6 commit 419acfc
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ A new Shell written in rust
- [X] Add basic shell functionality
- [ ] Reimplement alot of basic shell features
- [ ] echo
- [ ] ls
- [X] ls
- [ ] cp
- [ ] mv
- [ ] cd
- [ ] exit
- [X] cd
- [X] exit
- [ ] Add theme support
- [ ] Add plugin support
- [ ] Add config files
Expand Down
42 changes: 32 additions & 10 deletions src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use std::process::Command;

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

use self::{cd::cmd_cd, ls::cmd_ls};

fn args_handler(args: &Vec<&str>) -> Vec<String> {
let mut i = 1;

let mut cmd_args: Vec<String> = vec![];

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

Expand All @@ -22,15 +27,27 @@ fn syscalls(args: Vec<&str>) -> i8 {
}
}

if arg.starts_with("\"") {
arg = arg.strip_prefix("\"").unwrap().to_string();
}

if arg.ends_with("\"") {
arg = arg.strip_suffix("\"").unwrap().to_string();
}

cmd_args.push(arg);

i += 1;
}

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

fn syscalls(command: &str, args: &Vec<String>) -> i8 {
let mut cmd = Command::new(command);

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

return match cmd.status() {
Expand All @@ -42,16 +59,21 @@ fn syscalls(args: Vec<&str>) -> i8 {
}
}
Err(_) => {
println!("Command not found: {}", args[0]);
println!("Command not found: {}", command);
1
}
};
}

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

_ => return syscalls(args),
}
return match command {
"cd" => cmd_cd(&arguments),
"ls" => cmd_ls(&arguments),
"exit" => -1,

_ => syscalls(&command, &arguments),
};
}
16 changes: 16 additions & 0 deletions src/commands/cd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use std::{env, path::Path};

pub fn cmd_cd(args: &Vec<String>) -> i8 {
if args.len() == 0 {
env::set_current_dir(Path::new(env::var("HOME").unwrap().as_str())).unwrap();
return 0;
}

match env::set_current_dir(Path::new(args[0].as_str())) {
Ok(_) => return 0,
Err(_) => {
println!("cd: Directory \"{}\" not Found", args[0]);
return 1;
}
}
}
43 changes: 43 additions & 0 deletions src/commands/ls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use colored::Colorize;
use std::{env, fs, path::Path};

pub fn cmd_ls(args: &Vec<String>) -> i8 {
let dir: String;

if args.len() == 0 {
dir = env::current_dir().unwrap().display().to_string();
} else if args.len() == 1 {
dir = args[0].clone();
} else {
println!("ls: Too many Arguments Provided");
return 1;
}

let files = match fs::read_dir(Path::new(&dir)) {
Ok(res) => res,
Err(_) => {
println!("ls: Directory \"{}\" not Found", dir);
return 1;
}
};

for file in files {
let path = file.unwrap().path();

let name = String::from(
path.display()
.to_string()
.split("/")
.last()
.unwrap_or_default(),
);

if path.is_dir() {
println!("{}", name.bright_cyan().bold());
} else {
println!("{}", name);
}
}

return 0;
}
24 changes: 21 additions & 3 deletions src/prompt.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
use colored::Colorize;
use std::env;

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

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

return prompt;
let dir = env::current_dir().unwrap().display().to_string();

let mut display_dir = dir.split("/").last().unwrap();

if display_dir == "" {
display_dir = "/";
} else if dir == env::var("HOME").unwrap() {
display_dir = "~";
}

prompt.push_str(
&format!("{} ", display_dir)
.bright_cyan()
.to_string()
.clone(),
);

return prompt.bold().to_string();
}

0 comments on commit 419acfc

Please sign in to comment.