Skip to content

Commit

Permalink
Command arguments work
Browse files Browse the repository at this point in the history
  • Loading branch information
adamperkowski committed Jul 14, 2024
1 parent 1d1c07f commit ddcdab1
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
41 changes: 34 additions & 7 deletions shell/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,36 @@ use std::process;

pub struct Command {
pub name: &'static str,
pub args: &'static str,
pub doc: &'static str,
pub fun: fn() -> i32,
pub fun: fn(Vec<&str>) -> i32,
}

fn clrs() -> i32 {
fn clrs(_args: Vec<&str>) -> i32 {
print!("\x1B[2J\x1B[1;1H");
return 1;
}

fn help() -> i32 {
fn help(_args: Vec<&str>) -> i32 {
println!(
"HighlightOS Shell
List of commands:"
);

for cmd in COMMAND_LIST {
println!(". {} >> {}", cmd.name, cmd.doc);
println!(". {} {} >> {}", cmd.name, cmd.args, cmd.doc);
}

return 0;
}

fn test() -> i32 {
fn test(_args: Vec<&str>) -> i32 {
println!("hello. this is a test command. it's life goal is to always return 2.");
return 2;
}

fn cc() -> i32 {
fn cc(_args: Vec<&str>) -> i32 {
println!(
"Copyright (C) 2024 Adam Perkowski
Expand All @@ -55,7 +56,7 @@ fn cc() -> i32 {
return 0;
}

fn exit_hls() -> i32 {
fn exit_hls(_args: Vec<&str>) -> i32 {
print!("are you sure you want to exit? [ y/N ] < ");

let mut inpt = String::new();
Expand All @@ -73,30 +74,56 @@ fn exit_hls() -> i32 {
}
}

fn document(_args: Vec<&str>) -> i32 {
if _args.len() != 0 {
if let Some(command) = COMMAND_LIST.iter().find(|&cmd| cmd.name == _args[0]) {
println!("{} >> {}", command.name, command.doc);
return 0;
} else {
println!("Command not found.");
return 3;
}
} else {
println!("No command specified.");
return 4;
}
}

pub const COMMAND_LIST: &[Command] = &[
Command {
name: "clrs",
args: "",
doc: "clear screen",
fun: clrs,
},
Command {
name: "help",
args: "",
doc: "show list of commands",
fun: help,
},
Command {
name: "test",
args: "",
doc: "test :)",
fun: test,
},
Command {
name: "cc",
args: "",
doc: "display copyright info",
fun: cc,
},
Command {
name: "exit",
args: "",
doc: "exit the shell :((",
fun: exit_hls,
},
Command {
name: "getdoc",
args: "[cmd]",
doc: "display doc of selected command",
fun: document,
},
];
18 changes: 15 additions & 3 deletions shell/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct RtrType {
}

fn main() {
(COMMAND_LIST[0].fun)();
// (COMMAND_LIST[0].fun)();

println!("HighlightOS Shell\nversion {}\n", env!("CARGO_PKG_VERSION"));

Expand All @@ -27,8 +27,12 @@ fn main() {

inpt.pop();

if let Some(command) = COMMAND_LIST.iter().find(|&cmd| cmd.name == inpt) {
let rtr = (command.fun)();
let mut args: Vec<&str> = inpt.split(" ").collect();

if let Some(command) = COMMAND_LIST.iter().find(|&cmd| cmd.name == args[0]) {
args.remove(0);

let rtr = (command.fun)(args);

if rtr != 1 {
if let Some(return_code) = RTR_LIST.iter().find(|&rtr_t| rtr_t.code == &rtr) {
Expand All @@ -52,4 +56,12 @@ const RTR_LIST: &[RtrType] = &[
code: &2,
info: "returned general error",
},
RtrType {
code: &3,
info: "returned critical error",
},
RtrType {
code: &4,
info: "returned user error",
},
];

0 comments on commit ddcdab1

Please sign in to comment.