From 6d4fa873537c28e36e88a991ddf4a965a8548c58 Mon Sep 17 00:00:00 2001 From: Compey Date: Mon, 7 Aug 2023 12:37:20 +0530 Subject: [PATCH] feat: save history to file --- src/cli/repl.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/cli/repl.rs b/src/cli/repl.rs index 4c340ce..49ccc10 100644 --- a/src/cli/repl.rs +++ b/src/cli/repl.rs @@ -1,17 +1,18 @@ use std::{ - io::{Error, ErrorKind}, + io::ErrorKind, path::PathBuf, process::{exit, ExitCode}, }; +use anyhow::Error; use clap::Command; use lune::{Lune, LuneError}; -use rustyline::{error::ReadlineError, DefaultEditor}; +use rustyline::{error::ReadlineError, history::FileHistory, DefaultEditor, Editor}; use super::Cli; // 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 @@ -77,10 +78,14 @@ pub async fn show_interface(cmd: Command) -> Result { // Increment the counter interrupt_counter += 1; } else { + save_repl_activity(repl)?; break; } } - Err(ReadlineError::Eof) => break, + Err(ReadlineError::Eof) => { + save_repl_activity(repl)?; + break; + } Err(err) => { eprintln!("REPL ERROR: {}", err.to_string()); @@ -99,3 +104,11 @@ pub async fn show_interface(cmd: Command) -> Result { Ok(ExitCode::SUCCESS) } + +fn save_repl_activity(mut repl: Editor<(), FileHistory>) -> Result<(), Error> { + // Once again, we know that the specified home directory + // and history file already exist + repl.save_history(&home::home_dir().unwrap().join(".lune_history"))?; + + Ok(()) +}