use .to_owned instead of .to_string for &str's
This commit is contained in:
parent
ad8c6a97c5
commit
4a031d01e9
20
Cargo.lock
generated
20
Cargo.lock
generated
@ -32,9 +32,9 @@ checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cc"
|
name = "cc"
|
||||||
version = "1.2.5"
|
version = "1.2.6"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c31a0499c1dc64f458ad13872de75c0eb7e3fdb0e67964610c914b034fc5956e"
|
checksum = "8d6dbb628b8f8555f86d0323c2eb39e3ec81901f4b83e091db8a6a76d316a333"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"shlex",
|
"shlex",
|
||||||
]
|
]
|
||||||
@ -269,9 +269,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "quote"
|
name = "quote"
|
||||||
version = "1.0.37"
|
version = "1.0.38"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
|
checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
]
|
]
|
||||||
@ -299,18 +299,18 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde"
|
name = "serde"
|
||||||
version = "1.0.216"
|
version = "1.0.217"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "0b9781016e935a97e8beecf0c933758c97a5520d32930e460142b4cd80c6338e"
|
checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"serde_derive",
|
"serde_derive",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "serde_derive"
|
name = "serde_derive"
|
||||||
version = "1.0.216"
|
version = "1.0.217"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "46f859dbbf73865c6627ed570e78961cd3ac92407a2d117204c49232485da55e"
|
checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
@ -331,9 +331,9 @@ checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "2.0.91"
|
version = "2.0.92"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d53cbcb5a243bd33b7858b1d7f4aca2153490815872d86d955d6ea29f743c035"
|
checksum = "70ae51629bf965c5c098cc9e87908a3df5301051a9e087d6f9bef5c9771ed126"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"proc-macro2",
|
"proc-macro2",
|
||||||
"quote",
|
"quote",
|
||||||
|
@ -105,7 +105,7 @@ impl ChangeDirectory for Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn change_directory(&self, args: SplitWhitespace) -> Option<PathBuf> {
|
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() {
|
match vec_args.first() {
|
||||||
Some(arg) => match arg.as_str() {
|
Some(arg) => match arg.as_str() {
|
||||||
"/" => self.set_current_dir(Path::new("/")),
|
"/" => self.set_current_dir(Path::new("/")),
|
||||||
|
21
src/ps.rs
21
src/ps.rs
@ -4,23 +4,18 @@ use color_print::{cformat, cprint};
|
|||||||
pub const DEFAULT_PS: &str = formatcp!("lambdashell-{}", env!("CARGO_PKG_VERSION"));
|
pub const DEFAULT_PS: &str = formatcp!("lambdashell-{}", env!("CARGO_PKG_VERSION"));
|
||||||
|
|
||||||
pub fn working_dir_name() -> String {
|
pub fn working_dir_name() -> String {
|
||||||
match std::env::current_dir() {
|
std::env::current_dir().map_or("?".to_owned(), |path| {
|
||||||
Ok(pathbuf) => match pathbuf.file_name() {
|
path.file_name().map_or("?".to_owned(), |name| {
|
||||||
Some(name) => {
|
let name_os_string = name.to_os_string();
|
||||||
let name_os_string = name.to_os_string();
|
match name_os_string == whoami::username_os() && name_os_string != "root" {
|
||||||
match name_os_string == whoami::username_os() && name_os_string != "root" {
|
true => "~".to_owned(),
|
||||||
true => "~".to_string(),
|
false => name.to_string_lossy().to_string(),
|
||||||
false => name.to_string_lossy().to_string(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
None => "?".to_string(),
|
})
|
||||||
},
|
})
|
||||||
Err(_) => "?".to_string(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn display(ps1: &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());
|
let working_dir_name = cformat!(" <bold>{}</> ", working_dir_name());
|
||||||
cprint!("{}{}λ ", ps1, working_dir_name);
|
cprint!("{}{}λ ", ps1, working_dir_name);
|
||||||
}
|
}
|
||||||
|
@ -111,4 +111,8 @@ pub fn config_file() -> Option<PathBuf> {
|
|||||||
let mut config_file = config_dir()?;
|
let mut config_file = config_dir()?;
|
||||||
config_file.push("config.luau");
|
config_file.push("config.luau");
|
||||||
config_file.is_valid_file_or_create(DEFAULT_CONFIG_CONTENT.as_bytes())
|
config_file.is_valid_file_or_create(DEFAULT_CONFIG_CONTENT.as_bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn none() -> Option<PathBuf> {
|
||||||
|
None
|
||||||
}
|
}
|
45
src/shell.rs
45
src/shell.rs
@ -1,4 +1,4 @@
|
|||||||
use crate::{ps, commands};
|
use crate::{ps, commands, rc};
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
pub struct Config {
|
pub struct Config {
|
||||||
@ -18,10 +18,10 @@ struct Storage {
|
|||||||
|
|
||||||
impl LambdaShell {
|
impl LambdaShell {
|
||||||
pub fn create(config: Config) -> Self {
|
pub fn create(config: Config) -> Self {
|
||||||
Self {
|
Self {
|
||||||
storage: Storage {
|
storage: Storage {
|
||||||
command_exit_status: None,
|
command_exit_status: None,
|
||||||
ps1: ps::DEFAULT_PS.to_string(),
|
ps1: ps::DEFAULT_PS.to_owned(),
|
||||||
},
|
},
|
||||||
terminating: false,
|
terminating: false,
|
||||||
config,
|
config,
|
||||||
@ -30,34 +30,29 @@ impl LambdaShell {
|
|||||||
|
|
||||||
fn input(&mut self) {
|
fn input(&mut self) {
|
||||||
let mut input = String::new();
|
let mut input = String::new();
|
||||||
match io::stdin().read_line(&mut input) {
|
io::stdin().read_line(&mut input).map_or_else(|read_error| println!("{read_error}"), |_size| {
|
||||||
Ok(_size) => {
|
let trimmed_input = input.trim();
|
||||||
let trimmed_input = input.trim();
|
match trimmed_input {
|
||||||
match trimmed_input {
|
//special casey
|
||||||
//special casey
|
"exit" => self.terminating = true,
|
||||||
"exit" => self.terminating = true,
|
_ => self.storage.command_exit_status = commands::Command::new(trimmed_input.to_owned()).exec()
|
||||||
_ => self.storage.command_exit_status = commands::Command::new(trimmed_input.to_string()).exec()
|
};
|
||||||
};
|
})
|
||||||
}
|
|
||||||
Err(read_error) => println!("{read_error}"),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn wait(&mut self) -> Result<(), io::Error> {
|
pub fn wait(&mut self) -> Result<(), io::Error> {
|
||||||
match io::Write::flush(&mut io::stdout()) {
|
io::Write::flush(&mut io::stdout()).map_or_else(|flush_error| Err(flush_error), |()| {
|
||||||
Ok(()) => {
|
self.input();
|
||||||
self.input();
|
Ok(())
|
||||||
Ok(())
|
})
|
||||||
}
|
|
||||||
Err(flush_error) => {
|
|
||||||
println!("{flush_error}");
|
|
||||||
Err(flush_error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start(&mut self) {
|
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 {
|
loop {
|
||||||
match self.terminating {
|
match self.terminating {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user