2024-12-06 15:54:31 -05:00
|
|
|
use std::{ffi::OsStr, process::Command};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use crate::core::repl::repl::evaluate_command;
|
2024-12-01 16:02:06 -05:00
|
|
|
|
2024-12-05 11:00:08 -05:00
|
|
|
use super::COMMAND_LIST;
|
|
|
|
|
2024-12-06 15:54:31 -05:00
|
|
|
pub(crate) fn say_hello() -> anyhow::Result<()> {
|
2024-12-01 23:52:12 -06:00
|
|
|
println!("Hello, World!");
|
2024-12-06 15:54:31 -05:00
|
|
|
Ok(())
|
2024-12-01 16:02:06 -05:00
|
|
|
}
|
|
|
|
|
2024-12-06 15:54:31 -05:00
|
|
|
pub(crate) fn echo(args: Vec<String>) -> anyhow::Result<()> {
|
|
|
|
println!("{}", args.join(" "));
|
|
|
|
Ok(())
|
2024-12-01 16:02:06 -05:00
|
|
|
}
|
|
|
|
|
2024-12-06 15:54:31 -05:00
|
|
|
pub(crate) fn exit() -> anyhow::Result<()> {
|
2024-12-02 11:51:39 -06:00
|
|
|
println!("Exiting...");
|
2024-12-01 16:02:06 -05:00
|
|
|
std::process::exit(0)
|
|
|
|
}
|
2024-12-01 23:52:12 -06:00
|
|
|
|
2024-12-06 15:54:31 -05:00
|
|
|
pub(crate) fn clear() -> anyhow::Result<()> {
|
2024-12-02 11:51:39 -06:00
|
|
|
println!("Clearing screen..., running command");
|
2024-12-01 16:02:06 -05:00
|
|
|
let _result = if cfg!(target_os = "windows") {
|
|
|
|
Command::new("cmd").args(["/c", "cls"]).spawn()
|
|
|
|
} else {
|
2024-12-02 11:51:39 -06:00
|
|
|
Command::new("clear").spawn()
|
2024-12-01 16:02:06 -05:00
|
|
|
};
|
2024-12-06 15:54:31 -05:00
|
|
|
Ok(())
|
2024-12-01 16:02:06 -05:00
|
|
|
}
|
2024-12-01 23:52:12 -06:00
|
|
|
|
2024-12-06 15:54:31 -05:00
|
|
|
pub(crate) fn help() -> anyhow::Result<()> {
|
2024-12-01 16:02:06 -05:00
|
|
|
println!("Commands:");
|
|
|
|
for cmd in COMMAND_LIST.commands.read().iter() {
|
|
|
|
println!("{:#}", cmd);
|
|
|
|
}
|
2024-12-06 15:54:31 -05:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
pub(crate) fn exec(args: Vec<String>) -> anyhow::Result<()> {
|
|
|
|
let file_path_str = &args[0];
|
|
|
|
let file_path = std::path::Path::new(file_path_str);
|
|
|
|
println!("File path: {:#?}", file_path);
|
|
|
|
|
|
|
|
if !file_path.is_file() {
|
|
|
|
eprintln!("Error: File does not exist or is not a valid file: {}", file_path.display());
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
if file_path.extension() != Some(OsStr::new("zensh")) {
|
|
|
|
eprintln!("Error: File is not a zenshell script: {:#?}", file_path.extension());
|
|
|
|
//TODO: dont panic on this error
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
println!("Executing file: {:#?}", file_path);
|
|
|
|
let file_content = std::fs::read_to_string(file_path)?;
|
|
|
|
if file_content.is_empty() {
|
|
|
|
eprintln!("Error: file has no content. Is this a valid zenshell script?");
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
println!("File contents:\n{file_content}");
|
|
|
|
evaluate_command(file_content.trim())?;
|
|
|
|
Ok(())
|
2024-12-01 16:02:06 -05:00
|
|
|
}
|