소스 검색

Properly store history in .local/share

David Peter 3 년 전
부모
커밋
c0dda22e0d
2개의 변경된 파일15개의 추가작업 그리고 5개의 파일을 삭제
  1. 0 1
      .gitignore
  2. 15 4
      insect-cli/src/main.rs

+ 0 - 1
.gitignore

@@ -1,2 +1 @@
 /target
-/.history

+ 15 - 4
insect-cli/src/main.rs

@@ -12,7 +12,6 @@ use clap::Parser;
 use rustyline::error::ReadlineError;
 use rustyline::Editor;
 
-const HISTORY_FILE: &str = ".history";
 const PROMPT: &str = ">>> ";
 
 #[derive(Parser, Debug)]
@@ -91,8 +90,10 @@ impl Insect {
             println!(r"|_|_| |_|___/\___|\___|\__|");
             println!();
 
+            let history_path = self.get_history_path()?;
+
             let mut rl = Editor::<()>::new()?;
-            rl.load_history(HISTORY_FILE).ok();
+            rl.load_history(&history_path).ok();
 
             loop {
                 let readline = rl.readline(PROMPT);
@@ -115,8 +116,10 @@ impl Insect {
                 }
             }
 
-            rl.save_history(HISTORY_FILE)
-                .context("Error while saving history to file")
+            rl.save_history(&history_path).context(format!(
+                "Error while saving history to '{}'",
+                history_path.to_string_lossy()
+            ))
         }
     }
 
@@ -186,6 +189,14 @@ impl Insect {
         let config_dir = dirs_next::config_dir().unwrap_or_else(|| PathBuf::from("."));
         config_dir.join("insect").join("prelude.ins") // TODO: allow for preludes in system paths, user paths, …
     }
+
+    fn get_history_path(&self) -> Result<PathBuf> {
+        let data_dir = dirs_next::data_dir()
+            .unwrap_or_else(|| PathBuf::from("."))
+            .join("insect");
+        fs::create_dir(&data_dir).context("Error while creating directory for history")?;
+        Ok(data_dir.join("history"))
+    }
 }
 
 fn main() {