diff --git a/Cargo.lock b/Cargo.lock index 597ad3e..c7e4220 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1041,7 +1041,6 @@ dependencies = [ "env_logger", "futures-util", "glam", - "home", "hyper", "hyper-tungstenite", "include_dir", diff --git a/Cargo.toml b/Cargo.toml index a9f3a09..0bc1d5c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -74,7 +74,6 @@ lz4_flex = "0.11" pin-project = "1.0" os_str_bytes = "6.4" urlencoding = "2.1" -home = "0.5.5" ### RUNTIME diff --git a/src/cli/mod.rs b/src/cli/mod.rs index d244704..a40e70c 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -1,7 +1,4 @@ -use std::{ - fmt::Write as _, - process::ExitCode, -}; +use std::{fmt::Write as _, process::ExitCode}; use anyhow::{Context, Result}; use clap::{CommandFactory, Parser}; diff --git a/src/cli/repl.rs b/src/cli/repl.rs index a8381c4..d7ff774 100644 --- a/src/cli/repl.rs +++ b/src/cli/repl.rs @@ -1,28 +1,19 @@ use std::{ - env, io::ErrorKind, path::PathBuf, process::{exit, ExitCode}, }; -use anyhow::Error; +use anyhow::Result; use clap::Command; +use directories::UserDirs; use lune::lua::stdio::formatting::pretty_format_luau_error; use lune::Lune; use mlua::ExternalError; -use once_cell::sync::Lazy; use rustyline::{error::ReadlineError, history::FileHistory, DefaultEditor, Editor}; -fn env_var_bool(value: String) -> Option { - match value.to_lowercase().as_str() { - "true" | "1" => Some(true), - "false" | "0" => Some(false), - &_ => None, - } -} - // Isn't dependency injection plain awesome?! -pub async fn show_interface(cmd: Command) -> Result { +pub async fn show_interface(cmd: Command) -> Result { let lune_version = cmd.get_version(); // The version is mandatory and will always exist @@ -33,10 +24,12 @@ pub async fn show_interface(cmd: Command) -> Result { let mut repl = DefaultEditor::new()?; match repl.load_history(&(|| -> PathBuf { - let dir_opt = home::home_dir(); + let dir_opt = UserDirs::new(); - if let Some(dir) = dir_opt { - dir.join(".lune_history") + if let Some(dirs) = dir_opt { + let home_dir = dirs.home_dir(); + + home_dir.join(".lune_history") } else { eprintln!("Failed to find user home directory, abort!"); // Doesn't feel right to exit directly with a exit code of 1 @@ -46,17 +39,14 @@ pub async fn show_interface(cmd: Command) -> Result { })()) { Ok(_) => (), Err(err) => { - match err { - err => { - if let ReadlineError::Io(io_err) = err { - if io_err.kind() == ErrorKind::NotFound { - std::fs::write( - // We know for sure that the home dir already exists - home::home_dir().unwrap().join(".lune_history"), - String::new(), - )?; - } - } + if let ReadlineError::Io(io_err) = err { + // If global history file does not exist, we create it + if io_err.kind() == ErrorKind::NotFound { + std::fs::write( + // We know for sure that the home dir already exists + directories::UserDirs::new().unwrap().home_dir().join(".lune_history"), + String::new(), + )?; } } @@ -66,16 +56,6 @@ pub async fn show_interface(cmd: Command) -> Result { let mut interrupt_counter = 0u32; - let colorize: Lazy = Lazy::new(|| { - let no_color = env::var("NO_COLOR").unwrap_or_else(|_| "false".to_string()); - - if no_color.is_empty() { - true - } else { - !env_var_bool(no_color).unwrap_or_else(|| false) - } - }); - loop { let mut source_code = String::new(); @@ -122,7 +102,7 @@ pub async fn show_interface(cmd: Command) -> Result { Err(err) => { eprintln!( "{}", - pretty_format_luau_error(&err.into_lua_err(), (&*colorize).to_owned()) + pretty_format_luau_error(&err.into_lua_err(), true) ) } }; @@ -131,10 +111,10 @@ pub async fn show_interface(cmd: Command) -> Result { Ok(ExitCode::SUCCESS) } -fn save_repl_activity(mut repl: Editor<(), FileHistory>) -> Result<(), Error> { +fn save_repl_activity(mut repl: Editor<(), FileHistory>) -> Result<()> { // Once again, we know that the specified home directory // and history file already exist - repl.save_history(&home::home_dir().unwrap().join(".lune_history"))?; + repl.save_history(directories::UserDirs::new().unwrap().home_dir())?; Ok(()) }