From 43397e0640f5a5b4d02045f43208a3f93943b5ab Mon Sep 17 00:00:00 2001 From: Valerie Date: Tue, 27 Jul 2021 16:17:45 -0400 Subject: [PATCH] added the 'use_editor' option to enable using vim as a default --- README.md | 2 ++ src/main.rs | 31 ++++++++++++++++++++++++++----- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0e8b470..0e46d6d 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,8 @@ For example, for [open] # zero-operand command command = atom . +# use $EDITOR to edit files without specified commands? +use_editor = true [.md] command = typora diff --git a/src/main.rs b/src/main.rs index db9914e..2810c6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use std::{ - env::{args, current_dir}, + env::{args, current_dir, var}, path::Path, process::{Command, exit, Stdio} }; @@ -104,11 +104,32 @@ FLAGS: } let i_exe = config.get(ext, "command"); if i_exe.is_none() { - match ext { - "dir" => println!("open: no command specified for directories."), - _ => println!("open: no command specified for \"{}\" files.", ext) + let use_editor = config.getbool("open", "use_editor"); + if use_editor.is_ok() && use_editor.ok().unwrap().unwrap() { + let i_editor = var("EDITOR"); + if i_editor.is_err() { + println!("open: encountered an error trying to access $EDITOR"); + exit(1); + } + let editor = i_editor.ok().unwrap(); + if editor.is_empty() { + println!("open: $EDITOR is not defined."); + exit(1); + } + let mut command = Command::new(editor); + command.args(vec![i_target]); + command.stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .stdin(Stdio::inherit()); + command.output().ok(); + exit(0); + } else { + match ext { + "dir" => println!("open: no command specified for directories."), + _ => println!("open: no command specified for \"{}\" files.", ext) + } + exit(1); } - exit(1); } let exe = i_exe.unwrap(); let mut parts = exe.split(" ");