use .to_owned instead of .to_string for &str's

This commit is contained in:
2024-12-27 22:35:42 -05:00
parent ad8c6a97c5
commit 4a031d01e9
5 changed files with 43 additions and 49 deletions

View File

@ -105,7 +105,7 @@ impl ChangeDirectory for Command {
}
fn change_directory(&self, args: SplitWhitespace) -> Option<PathBuf> {
let vec_args: Vec<String> = args.map(|arg| arg.to_string()).collect();
let vec_args: Vec<String> = args.map(|arg| arg.to_owned()).collect();
match vec_args.first() {
Some(arg) => match arg.as_str() {
"/" => self.set_current_dir(Path::new("/")),

View File

@ -4,23 +4,18 @@ use color_print::{cformat, cprint};
pub const DEFAULT_PS: &str = formatcp!("lambdashell-{}", env!("CARGO_PKG_VERSION"));
pub fn working_dir_name() -> String {
match std::env::current_dir() {
Ok(pathbuf) => match pathbuf.file_name() {
Some(name) => {
let name_os_string = name.to_os_string();
match name_os_string == whoami::username_os() && name_os_string != "root" {
true => "~".to_string(),
false => name.to_string_lossy().to_string(),
}
std::env::current_dir().map_or("?".to_owned(), |path| {
path.file_name().map_or("?".to_owned(), |name| {
let name_os_string = name.to_os_string();
match name_os_string == whoami::username_os() && name_os_string != "root" {
true => "~".to_owned(),
false => name.to_string_lossy().to_string(),
}
None => "?".to_string(),
},
Err(_) => "?".to_string(),
}
})
})
}
pub fn display(ps1: &String) {
// let exit_status = shell_storage.command_exit_status.map(|s| format!(" [{s}] ")).unwrap_or(" ".to_string());
let working_dir_name = cformat!(" <bold>{}</> ", working_dir_name());
cprint!("{}{}λ ", ps1, working_dir_name);
}

View File

@ -111,4 +111,8 @@ pub fn config_file() -> Option<PathBuf> {
let mut config_file = config_dir()?;
config_file.push("config.luau");
config_file.is_valid_file_or_create(DEFAULT_CONFIG_CONTENT.as_bytes())
}
pub fn none() -> Option<PathBuf> {
None
}

View File

@ -1,4 +1,4 @@
use crate::{ps, commands};
use crate::{ps, commands, rc};
use std::io;
pub struct Config {
@ -18,10 +18,10 @@ struct Storage {
impl LambdaShell {
pub fn create(config: Config) -> Self {
Self {
Self {
storage: Storage {
command_exit_status: None,
ps1: ps::DEFAULT_PS.to_string(),
ps1: ps::DEFAULT_PS.to_owned(),
},
terminating: false,
config,
@ -30,34 +30,29 @@ impl LambdaShell {
fn input(&mut self) {
let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(_size) => {
let trimmed_input = input.trim();
match trimmed_input {
//special casey
"exit" => self.terminating = true,
_ => self.storage.command_exit_status = commands::Command::new(trimmed_input.to_string()).exec()
};
}
Err(read_error) => println!("{read_error}"),
};
io::stdin().read_line(&mut input).map_or_else(|read_error| println!("{read_error}"), |_size| {
let trimmed_input = input.trim();
match trimmed_input {
//special casey
"exit" => self.terminating = true,
_ => self.storage.command_exit_status = commands::Command::new(trimmed_input.to_owned()).exec()
};
})
}
pub fn wait(&mut self) -> Result<(), io::Error> {
match io::Write::flush(&mut io::stdout()) {
Ok(()) => {
self.input();
Ok(())
}
Err(flush_error) => {
println!("{flush_error}");
Err(flush_error)
}
}
io::Write::flush(&mut io::stdout()).map_or_else(|flush_error| Err(flush_error), |()| {
self.input();
Ok(())
})
}
pub fn start(&mut self) {
ps::display(&self.storage.ps1);
let rc_file = match self.config.norc {
true => rc::config_file(),
false => rc::none(),
};
ps::display(&self.storage.ps1);
loop {
match self.terminating {