mirror of
https://github.com/lune-org/lune.git
synced 2025-05-04 10:43:57 +01:00
fix: write to lune_history file, not homedir
This commit is contained in:
parent
431f0ff666
commit
a3f88fac4a
1 changed files with 26 additions and 8 deletions
|
@ -1,4 +1,5 @@
|
||||||
use std::{
|
use std::{
|
||||||
|
fmt::Write,
|
||||||
io::ErrorKind,
|
io::ErrorKind,
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
process::{exit, ExitCode},
|
process::{exit, ExitCode},
|
||||||
|
@ -8,10 +9,15 @@ use anyhow::Result;
|
||||||
use clap::Command;
|
use clap::Command;
|
||||||
use directories::UserDirs;
|
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, LuneError};
|
||||||
use mlua::ExternalError;
|
use mlua::ExternalError;
|
||||||
use rustyline::{error::ReadlineError, history::FileHistory, DefaultEditor, Editor};
|
use rustyline::{error::ReadlineError, history::FileHistory, DefaultEditor, Editor};
|
||||||
|
|
||||||
|
enum PromptState {
|
||||||
|
Regular,
|
||||||
|
Continuation,
|
||||||
|
}
|
||||||
|
|
||||||
// Isn't dependency injection plain awesome?!
|
// Isn't dependency injection plain awesome?!
|
||||||
pub async fn show_interface(cmd: Command) -> Result<ExitCode> {
|
pub async fn show_interface(cmd: Command) -> Result<ExitCode> {
|
||||||
let lune_version = cmd.get_version();
|
let lune_version = cmd.get_version();
|
||||||
|
@ -44,7 +50,10 @@ pub async fn show_interface(cmd: Command) -> Result<ExitCode> {
|
||||||
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"),
|
directories::UserDirs::new()
|
||||||
|
.unwrap()
|
||||||
|
.home_dir()
|
||||||
|
.join(".lune_history"),
|
||||||
String::new(),
|
String::new(),
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
@ -54,12 +63,18 @@ pub async fn show_interface(cmd: Command) -> Result<ExitCode> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut prompt_kind: PromptState = PromptState::Regular;
|
||||||
let mut interrupt_counter = 0u32;
|
let mut interrupt_counter = 0u32;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let mut source_code = String::new();
|
let mut source_code = String::new();
|
||||||
|
|
||||||
match repl.readline("> ") {
|
let prompt = match prompt_kind {
|
||||||
|
PromptState::Regular => "> ",
|
||||||
|
PromptState::Continuation => ">> ",
|
||||||
|
};
|
||||||
|
|
||||||
|
match repl.readline(prompt) {
|
||||||
Ok(code) => {
|
Ok(code) => {
|
||||||
source_code = code.clone();
|
source_code = code.clone();
|
||||||
|
|
||||||
|
@ -99,11 +114,14 @@ pub async fn show_interface(cmd: Command) -> Result<ExitCode> {
|
||||||
|
|
||||||
match eval_result {
|
match eval_result {
|
||||||
Ok(_) => (),
|
Ok(_) => (),
|
||||||
|
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
eprintln!(
|
println!("{}", err.to_string());
|
||||||
"{}",
|
|
||||||
pretty_format_luau_error(&err.into_lua_err(), true)
|
write!(&mut source_code, "{}", "\n")?;
|
||||||
)
|
prompt_kind = PromptState::Continuation;
|
||||||
|
|
||||||
|
eprintln!("{}", pretty_format_luau_error(&err.into_lua_err(), true))
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@ -114,7 +132,7 @@ pub async fn show_interface(cmd: Command) -> Result<ExitCode> {
|
||||||
fn save_repl_activity(mut repl: Editor<(), FileHistory>) -> Result<()> {
|
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(directories::UserDirs::new().unwrap().home_dir())?;
|
repl.save_history(&directories::UserDirs::new().unwrap().home_dir().join(".lune_history"))?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue