From b91af6d847ae11b9c4152bca0d430c4d8d609d88 Mon Sep 17 00:00:00 2001 From: aaron-jack-manning Date: Sun, 21 Aug 2022 17:34:44 +1000 Subject: [PATCH] ability to change editor --- README.md | 3 --- src/config.rs | 12 +++++++++++- src/main.rs | 38 ++++++++++++++++++++++++++++++++------ 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 25730bc..b192736 100644 --- a/README.md +++ b/README.md @@ -3,9 +3,6 @@ A (currently in development) to do app for the command line. ## Planned Features and Changes: -- Options for editing additional config - - `config` - - `editor` subcommand for setting default text editor - Listing tasks in vault (command: `list`) - Options for which field to order by, and how to order (ascending or descending) - Options for which columns to include diff --git a/src/config.rs b/src/config.rs index 04da6aa..2085cc0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,10 +4,20 @@ use crate::colour; use std::path; -#[derive(Debug, Default, serde::Serialize, serde::Deserialize)] +#[derive(Debug, serde::Serialize, serde::Deserialize)] pub struct Config { /// Paths for all vaults, ordered according to recent usage, with current at the front. pub vaults : Vec<(String, path::PathBuf)>, + pub editor : String, +} + +impl Default for Config { + fn default() -> Self { + Self { + vaults : Vec::default(), + editor : String::from("vim"), + } + } } impl Config { diff --git a/src/main.rs b/src/main.rs index 90ef936..c69801e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,3 @@ -//#![allow(dead_code, unused_variables)] - mod git; mod edit; mod vault; @@ -71,11 +69,23 @@ enum Command { // - which columns to include // - filters which exclude values }, + /// For making changes to global configuration. + #[clap(subcommand)] + Config(ConfigCommand), /// Commands for interacting with vaults. #[clap(subcommand)] Vault(VaultCommand), } +#[derive(clap::Subcommand, Debug)] +enum ConfigCommand { + /// For checking or changing default text editor command. + Editor { + /// Command to launch editor. Omit to view current editor. + editor : Option, + } +} + #[derive(clap::Subcommand, Debug)] enum VaultCommand { /// Creates a new vault at the specified location of the given name. @@ -153,7 +163,23 @@ fn program() -> Result<(), error::Error> { println!("Switched to vault {}", colour::vault(&name)); }, } - } + }, + Config(command) => { + use ConfigCommand::*; + match command { + Editor { editor } => { + match editor { + Some(editor) => { + config.editor = editor; + println!("Updated editor command to: {}", config.editor); + }, + None => { + println!("Current editor command: {}", config.editor); + } + } + } + } + }, command => { let vault_folder = &config.current_vault()?.1; let mut state = state::State::load(vault_folder)?; @@ -180,10 +206,10 @@ fn program() -> Result<(), error::Error> { Edit { id_or_name, info } => { let id = state.name_or_id_to_id(&id_or_name)?; if info { - edit::edit_info(id, vault_folder.clone(), "nvim")?; + edit::edit_info(id, vault_folder.clone(), &config.editor)?; } else { - edit::edit_raw(id, vault_folder.clone(), "nvim", &mut state)?; + edit::edit_raw(id, vault_folder.clone(), &config.editor, &mut state)?; } println!("Updated task {}", colour::id(&id.to_string())); }, @@ -207,7 +233,7 @@ fn program() -> Result<(), error::Error> { List {} => { tasks::list(vault_folder)?; } - Vault(_) => unreachable!(), + Vault(_) | Config(_) => unreachable!(), } state.save()?;