mirror of
https://github.com/lune-org/lune.git
synced 2025-05-04 10:43:57 +01:00
refactor: simplify history file path err handling
This commit is contained in:
parent
90d51aeaf6
commit
aa50d3e5af
2 changed files with 24 additions and 57 deletions
|
@ -1,22 +1,19 @@
|
||||||
use std::{
|
use std::{
|
||||||
fmt::Write,
|
fmt::Write,
|
||||||
io::ErrorKind,
|
path::{Path, PathBuf},
|
||||||
path::PathBuf,
|
|
||||||
process::{exit, ExitCode},
|
process::{exit, ExitCode},
|
||||||
};
|
};
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::{Error, Result};
|
||||||
use clap::Command;
|
use clap::Command;
|
||||||
use directories::UserDirs;
|
use directories::UserDirs;
|
||||||
use lune::lua::stdio::formatting::pretty_format_luau_error;
|
|
||||||
use lune::Lune;
|
use lune::Lune;
|
||||||
use mlua::ExternalError;
|
use rustyline::{error::ReadlineError, DefaultEditor};
|
||||||
use rustyline::{error::ReadlineError, history::FileHistory, DefaultEditor, Editor};
|
|
||||||
|
|
||||||
#[derive(PartialEq)]
|
#[derive(PartialEq)]
|
||||||
enum PromptState {
|
enum PromptState {
|
||||||
Regular,
|
Regular,
|
||||||
Continuation
|
Continuation,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Isn't dependency injection plain awesome?!
|
// Isn't dependency injection plain awesome?!
|
||||||
|
@ -29,40 +26,23 @@ pub async fn show_interface(cmd: Command) -> Result<ExitCode> {
|
||||||
let lune_instance = Lune::new();
|
let lune_instance = Lune::new();
|
||||||
|
|
||||||
let mut repl = DefaultEditor::new()?;
|
let mut repl = DefaultEditor::new()?;
|
||||||
|
let history_file_path: &PathBuf = &UserDirs::new()
|
||||||
|
.ok_or(Error::msg("cannot find user home directory"))?
|
||||||
|
.home_dir()
|
||||||
|
.join(".lune_history");
|
||||||
|
|
||||||
match repl.load_history(&(|| -> PathBuf {
|
if !history_file_path.exists() {
|
||||||
let dir_opt = UserDirs::new();
|
std::fs::write(
|
||||||
|
// We know for sure that the home dir already exists
|
||||||
|
directories::UserDirs::new()
|
||||||
|
.unwrap()
|
||||||
|
.home_dir()
|
||||||
|
.join(".lune_history"),
|
||||||
|
String::new(),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(dirs) = dir_opt {
|
repl.load_history(history_file_path)?;
|
||||||
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
|
|
||||||
// Lmk if there is a better way of doing this
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
})()) {
|
|
||||||
Ok(_) => (),
|
|
||||||
Err(err) => {
|
|
||||||
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(),
|
|
||||||
)?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
eprintln!("WARN: Failed to load REPL history")
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut interrupt_counter = 0u32;
|
let mut interrupt_counter = 0u32;
|
||||||
let mut prompt_state: PromptState = PromptState::Regular;
|
let mut prompt_state: PromptState = PromptState::Regular;
|
||||||
|
@ -71,7 +51,7 @@ pub async fn show_interface(cmd: Command) -> Result<ExitCode> {
|
||||||
loop {
|
loop {
|
||||||
let prompt = match prompt_state {
|
let prompt = match prompt_state {
|
||||||
PromptState::Regular => "> ",
|
PromptState::Regular => "> ",
|
||||||
PromptState::Continuation => ">> "
|
PromptState::Continuation => ">> ",
|
||||||
};
|
};
|
||||||
|
|
||||||
match repl.readline(prompt) {
|
match repl.readline(prompt) {
|
||||||
|
@ -98,12 +78,12 @@ pub async fn show_interface(cmd: Command) -> Result<ExitCode> {
|
||||||
// Increment the counter
|
// Increment the counter
|
||||||
interrupt_counter += 1;
|
interrupt_counter += 1;
|
||||||
} else {
|
} else {
|
||||||
save_repl_activity(repl)?;
|
repl.save_history(history_file_path)?;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(ReadlineError::Eof) => {
|
Err(ReadlineError::Eof) => {
|
||||||
save_repl_activity(repl)?;
|
repl.save_history(history_file_path)?;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
@ -124,7 +104,7 @@ pub async fn show_interface(cmd: Command) -> Result<ExitCode> {
|
||||||
prompt_state = PromptState::Continuation;
|
prompt_state = PromptState::Continuation;
|
||||||
source_code.push_str("\n")
|
source_code.push_str("\n")
|
||||||
} else {
|
} else {
|
||||||
eprintln!("{}", pretty_format_luau_error(&err.into_lua_err(), true))
|
eprintln!("{}", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -132,16 +112,3 @@ pub async fn show_interface(cmd: Command) -> Result<ExitCode> {
|
||||||
|
|
||||||
Ok(ExitCode::SUCCESS)
|
Ok(ExitCode::SUCCESS)
|
||||||
}
|
}
|
||||||
|
|
||||||
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(
|
|
||||||
&directories::UserDirs::new()
|
|
||||||
.unwrap()
|
|
||||||
.home_dir()
|
|
||||||
.join(".lune_history"),
|
|
||||||
)?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
|
@ -6,4 +6,4 @@ pub mod roblox;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
pub use crate::lune::{lua, Lune, LuneError};
|
pub use crate::lune::{Lune, LuneError};
|
||||||
|
|
Loading…
Add table
Reference in a new issue