Skip to content

Commit

Permalink
Merge pull request #53 from afh/vim_mode
Browse files Browse the repository at this point in the history
Set vim_mode if VISUAL contains n?vim?
  • Loading branch information
humblepenguinn authored Mar 1, 2024
2 parents d5bf0ec + ec14909 commit 6b4edfe
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/bin/envio/commands.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use colored::Colorize;
use inquire::{min_length, Confirm, MultiSelect, Password, PasswordDisplayMode, Select, Text};
use regex::Regex;

use std::collections::HashMap;
use std::env;
Expand Down Expand Up @@ -39,21 +40,31 @@ fn get_userkey() -> String {
}
}

/**
* Check to see if the user is using a vi based editor so that we can use the vim mode in the inquire crate
@return Result<bool, String>
*/
fn get_vim_mode() -> Result<bool, String> {
let env = env::var("VISUAL").unwrap_or_else(|_| env::var("EDITOR").unwrap_or_default());

let program = env.split_whitespace().next().ok_or("")?; // Throw an error if the program is empty, we don't really care about the error message

let program_stem = Path::new(program)
.file_stem()
.and_then(|stem| stem.to_str())
.ok_or("")?; // Same here

Ok(Regex::new(r"n?vim?").unwrap().is_match(program_stem)) // unwrap is safe here because we know that the regex will always compile
}

impl Command {
/**
* Run the subcommand that was passed to the program
*/
pub fn run(&self) {
let vim_mode = if let Ok(value) = env::var("VISUAL") {
if let Ok(boolean_value) = value.parse::<bool>() {
boolean_value
} else {
false
}
} else {
false
};

let vim_mode = get_vim_mode().unwrap_or(false);

match self {
Command::Create {
profile_name,
Expand Down

0 comments on commit 6b4edfe

Please sign in to comment.