mj 0.4.3

My Journal - personal tool to capture ideas, work with journals, notes and tasks in your favourite text $EDITOR.
Documentation
use clap::{ArgMatches, Shell};
use crate::cli::app;
use crate::core::*;

#[derive(Debug)]
pub enum CompletionArgs {
  Bash,
  Fish,
  Zsh,
  PowerShell,
  Elvish,
}

pub fn run(_vault: &Vault, args: &CompletionArgs) -> Result<()> {
  let (shell, is_zsh) =
    match args {
      CompletionArgs::Bash => (Shell::Bash, false),
      CompletionArgs::Fish => (Shell::Fish, false),
      CompletionArgs::Zsh => (Shell::Zsh, true),
      CompletionArgs::PowerShell => (Shell::PowerShell, false),
      CompletionArgs::Elvish => (Shell::Elvish, false),
    };
  let mut x = app();
  if is_zsh {
    println!(r#"#!/usr/bin/env bash
if [ ! -d "$HOME/.local/share/zsh/site-functions" ]; then
  mkdir -p "$HOME/.local/share/zsh/site-functions"
fi
fpath=($HOME/.local/share/zsh/site-functions $fpath)
cat <<'EOF' > $HOME/.local/share/zsh/site-functions/_mj"#);
  }
  x.gen_completions_to("mj", shell, &mut std::io::stdout());
  if is_zsh {
    println!(r#"
EOF
autoload -U compinit && compinit"#);
  }
  Ok(())
}

pub fn match_args(matches: &ArgMatches) -> Option<CompletionArgs> {
  if let Some(matches) = matches.subcommand_matches("completion") {
    return
      match matches.value_of("SHELL") {
        Some("bash") => Some(CompletionArgs::Bash),
        Some("fish") => Some(CompletionArgs::Fish),
        Some("zsh") => Some(CompletionArgs::Zsh),
        Some("powershell") => Some(CompletionArgs::PowerShell),
        Some("elvish") => Some(CompletionArgs::Elvish),
        _ => None,
      };
  }
  None
}