From 7835955bd9822a2fe49f70d05827fc3839f7d59e Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Mon, 24 Jun 2019 22:02:31 +0530 Subject: use better directory crate, use data_dir for history --- Cargo.toml | 2 +- src/main.rs | 18 +++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1aafe11..25b849f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,4 @@ rustyline = "4.1.0" clap = "2.32.0" radix_fmt = "1.0.0" lazy_static = "1.3.0" -dirs = "2.0.1" +directories = "2.0.1" diff --git a/src/main.rs b/src/main.rs index 10ae251..50f4375 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use std::f64; use std::borrow::Cow::{self,Owned}; use std::path::PathBuf; +use std::fs::create_dir_all; // modules mod lex; @@ -30,6 +31,8 @@ use rustyline::highlight::Highlighter; use clap::{Arg, App}; use lazy_static::lazy_static; +use directories::{ ProjectDirs, UserDirs }; + struct RLHelper { completer: FilenameCompleter, highlighter: LineHighlighter, @@ -131,14 +134,15 @@ fn main() { hinter: AnswerHinter {} }; rl.set_helper(Some(h)); - let mut history_path = PathBuf::new(); - match dirs::home_dir() { - Some(p) => { - history_path = p; - history_path.push("history.txt"); - }, - None => history_path.set_file_name("history.txt"), + + let eva_dirs = ProjectDirs::from("com", "NerdyPepper", "eva").unwrap(); + let eva_data_dir = eva_dirs.data_dir(); + let mut history_path = PathBuf::from(eva_data_dir); + match create_dir_all(eva_data_dir) { + Ok(_) => history_path.push("history.txt"), + Err(_) => history_path = PathBuf::from(UserDirs::new().unwrap().home_dir()), }; + if rl.load_history(history_path.as_path()).is_err() { println!("No previous history.") }; -- cgit v1.2.3