Fix history on windows

This commit is contained in:
Dheepak Krishnamurthy 2021-04-01 00:10:26 -06:00
parent b3ea67d48a
commit ef50763f24
6 changed files with 42 additions and 55 deletions

7
Cargo.lock generated
View file

@ -1396,7 +1396,6 @@ dependencies = [
"unicode-segmentation", "unicode-segmentation",
"unicode-width", "unicode-width",
"uuid", "uuid",
"xdg",
] ]
[[package]] [[package]]
@ -1619,9 +1618,3 @@ name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "xdg"
version = "2.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d089681aa106a86fade1b0128fb5daf07d5867a509ab036d99988dec80429a57"

View file

@ -41,7 +41,6 @@ async-std = { version = "1", features = ["attributes", "unstable"] }
futures = "0.3" futures = "0.3"
futures-timer = "3.0" futures-timer = "3.0"
dirs = "2.0.2" dirs = "2.0.2"
xdg = "2"
[package.metadata.rpm] [package.metadata.rpm]
package = "taskwarrior-tui" package = "taskwarrior-tui"

View file

@ -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) [![](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 ### Documentation

View file

@ -17,7 +17,6 @@ use std::path::Path;
use std::io::Read; use std::io::Read;
use std::io::Write; use std::io::Write;
use xdg::BaseDirectories;
use std::process::Command; use std::process::Command;
use std::time::SystemTime; use std::time::SystemTime;
@ -222,17 +221,17 @@ impl TaskwarriorTuiApp {
keyconfig: kc, keyconfig: kc,
terminal_width: w, terminal_width: w,
terminal_height: h, terminal_height: h,
filter_history_context: HistoryContext::new(), filter_history_context: HistoryContext::new("filter.history"),
command_history_context: HistoryContext::new(), command_history_context: HistoryContext::new("command.history"),
}; };
for c in app.config.filter.chars() { for c in app.config.filter.chars() {
app.filter.insert(c, 1); app.filter.insert(c, 1);
} }
app.get_context()?; app.get_context()?;
app.update(true)?; 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.filter_history_context.add(app.filter.as_str());
app.command_history_context.load("command.history")?; app.command_history_context.load()?;
Ok(app) Ok(app)
} }
@ -892,8 +891,8 @@ impl TaskwarriorTuiApp {
} }
pub fn save_history(&mut self) -> Result<()> { pub fn save_history(&mut self) -> Result<()> {
self.filter_history_context.write("filter.history")?; self.filter_history_context.write()?;
self.command_history_context.write("command.history")?; self.command_history_context.write()?;
Ok(()) Ok(())
} }

View file

@ -1,46 +1,57 @@
use anyhow::Result; use anyhow::{anyhow, Result};
use rustyline::error::ReadlineError; use rustyline::error::ReadlineError;
use rustyline::history::Direction; use rustyline::history::Direction;
use rustyline::history::History; use rustyline::history::History;
use std::fs::File; use std::fs::File;
use xdg::BaseDirectories; use std::path::{Path, PathBuf};
#[cfg(target_os = "macos")]
use std::env;
pub struct HistoryContext { pub struct HistoryContext {
history: History, history: History,
history_index: usize, history_index: usize,
config_path: PathBuf,
} }
impl HistoryContext { impl HistoryContext {
pub fn new() -> Self { pub fn new(filename: &str) -> Self {
let history = History::new(); 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 { Self {
history, history,
history_index: 0, history_index: 0,
config_path,
} }
} }
pub fn load(&mut self, filename: &str) -> Result<()> { pub fn load(&mut self) -> Result<()> {
let d = BaseDirectories::with_prefix("taskwarrior-tui")?; if self.config_path.exists() {
if let Some(path) = d.find_config_file(filename) { self.history.load(&self.config_path)?;
self.history.load(&path)?;
Ok(())
} else { } else {
let path = d.place_config_file(filename)?; self.history.save(&self.config_path)?;
self.history.save(&path)?;
Ok(())
} }
Ok(())
} }
pub fn write(&mut self, filename: &str) -> Result<()> { pub fn write(&mut self) -> Result<()> {
let d = BaseDirectories::with_prefix("taskwarrior-tui")?; self.history.save(&self.config_path)?;
if let Some(path) = d.find_config_file(filename) {
self.history.save(&path)?;
Ok(()) Ok(())
} else {
let path = d.place_config_file(filename)?;
self.history.save(&path)?;
Ok(())
}
} }
pub fn history(&self) -> &History { pub fn history(&self) -> &History {

View file

@ -33,7 +33,7 @@ use app::{AppMode, TaskwarriorTuiApp};
const APP_VERSION: &str = env!("CARGO_PKG_VERSION"); const APP_VERSION: &str = env!("CARGO_PKG_VERSION");
const APP_NAME: &str = env!("CARGO_PKG_NAME"); const APP_NAME: &str = env!("CARGO_PKG_NAME");
fn main() -> Result<()> { fn main() {
better_panic::install(); better_panic::install();
let matches = App::new(APP_NAME) let matches = App::new(APP_NAME)
.version(APP_VERSION) .version(APP_VERSION)
@ -50,23 +50,9 @@ fn main() -> Result<()> {
.get_matches(); .get_matches();
let config = matches.value_of("config").unwrap_or("~/.taskrc"); let config = matches.value_of("config").unwrap_or("~/.taskrc");
let r = task::block_on(tui_main(config)); task::block_on(tui_main(config)).expect(
match r { "[taskwarrior-tui error]. Please report as a github issue on https://github.com/kdheepak/taskwarrior-tui",
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);
}
}
} }
async fn tui_main(_config: &str) -> Result<()> { async fn tui_main(_config: &str) -> Result<()> {