mirror of
https://github.com/lune-org/lune.git
synced 2025-05-04 10:43:57 +01:00
refactor: implemented requested changes
* makes use of `directories` instead of `home` * use `anyhow::Result` instead of more verbose `Result<_, anyhow::Error>` * removes redundant match statement * removes NO_COLOR logic, already handled by `console` library
This commit is contained in:
parent
7ee5b095bb
commit
431f0ff666
4 changed files with 20 additions and 45 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1041,7 +1041,6 @@ dependencies = [
|
||||||
"env_logger",
|
"env_logger",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"glam",
|
"glam",
|
||||||
"home",
|
|
||||||
"hyper",
|
"hyper",
|
||||||
"hyper-tungstenite",
|
"hyper-tungstenite",
|
||||||
"include_dir",
|
"include_dir",
|
||||||
|
|
|
@ -74,7 +74,6 @@ lz4_flex = "0.11"
|
||||||
pin-project = "1.0"
|
pin-project = "1.0"
|
||||||
os_str_bytes = "6.4"
|
os_str_bytes = "6.4"
|
||||||
urlencoding = "2.1"
|
urlencoding = "2.1"
|
||||||
home = "0.5.5"
|
|
||||||
|
|
||||||
### RUNTIME
|
### RUNTIME
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,4 @@
|
||||||
use std::{
|
use std::{fmt::Write as _, process::ExitCode};
|
||||||
fmt::Write as _,
|
|
||||||
process::ExitCode,
|
|
||||||
};
|
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use clap::{CommandFactory, Parser};
|
use clap::{CommandFactory, Parser};
|
||||||
|
|
|
@ -1,28 +1,19 @@
|
||||||
use std::{
|
use std::{
|
||||||
env,
|
|
||||||
io::ErrorKind,
|
io::ErrorKind,
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
process::{exit, ExitCode},
|
process::{exit, ExitCode},
|
||||||
};
|
};
|
||||||
|
|
||||||
use anyhow::Error;
|
use anyhow::Result;
|
||||||
use clap::Command;
|
use clap::Command;
|
||||||
|
use directories::UserDirs;
|
||||||
use lune::lua::stdio::formatting::pretty_format_luau_error;
|
use lune::lua::stdio::formatting::pretty_format_luau_error;
|
||||||
use lune::Lune;
|
use lune::Lune;
|
||||||
use mlua::ExternalError;
|
use mlua::ExternalError;
|
||||||
use once_cell::sync::Lazy;
|
|
||||||
use rustyline::{error::ReadlineError, history::FileHistory, DefaultEditor, Editor};
|
use rustyline::{error::ReadlineError, history::FileHistory, DefaultEditor, Editor};
|
||||||
|
|
||||||
fn env_var_bool(value: String) -> Option<bool> {
|
|
||||||
match value.to_lowercase().as_str() {
|
|
||||||
"true" | "1" => Some(true),
|
|
||||||
"false" | "0" => Some(false),
|
|
||||||
&_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Isn't dependency injection plain awesome?!
|
// Isn't dependency injection plain awesome?!
|
||||||
pub async fn show_interface(cmd: Command) -> Result<ExitCode, Error> {
|
pub async fn show_interface(cmd: Command) -> Result<ExitCode> {
|
||||||
let lune_version = cmd.get_version();
|
let lune_version = cmd.get_version();
|
||||||
|
|
||||||
// The version is mandatory and will always exist
|
// The version is mandatory and will always exist
|
||||||
|
@ -33,10 +24,12 @@ pub async fn show_interface(cmd: Command) -> Result<ExitCode, Error> {
|
||||||
let mut repl = DefaultEditor::new()?;
|
let mut repl = DefaultEditor::new()?;
|
||||||
|
|
||||||
match repl.load_history(&(|| -> PathBuf {
|
match repl.load_history(&(|| -> PathBuf {
|
||||||
let dir_opt = home::home_dir();
|
let dir_opt = UserDirs::new();
|
||||||
|
|
||||||
if let Some(dir) = dir_opt {
|
if let Some(dirs) = dir_opt {
|
||||||
dir.join(".lune_history")
|
let home_dir = dirs.home_dir();
|
||||||
|
|
||||||
|
home_dir.join(".lune_history")
|
||||||
} else {
|
} else {
|
||||||
eprintln!("Failed to find user home directory, abort!");
|
eprintln!("Failed to find user home directory, abort!");
|
||||||
// Doesn't feel right to exit directly with a exit code of 1
|
// 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<ExitCode, Error> {
|
||||||
})()) {
|
})()) {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
match err {
|
if let ReadlineError::Io(io_err) = err {
|
||||||
err => {
|
// If global history file does not exist, we create it
|
||||||
if let ReadlineError::Io(io_err) = err {
|
if io_err.kind() == ErrorKind::NotFound {
|
||||||
if io_err.kind() == ErrorKind::NotFound {
|
std::fs::write(
|
||||||
std::fs::write(
|
// We know for sure that the home dir already exists
|
||||||
// We know for sure that the home dir already exists
|
directories::UserDirs::new().unwrap().home_dir().join(".lune_history"),
|
||||||
home::home_dir().unwrap().join(".lune_history"),
|
String::new(),
|
||||||
String::new(),
|
)?;
|
||||||
)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,16 +56,6 @@ pub async fn show_interface(cmd: Command) -> Result<ExitCode, Error> {
|
||||||
|
|
||||||
let mut interrupt_counter = 0u32;
|
let mut interrupt_counter = 0u32;
|
||||||
|
|
||||||
let colorize: Lazy<bool> = 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 {
|
loop {
|
||||||
let mut source_code = String::new();
|
let mut source_code = String::new();
|
||||||
|
|
||||||
|
@ -122,7 +102,7 @@ pub async fn show_interface(cmd: Command) -> Result<ExitCode, Error> {
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!(
|
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<ExitCode, Error> {
|
||||||
Ok(ExitCode::SUCCESS)
|
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
|
// Once again, we know that the specified home directory
|
||||||
// and history file already exist
|
// 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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue