Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kruserr committed Oct 12, 2024
1 parent d28281a commit 31cc095
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 37 deletions.
5 changes: 2 additions & 3 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion i6-shell/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ categories = ["command-line-interface", "command-line-utilities", "development-t
workspace = true

[dependencies]
walkdir = "2"
rustyline = "14"
rustyline-derive = "0.10"
chrono = "0.4"
Expand Down
35 changes: 20 additions & 15 deletions i6-shell/src/command.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![allow(non_camel_case_types)]
#![allow(unused_variables)]

use std::{
collections::HashMap,
sync::{Arc, Mutex},
Expand Down Expand Up @@ -35,7 +38,7 @@ impl Command for cd {
let args = input.split(" ").collect::<Vec<&str>>();

let deafult_path = &"";
let mut path = args.get(0).unwrap_or(deafult_path).to_string();
let mut path = args.first().unwrap_or(deafult_path).to_string();

let _ = COMMAND_STATE.lock().map(|mut lock| {
if (path == "-") {
Expand Down Expand Up @@ -73,11 +76,11 @@ impl Command for ls {
let used_args = vec!["-la"];

let mut path = ".";
args.last().map(|x| {
if (is_path(x, used_args)) {
if let Some(x) = args.last() {
if is_path(x, used_args) {
path = x;
}
});
}

let mut items = std::fs::read_dir(path)
.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send>)?
Expand All @@ -91,7 +94,7 @@ impl Command for ls {

if (list_all) {
for i in 0..items.len() {
let metadata = std::fs::metadata(&path)
let metadata = std::fs::metadata(path)
.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send>)?;
let file_type = metadata.file_type();
let permissions = metadata.permissions();
Expand Down Expand Up @@ -141,7 +144,12 @@ impl Command for touch {
input: &str,
) -> Result<String, Box<dyn std::error::Error + Send>> {
let path = std::path::Path::new(input);
match std::fs::OpenOptions::new().create(true).write(true).open(&path) {
match std::fs::OpenOptions::new()
.create(true)
.truncate(false)
.write(true)
.open(path)
{
Ok(file) => {
let now = std::time::SystemTime::now();
file
Expand All @@ -162,7 +170,7 @@ impl Command for mkdir {
input: &str,
) -> Result<String, Box<dyn std::error::Error + Send>> {
let path = std::path::Path::new(input);
std::fs::create_dir_all(&path)
std::fs::create_dir_all(path)
.map_err(|e| Box::new(e) as Box<dyn std::error::Error + Send>)?;
Ok("".into())
}
Expand Down Expand Up @@ -452,15 +460,12 @@ impl Command for grep {
}
}

type CommandResult =
Result<HashMap<String, Box<dyn Command>>, Box<dyn std::error::Error + Send>>;
type CommandMap = Arc<Mutex<CommandResult>>;

lazy_static! {
pub static ref DEFAULT_COMMANDS: Arc<
Mutex<
Result<
HashMap<String, Box<dyn Command>>,
Box<dyn std::error::Error + Send>,
>,
>,
> = Arc::new(Mutex::new({
pub static ref DEFAULT_COMMANDS: CommandMap = Arc::new(Mutex::new({
let mut result: HashMap<String, Box<dyn Command>> = Default::default();

result.insert("cd".to_owned(), Box::new(cd));
Expand Down
25 changes: 11 additions & 14 deletions i6-shell/src/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,8 @@ impl Parser for DefaultParser {
if token.value == "|" {
let right = args.pop().unwrap();
let left = args.pop().unwrap();
root = ASTNode::Pipe {
left: Box::new(left),
right: Box::new(right),
};
root =
ASTNode::Pipe { left: Box::new(left), right: Box::new(right) };
} else {
root = ASTNode::Operator { op: token.value, args };
}
Expand Down Expand Up @@ -218,16 +216,15 @@ impl Interpreter for DefaultInterpreter {
}
}

match name.as_str() {
command => {
let child = std::process::Command::new(command).args(args).spawn();
let command = name.as_str();
{
let child = std::process::Command::new(command).args(args).spawn();

match child {
Ok(mut child) => {
child.wait().unwrap();
}
Err(e) => eprintln!("{}", e),
match child {
Ok(mut child) => {
child.wait().unwrap();
}
Err(e) => eprintln!("{}", e),
}
}
}
Expand Down Expand Up @@ -288,8 +285,8 @@ impl Interpreter for DefaultInterpreter {
};

let output = second_command
.wait_with_output()
.expect("Failed to wait on second command");
.wait_with_output()
.expect("Failed to wait on second command");

println!("{}", String::from_utf8_lossy(&output.stdout));
}
Expand Down
3 changes: 3 additions & 0 deletions i6-shell/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![allow(non_camel_case_types)]
#![allow(unused_variables)]

use std::borrow::Cow::{self, Borrowed, Owned};
use std::collections::HashMap;

Expand Down
6 changes: 2 additions & 4 deletions i6/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::{value_parser, Arg, Command};
use std::error::Error;
use i6_shell::lang::DefaultInterpreter;
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
Expand Down Expand Up @@ -30,9 +30,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.value_parser(value_parser!(u16)),
),
)
.subcommand(
Command::new(sh_id).about("Start an interactive shell")
)
.subcommand(Command::new(sh_id).about("Start an interactive shell"))
.subcommand(
Command::new("timer")
.about("Manages timers")
Expand Down

0 comments on commit 31cc095

Please sign in to comment.