From ef50763f24a0563643f3b833a0910a63060e0ba5 Mon Sep 17 00:00:00 2001 From: Dheepak Krishnamurthy Date: Thu, 1 Apr 2021 00:10:26 -0600 Subject: [PATCH] Fix history on windows --- Cargo.lock | 7 ------- Cargo.toml | 1 - README.md | 1 - src/app.rs | 13 ++++++------- src/history.rs | 53 ++++++++++++++++++++++++++++++-------------------- src/main.rs | 22 ++++----------------- 6 files changed, 42 insertions(+), 55 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cfa3fae..62a9132 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1396,7 +1396,6 @@ dependencies = [ "unicode-segmentation", "unicode-width", "uuid", - "xdg", ] [[package]] @@ -1619,9 +1618,3 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - -[[package]] -name = "xdg" -version = "2.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d089681aa106a86fade1b0128fb5daf07d5867a509ab036d99988dec80429a57" diff --git a/Cargo.toml b/Cargo.toml index 4ab530d..8dca266 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,7 +41,6 @@ async-std = { version = "1", features = ["attributes", "unstable"] } futures = "0.3" futures-timer = "3.0" dirs = "2.0.2" -xdg = "2" [package.metadata.rpm] package = "taskwarrior-tui" diff --git a/README.md b/README.md index 32b96ab..5f7e68d 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,6 @@ A Terminal User Interface for [Taskwarrior](https://taskwarrior.org/). [![](https://user-images.githubusercontent.com/1813121/97066323-acd41500-1571-11eb-90c2-d74faa21e1ad.png)](https://youtu.be/0ZdkfNrIAcw) -Click on the image above for a video showcasing some of the features of `taskwarrior-tui`. ### Documentation diff --git a/src/app.rs b/src/app.rs index 499bee3..ff4bd69 100644 --- a/src/app.rs +++ b/src/app.rs @@ -17,7 +17,6 @@ use std::path::Path; use std::io::Read; use std::io::Write; -use xdg::BaseDirectories; use std::process::Command; use std::time::SystemTime; @@ -222,17 +221,17 @@ impl TaskwarriorTuiApp { keyconfig: kc, terminal_width: w, terminal_height: h, - filter_history_context: HistoryContext::new(), - command_history_context: HistoryContext::new(), + filter_history_context: HistoryContext::new("filter.history"), + command_history_context: HistoryContext::new("command.history"), }; for c in app.config.filter.chars() { app.filter.insert(c, 1); } app.get_context()?; app.update(true)?; - app.filter_history_context.load("filter.history")?; + app.filter_history_context.load()?; app.filter_history_context.add(app.filter.as_str()); - app.command_history_context.load("command.history")?; + app.command_history_context.load()?; Ok(app) } @@ -892,8 +891,8 @@ impl TaskwarriorTuiApp { } pub fn save_history(&mut self) -> Result<()> { - self.filter_history_context.write("filter.history")?; - self.command_history_context.write("command.history")?; + self.filter_history_context.write()?; + self.command_history_context.write()?; Ok(()) } diff --git a/src/history.rs b/src/history.rs index d8f37be..7df735c 100644 --- a/src/history.rs +++ b/src/history.rs @@ -1,46 +1,57 @@ -use anyhow::Result; +use anyhow::{anyhow, Result}; use rustyline::error::ReadlineError; use rustyline::history::Direction; use rustyline::history::History; use std::fs::File; -use xdg::BaseDirectories; +use std::path::{Path, PathBuf}; + +#[cfg(target_os = "macos")] +use std::env; pub struct HistoryContext { history: History, history_index: usize, + config_path: PathBuf, } impl HistoryContext { - pub fn new() -> Self { + pub fn new(filename: &str) -> Self { let history = History::new(); + + #[cfg(target_os = "macos")] + let config_dir_op = env::var_os("XDG_CONFIG_HOME") + .map(PathBuf::from) + .filter(|p| p.is_absolute()) + .or_else(|| dirs::home_dir().map(|d| d.join(".config"))); + + #[cfg(not(target_os = "macos"))] + let config_dir_op = dirs::config_dir(); + + let config_path = config_dir_op.map(|d| d.join("taskwarrior-tui")).unwrap(); + + std::fs::create_dir_all(&config_path).unwrap(); + + let config_path = config_path.join(filename); + Self { history, history_index: 0, + config_path, } } - pub fn load(&mut self, filename: &str) -> Result<()> { - let d = BaseDirectories::with_prefix("taskwarrior-tui")?; - if let Some(path) = d.find_config_file(filename) { - self.history.load(&path)?; - Ok(()) + pub fn load(&mut self) -> Result<()> { + if self.config_path.exists() { + self.history.load(&self.config_path)?; } else { - let path = d.place_config_file(filename)?; - self.history.save(&path)?; - Ok(()) + self.history.save(&self.config_path)?; } + Ok(()) } - pub fn write(&mut self, filename: &str) -> Result<()> { - let d = BaseDirectories::with_prefix("taskwarrior-tui")?; - if let Some(path) = d.find_config_file(filename) { - self.history.save(&path)?; - Ok(()) - } else { - let path = d.place_config_file(filename)?; - self.history.save(&path)?; - Ok(()) - } + pub fn write(&mut self) -> Result<()> { + self.history.save(&self.config_path)?; + Ok(()) } pub fn history(&self) -> &History { diff --git a/src/main.rs b/src/main.rs index c540f28..8d13b86 100644 --- a/src/main.rs +++ b/src/main.rs @@ -33,7 +33,7 @@ use app::{AppMode, TaskwarriorTuiApp}; const APP_VERSION: &str = env!("CARGO_PKG_VERSION"); const APP_NAME: &str = env!("CARGO_PKG_NAME"); -fn main() -> Result<()> { +fn main() { better_panic::install(); let matches = App::new(APP_NAME) .version(APP_VERSION) @@ -50,23 +50,9 @@ fn main() -> Result<()> { .get_matches(); let config = matches.value_of("config").unwrap_or("~/.taskrc"); - let r = task::block_on(tui_main(config)); - match r { - Ok(_) => std::process::exit(0), - Err(error) => { - if error.to_string().to_lowercase().contains("no such file or directory") { - eprintln!( - "[taskwarrior-tui error]: Unable to find executable `task`: {}. Check that taskwarrior is installed correctly and try again.", error - ); - } else { - eprintln!( - "[taskwarrior-tui error]: {}. Please report as a github issue on https://github.com/kdheepak/taskwarrior-tui", - error - ); - } - std::process::exit(1); - } - } + task::block_on(tui_main(config)).expect( + "[taskwarrior-tui error]. Please report as a github issue on https://github.com/kdheepak/taskwarrior-tui", + ); } async fn tui_main(_config: &str) -> Result<()> {