Compare commits
3 Commits
userdata
...
d40f4bece3
Author | SHA1 | Date | |
---|---|---|---|
d40f4bece3 | |||
ca639db826 | |||
ab349f7894 |
@ -6,27 +6,16 @@ use std::{
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
use crate::MapDisplay;
|
||||
use crate::{history::History, MapDisplay};
|
||||
|
||||
enum ValidStatus {
|
||||
NoRootFolder,
|
||||
TryExists(io::Error)
|
||||
}
|
||||
|
||||
trait PathBufIsValid {
|
||||
fn is_valid(&self) -> Result<PathBuf, ValidStatus>;
|
||||
fn is_valid_or_home(&self) -> Option<PathBuf>;
|
||||
}
|
||||
|
||||
trait ChangeDirectory {
|
||||
fn change_directory(&self, args: SplitWhitespace) -> Option<PathBuf>;
|
||||
fn set_current_dir(&self, new_path: &Path) -> Option<PathBuf>;
|
||||
fn specific_user_dir(&self, user: String) -> Option<PathBuf>;
|
||||
fn cd_args(&self, vec_args: Vec<String>) -> Option<PathBuf>;
|
||||
fn previous_dir(&self) -> Option<PathBuf>;
|
||||
fn home_dir(&self) -> Option<PathBuf>;
|
||||
}
|
||||
|
||||
impl PathBufIsValid for PathBuf {
|
||||
fn is_valid(&self) -> Result<PathBuf, ValidStatus> {
|
||||
match self.try_exists() {
|
||||
@ -50,6 +39,14 @@ impl PathBufIsValid for PathBuf {
|
||||
}
|
||||
}
|
||||
|
||||
trait ChangeDirectory {
|
||||
fn change_directory(&self, args: SplitWhitespace) -> Option<PathBuf>;
|
||||
fn set_current_dir(&self, new_path: &Path) -> Option<PathBuf>;
|
||||
fn specific_user_dir(&self, user: String) -> Option<PathBuf>;
|
||||
fn cd_args(&self, vec_args: Vec<String>) -> Option<PathBuf>;
|
||||
fn previous_dir(&self) -> Option<PathBuf>;
|
||||
fn home_dir(&self) -> Option<PathBuf>;
|
||||
}
|
||||
impl ChangeDirectory for Command {
|
||||
fn set_current_dir(&self, new_path: &Path) -> Option<PathBuf> {
|
||||
std::env::set_current_dir(new_path).map_or_display_none(|()| Some(new_path.to_path_buf()))
|
||||
@ -117,26 +114,39 @@ impl ChangeDirectory for Command {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Command(String);
|
||||
pub struct Command {
|
||||
input: String,
|
||||
history: Option<History>
|
||||
}
|
||||
impl Command {
|
||||
pub fn new(input: String) -> Self {
|
||||
Self(input)
|
||||
Self {
|
||||
history: History::init(),
|
||||
input
|
||||
}
|
||||
}
|
||||
|
||||
pub fn spawn(&self, command_process: io::Result<process::Child>) {
|
||||
command_process.map_or_display_none(|mut child| Some(child.wait()));
|
||||
pub fn write_history(&mut self) {
|
||||
if let Some(history_file) = self.history.as_mut() {
|
||||
history_file.write(&self.input);
|
||||
};
|
||||
}
|
||||
|
||||
pub fn exec(&self) {
|
||||
let mut args = self.0.split_whitespace();
|
||||
pub fn spawn_handle(&mut self, command_process: io::Result<process::Child>) {
|
||||
if let Ok(mut child) = command_process {
|
||||
self.write_history();
|
||||
child.wait().ok();
|
||||
} else {
|
||||
println!("Unknown command: {}", self.input)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn exec(&mut self) {
|
||||
let mut args = self.input.split_whitespace();
|
||||
if let Some(command) = args.next() {
|
||||
match command {
|
||||
"cd" => {
|
||||
self.change_directory(args);
|
||||
},
|
||||
command => {
|
||||
self.spawn(process::Command::new(command).args(args).spawn());
|
||||
}
|
||||
"cd" => { self.change_directory(args); },
|
||||
command => { self.spawn_handle(process::Command::new(command).args(args).spawn()); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
48
src/history.rs
Normal file
48
src/history.rs
Normal file
@ -0,0 +1,48 @@
|
||||
use std::{fs::{File, OpenOptions}, io::{BufRead, BufReader, Write}, path::PathBuf};
|
||||
|
||||
use crate::{rc::{self}, shell_error, valid_pbuf::IsValid, MapDisplay};
|
||||
|
||||
pub struct History {
|
||||
history_file: PathBuf,
|
||||
checked_empty: bool
|
||||
}
|
||||
impl History {
|
||||
pub fn init() -> Option<Self> {
|
||||
rc::config_dir().map(|mut config| {
|
||||
config.push(".history");
|
||||
config.is_valid_file_or_create(b"");
|
||||
Self {
|
||||
history_file: config,
|
||||
checked_empty: false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn is_empty(&mut self) -> bool {
|
||||
match self.checked_empty {
|
||||
true => true,
|
||||
false => self.read().map_or(false, |history_l| {
|
||||
self.checked_empty = true;
|
||||
history_l.is_empty()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write<S: AsRef<str>>(&mut self, content: S) {
|
||||
OpenOptions::new().append(true).open(self.history_file.as_path()).map_or_display(|mut file| {
|
||||
let write_data = match self.is_empty() {
|
||||
true => content.as_ref().to_owned(),
|
||||
false => format!("\n{}", content.as_ref()),
|
||||
};
|
||||
if let Err(write_err) = file.write_all(write_data.as_bytes()) {
|
||||
shell_error(write_err);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
pub fn read(&self) -> Option<Vec<String>> {
|
||||
File::open(&self.history_file).map_or_display_none(|file| {
|
||||
Some(BufReader::new(file).lines().map_while(Result::ok).collect::<Vec<String>>())
|
||||
})
|
||||
}
|
||||
}
|
@ -2,10 +2,13 @@ pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
pub mod session;
|
||||
pub mod commands;
|
||||
pub mod history;
|
||||
pub mod ps;
|
||||
pub mod rc;
|
||||
pub mod vm;
|
||||
|
||||
mod valid_pbuf;
|
||||
|
||||
#[inline]
|
||||
pub fn shell_error<E: core::fmt::Display>(err: E) {
|
||||
color_print::ceprintln!("<bold,r>[!]:</> {err}")
|
||||
|
78
src/rc.rs
78
src/rc.rs
@ -1,7 +1,6 @@
|
||||
use std::{path::PathBuf, fs::{self, File}, io::{self, Write}};
|
||||
use thiserror::Error;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::MapDisplay;
|
||||
use crate::valid_pbuf::IsValid;
|
||||
|
||||
pub const DEFAULT_CONFIG_CONTENT: &str = r#"--!strict
|
||||
|
||||
@ -15,72 +14,6 @@ username = if username == "root" then red(username) else cyan(username)
|
||||
|
||||
SHELL.PROMPT = `{username}@{hostname} λ `"#;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
#[allow(dead_code)]
|
||||
enum IsValidDirErr {
|
||||
#[error("Failed to see if a file exists: {0}")]
|
||||
TryExists(#[from] io::Error),
|
||||
#[error("Not a valid entry")]
|
||||
NotAnEntry,
|
||||
#[error("Directory missing")]
|
||||
Missing
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
enum CreateErr {
|
||||
TryExists(io::Error),
|
||||
Passable
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
trait IsValid {
|
||||
fn is_valid(&self, is_dir_or_file: bool) -> Result<PathBuf, IsValidDirErr>;
|
||||
fn is_valid_option(&self, is_dir_or_file: bool) -> Option<PathBuf>;
|
||||
fn is_valid_file_or_create(&self, default_file_bytes: &[u8]) -> Option<PathBuf>;
|
||||
fn is_valid_dir_or_create(&self) -> Option<PathBuf>;
|
||||
fn is_valid_or<F>(&self, is_content: bool, f: F) -> Option<PathBuf>
|
||||
where
|
||||
F: FnOnce() -> Option<PathBuf>;
|
||||
}
|
||||
impl IsValid for PathBuf {
|
||||
fn is_valid(&self, is_content: bool) -> Result<PathBuf, IsValidDirErr> {
|
||||
match self.try_exists() {
|
||||
Ok(true) => if is_content { Ok(self.to_path_buf()) } else { Err(IsValidDirErr::NotAnEntry) },
|
||||
Ok(false) => Err(IsValidDirErr::Missing),
|
||||
Err(try_e) => Err(IsValidDirErr::TryExists(try_e))
|
||||
}
|
||||
}
|
||||
|
||||
fn is_valid_or<F>(&self, is_content: bool, f: F) -> Option<PathBuf>
|
||||
where
|
||||
F: FnOnce() -> Option<PathBuf>
|
||||
{
|
||||
self.is_valid(is_content).map_err(|e| match e {
|
||||
IsValidDirErr::TryExists(try_e) => CreateErr::TryExists(try_e),
|
||||
IsValidDirErr::NotAnEntry | IsValidDirErr::Missing => CreateErr::Passable
|
||||
}).map_or_else(|e| match e {
|
||||
CreateErr::TryExists(_) => None,
|
||||
CreateErr::Passable => f()
|
||||
}, Some)
|
||||
}
|
||||
|
||||
fn is_valid_dir_or_create(&self) -> Option<PathBuf> {
|
||||
self.is_valid_or(self.is_dir(), || fs::create_dir(self).map_or_display_none(|()| Some(self.to_path_buf())))
|
||||
}
|
||||
|
||||
fn is_valid_file_or_create(&self, default_file_bytes: &[u8]) -> Option<PathBuf> {
|
||||
self.is_valid_or(self.is_file(), || {
|
||||
File::create(self).map_or_display_none(|mut file| {
|
||||
file.write_all(default_file_bytes).map_or_display_none(|()| Some(self.to_path_buf()))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
fn is_valid_option(&self, is_dir_or_file: bool) -> Option<PathBuf> {
|
||||
self.is_valid(is_dir_or_file).ok()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn config_dir() -> Option<PathBuf> {
|
||||
let mut config = home::home_dir()?;
|
||||
config.push(".config");
|
||||
@ -94,10 +27,3 @@ pub fn config_file() -> Option<PathBuf> {
|
||||
config_file.push("init.luau");
|
||||
config_file.is_valid_file_or_create(DEFAULT_CONFIG_CONTENT.as_bytes())
|
||||
}
|
||||
|
||||
// TODO: history.rs
|
||||
pub fn history_file() -> Option<PathBuf> {
|
||||
let mut config_file = config_dir()?;
|
||||
config_file.push(".history");
|
||||
config_file.is_valid_file_or_create(b"")
|
||||
}
|
@ -2,7 +2,7 @@ use std::{cell::RefCell, fs, io::{self}, rc::Rc};
|
||||
use core::fmt;
|
||||
|
||||
use crate::{
|
||||
commands, ps::{self, Ps}, rc, shell_error, vm::{self, LuauVm}, MapDisplay
|
||||
commands, ps::{self, Ps}, rc::{self}, shell_error, vm::{self, LuauVm}, MapDisplay
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@ -48,7 +48,6 @@ impl LambdaShell {
|
||||
fs::read_to_string(conf_file).map_or_display(|luau_conf| self.vm_exec(luau_conf));
|
||||
}
|
||||
}
|
||||
|
||||
self.ps.borrow().display();
|
||||
loop {
|
||||
if self.terminate { break } else {
|
||||
|
75
src/valid_pbuf.rs
Normal file
75
src/valid_pbuf.rs
Normal file
@ -0,0 +1,75 @@
|
||||
use std::{fs::{self, File}, io::{self, Write}, path::PathBuf};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::MapDisplay;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
#[allow(dead_code)]
|
||||
pub enum IsValidDirErr {
|
||||
#[error("Failed to see if a file exists: {0}")]
|
||||
TryExists(#[from] io::Error),
|
||||
#[error("Not a valid entry")]
|
||||
NotAnEntry,
|
||||
#[error("Directory missing")]
|
||||
Missing
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
enum CreateErr {
|
||||
TryExists(io::Error),
|
||||
Passable
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub trait IsValid {
|
||||
fn is_valid(&self, is_dir_or_file: bool) -> Result<PathBuf, IsValidDirErr>;
|
||||
fn is_valid_option(&self, is_dir_or_file: bool) -> Option<PathBuf>;
|
||||
fn is_valid_file_or_create(&self, default_file_bytes: &[u8]) -> Option<PathBuf>;
|
||||
fn is_valid_dir_or_create(&self) -> Option<PathBuf>;
|
||||
fn is_valid_or<F>(&self, is_content: bool, f: F) -> Option<PathBuf>
|
||||
where
|
||||
F: FnOnce() -> Option<PathBuf>;
|
||||
}
|
||||
impl IsValid for PathBuf {
|
||||
#[inline]
|
||||
fn is_valid(&self, is_content: bool) -> Result<PathBuf, IsValidDirErr> {
|
||||
match self.try_exists() {
|
||||
Ok(true) => if is_content { Ok(self.to_path_buf()) } else { Err(IsValidDirErr::NotAnEntry) },
|
||||
Ok(false) => Err(IsValidDirErr::Missing),
|
||||
Err(try_e) => Err(IsValidDirErr::TryExists(try_e))
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_valid_or<F>(&self, is_content: bool, f: F) -> Option<PathBuf>
|
||||
where
|
||||
F: FnOnce() -> Option<PathBuf>
|
||||
{
|
||||
self.is_valid(is_content).map_err(|e| match e {
|
||||
IsValidDirErr::TryExists(try_e) => CreateErr::TryExists(try_e),
|
||||
IsValidDirErr::NotAnEntry | IsValidDirErr::Missing => CreateErr::Passable
|
||||
}).map_or_else(|e| match e {
|
||||
CreateErr::TryExists(_) => None,
|
||||
CreateErr::Passable => f()
|
||||
}, Some)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_valid_dir_or_create(&self) -> Option<PathBuf> {
|
||||
self.is_valid_or(self.is_dir(), || fs::create_dir(self).map_or_display_none(|()| Some(self.to_path_buf())))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_valid_file_or_create(&self, default_file_bytes: &[u8]) -> Option<PathBuf> {
|
||||
self.is_valid_or(self.is_file(), || {
|
||||
File::create(self).map_or_display_none(|mut file| {
|
||||
file.write_all(default_file_bytes).map_or_display_none(|()| Some(self.to_path_buf()))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn is_valid_option(&self, is_dir_or_file: bool) -> Option<PathBuf> {
|
||||
self.is_valid(is_dir_or_file).ok()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user