feat: Update cli interface

This commit is contained in:
Dheepak Krishnamurthy 2021-12-31 23:58:09 -07:00
parent d8176e01df
commit 8342ce05af
8 changed files with 137 additions and 13 deletions

7
Cargo.lock generated
View file

@ -1109,6 +1109,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "path-clean"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ecba01bf2678719532c5e3059e0b5f0811273d94b397088b82e3bd0a78c78fdd"
[[package]] [[package]]
name = "pin-project-lite" name = "pin-project-lite"
version = "0.2.7" version = "0.2.7"
@ -1487,6 +1493,7 @@ dependencies = [
"lazy_static", "lazy_static",
"log", "log",
"log4rs", "log4rs",
"path-clean",
"rand", "rand",
"regex", "regex",
"rustyline", "rustyline",

View file

@ -32,6 +32,7 @@ itertools = "0.10.3"
lazy_static = "1.4.0" lazy_static = "1.4.0"
log = "0.4.14" log = "0.4.14"
log4rs = "1.0.0" log4rs = "1.0.0"
path-clean = "0.1.0"
rand = "0.8.4" rand = "0.8.4"
regex = "1.5.4" regex = "1.5.4"
rustyline = "9.1.1" rustyline = "9.1.1"

View file

@ -15,8 +15,12 @@ _taskwarrior-tui() {
local context curcontext="$curcontext" state line local context curcontext="$curcontext" state line
_arguments "${_arguments_options[@]}" \ _arguments "${_arguments_options[@]}" \
'-c+[Sets a custom config file]:FILE: ' \ '-d+[Sets the data folder for taskwarrior-tui]:FOLDER: ' \
'--config=[Sets a custom config file]:FILE: ' \ '--data=[Sets the data folder for taskwarrior-tui]:FOLDER: ' \
'-c+[Sets the config folder for taskwarrior-tui]:FOLDER: ' \
'--config=[Sets the config folder for taskwarrior-tui]:FOLDER: ' \
'--taskdata=[Sets the .task folder using the TASKDATA environment variable for taskwarrior]:FOLDER: ' \
'--taskrc=[Sets the .taskrc file using the TASKRC environment variable for taskwarrior]:FILE: ' \
'-r+[Sets default report]:STRING: ' \ '-r+[Sets default report]:STRING: ' \
'--report=[Sets default report]:STRING: ' \ '--report=[Sets default report]:STRING: ' \
'-h[Print help information]' \ '-h[Print help information]' \

View file

@ -20,8 +20,12 @@ Register-ArgumentCompleter -Native -CommandName 'taskwarrior-tui' -ScriptBlock {
$completions = @(switch ($command) { $completions = @(switch ($command) {
'taskwarrior-tui' { 'taskwarrior-tui' {
[CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'Sets a custom config file') [CompletionResult]::new('-d', 'd', [CompletionResultType]::ParameterName, 'Sets the data folder for taskwarrior-tui')
[CompletionResult]::new('--config', 'config', [CompletionResultType]::ParameterName, 'Sets a custom config file') [CompletionResult]::new('--data', 'data', [CompletionResultType]::ParameterName, 'Sets the data folder for taskwarrior-tui')
[CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'Sets the config folder for taskwarrior-tui')
[CompletionResult]::new('--config', 'config', [CompletionResultType]::ParameterName, 'Sets the config folder for taskwarrior-tui')
[CompletionResult]::new('--taskdata', 'taskdata', [CompletionResultType]::ParameterName, 'Sets the .task folder using the TASKDATA environment variable for taskwarrior')
[CompletionResult]::new('--taskrc', 'taskrc', [CompletionResultType]::ParameterName, 'Sets the .taskrc file using the TASKRC environment variable for taskwarrior')
[CompletionResult]::new('-r', 'r', [CompletionResultType]::ParameterName, 'Sets default report') [CompletionResult]::new('-r', 'r', [CompletionResultType]::ParameterName, 'Sets default report')
[CompletionResult]::new('--report', 'report', [CompletionResultType]::ParameterName, 'Sets default report') [CompletionResult]::new('--report', 'report', [CompletionResultType]::ParameterName, 'Sets default report')
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information') [CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help information')

View file

@ -19,12 +19,20 @@ _taskwarrior-tui() {
case "${cmd}" in case "${cmd}" in
taskwarrior__tui) taskwarrior__tui)
opts="-h -V -c -r --help --version --config --report" opts="-h -V -d -c -r --help --version --data --config --taskdata --taskrc --report"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0 return 0
fi fi
case "${prev}" in case "${prev}" in
--data)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
-d)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
--config) --config)
COMPREPLY=($(compgen -f "${cur}")) COMPREPLY=($(compgen -f "${cur}"))
return 0 return 0
@ -33,6 +41,14 @@ _taskwarrior-tui() {
COMPREPLY=($(compgen -f "${cur}")) COMPREPLY=($(compgen -f "${cur}"))
return 0 return 0
;; ;;
--taskdata)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
--taskrc)
COMPREPLY=($(compgen -f "${cur}"))
return 0
;;
--report) --report)
COMPREPLY=($(compgen -f "${cur}")) COMPREPLY=($(compgen -f "${cur}"))
return 0 return 0

View file

@ -1,4 +1,7 @@
complete -c taskwarrior-tui -s c -l config -d 'Sets a custom config file' -r complete -c taskwarrior-tui -s d -l data -d 'Sets the data folder for taskwarrior-tui' -r
complete -c taskwarrior-tui -s c -l config -d 'Sets the config folder for taskwarrior-tui' -r
complete -c taskwarrior-tui -l taskdata -d 'Sets the .task folder using the TASKDATA environment variable for taskwarrior' -r
complete -c taskwarrior-tui -l taskrc -d 'Sets the .taskrc file using the TASKRC environment variable for taskwarrior' -r
complete -c taskwarrior-tui -s r -l report -d 'Sets default report' -r complete -c taskwarrior-tui -s r -l report -d 'Sets default report' -r
complete -c taskwarrior-tui -s h -l help -d 'Print help information' complete -c taskwarrior-tui -s h -l help -d 'Print help information'
complete -c taskwarrior-tui -s V -l version -d 'Print version information' complete -c taskwarrior-tui -s V -l version -d 'Print version information'

View file

@ -8,12 +8,34 @@ pub fn generate_cli_app() -> App<'static> {
.version(APP_VERSION) .version(APP_VERSION)
.author("Dheepak Krishnamurthy <@kdheepak>") .author("Dheepak Krishnamurthy <@kdheepak>")
.about("A taskwarrior terminal user interface") .about("A taskwarrior terminal user interface")
.arg(
Arg::new("data")
.short('d')
.long("data")
.value_name("FOLDER")
.help("Sets the data folder for taskwarrior-tui")
.takes_value(true),
)
.arg( .arg(
Arg::new("config") Arg::new("config")
.short('c') .short('c')
.long("config") .long("config")
.value_name("FOLDER")
.help("Sets the config folder for taskwarrior-tui")
.takes_value(true),
)
.arg(
Arg::new("taskdata")
.long("taskdata")
.value_name("FOLDER")
.help("Sets the .task folder using the TASKDATA environment variable for taskwarrior")
.takes_value(true),
)
.arg(
Arg::new("taskrc")
.long("taskrc")
.value_name("FILE") .value_name("FILE")
.help("Sets a custom config file") .help("Sets the .taskrc file using the TASKRC environment variable for taskwarrior")
.takes_value(true), .takes_value(true),
) )
.arg( .arg(

View file

@ -41,6 +41,8 @@ use crossterm::{
use futures::stream::{FuturesUnordered, StreamExt}; use futures::stream::{FuturesUnordered, StreamExt};
use tui::{backend::CrosstermBackend, Terminal}; use tui::{backend::CrosstermBackend, Terminal};
use path_clean::PathClean;
use app::{Mode, TaskwarriorTui}; use app::{Mode, TaskwarriorTui};
use crate::action::Action; use crate::action::Action;
@ -101,26 +103,91 @@ pub fn initialize_logging() {
log4rs::init_config(config).expect("Failed to initialize logging."); log4rs::init_config(config).expect("Failed to initialize logging.");
} }
pub fn absolute_path(path: impl AsRef<Path>) -> io::Result<PathBuf> {
let path = path.as_ref();
let absolute_path = if path.is_absolute() {
path.to_path_buf()
} else {
env::current_dir()?.join(path)
}
.clean();
Ok(absolute_path)
}
fn main() { fn main() {
better_panic::install(); better_panic::install();
initialize_logging();
let matches = cli::generate_cli_app().get_matches(); let matches = cli::generate_cli_app().get_matches();
debug!("getting matches from clap..."); let config = matches.value_of("config");
let config = matches.value_of("config").unwrap_or("~/.taskrc"); let data = matches.value_of("data");
let taskrc = matches.value_of("taskrc");
let taskdata = matches.value_of("taskdata");
let report = matches.value_of("report").unwrap_or("next"); let report = matches.value_of("report").unwrap_or("next");
if let Some(e) = config {
if env::var("TASKWARRIOR_TUI_CONFIG").is_err() {
// if environment variable is not set, this env::var returns an error
env::set_var(
"TASKWARRIOR_TUI_CONFIG",
absolute_path(PathBuf::from(e)).expect("Unable to get path for config"),
)
} else {
warn!("TASKWARRIOR_TUI_CONFIG environment variable cannot be set.")
}
}
if let Some(e) = data {
if env::var("TASKWARRIOR_TUI_DATA").is_err() {
// if environment variable is not set, this env::var returns an error
env::set_var(
"TASKWARRIOR_TUI_DATA",
absolute_path(PathBuf::from(e)).expect("Unable to get path for data"),
)
} else {
warn!("TASKWARRIOR_TUI_DATA environment variable cannot be set.")
}
}
if let Some(e) = taskrc {
if env::var("TASKRC").is_err() {
// if environment variable is not set, this env::var returns an error
env::set_var(
"TASKRC",
absolute_path(PathBuf::from(e)).expect("Unable to get path for taskrc"),
)
} else {
warn!("TASKRC environment variable cannot be set.")
}
}
if let Some(e) = taskdata {
if env::var("TASKDATA").is_err() {
// if environment variable is not set, this env::var returns an error
env::set_var(
"TASKDATA",
absolute_path(PathBuf::from(e)).expect("Unable to get path for taskdata"),
)
} else {
warn!("TASKDATA environment variable cannot be set.")
}
}
initialize_logging();
debug!("getting matches from clap...");
debug!("report = {:?}", &report); debug!("report = {:?}", &report);
debug!("config = {:?}", &config); debug!("config = {:?}", &config);
let r = task::block_on(tui_main(config, report)); let r = task::block_on(tui_main(report));
if let Err(err) = r { if let Err(err) = r {
eprintln!("\x1b[0;31m[taskwarrior-tui error]\x1b[0m: {}\n\nIf you need additional help, please report as a github issue on https://github.com/kdheepak/taskwarrior-tui", err); eprintln!("\x1b[0;31m[taskwarrior-tui error]\x1b[0m: {}\n\nIf you need additional help, please report as a github issue on https://github.com/kdheepak/taskwarrior-tui", err);
std::process::exit(1); std::process::exit(1);
} }
} }
async fn tui_main(_config: &str, report: &str) -> Result<()> { async fn tui_main(report: &str) -> Result<()> {
panic::set_hook(Box::new(|panic_info| { panic::set_hook(Box::new(|panic_info| {
destruct_terminal(); destruct_terminal();
better_panic::Settings::auto().create_panic_handler()(panic_info); better_panic::Settings::auto().create_panic_handler()(panic_info);