From 93d942b8a7339486933f7f9aa3e9cdca95a93647 Mon Sep 17 00:00:00 2001 From: Karcsesz Date: Wed, 14 Feb 2024 21:46:57 +0100 Subject: [PATCH] Default to `vi` if $EDITOR isn't set --- src/editor/open_in_editor.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/editor/open_in_editor.rs b/src/editor/open_in_editor.rs index 5693f93..2dd6ca8 100644 --- a/src/editor/open_in_editor.rs +++ b/src/editor/open_in_editor.rs @@ -4,13 +4,11 @@ use std::io::{Read, Seek, SeekFrom, Write}; use std::process::Command; use tempfile::NamedTempFile; use thiserror::Error; -use tracing::{debug, instrument, trace}; +use tracing::{debug, instrument, trace, warn}; /// Error type returned by `spawn_editor` #[derive(Debug, Error)] pub enum EditorSpawnError { - #[error("$EDITOR environment variable isn't set")] - NoEditorEnv, #[error("failed to parse out absolute path of editor: {0}")] InvalidEditorPath(which::Error), #[error("failed to create temporary file for edit buffer: {0}")] @@ -34,7 +32,10 @@ pub enum EditorSpawnError { pub fn spawn_editor(buffer: impl AsRef + Display) -> Result { use EditorSpawnError::*; trace!("Input buffer: {buffer}"); - let editor = option_env!("EDITOR").ok_or(NoEditorEnv)?; + let editor = option_env!("EDITOR").unwrap_or_else(|| { + warn!("EDITOR environment variable isn't set, defaulting to vi"); + "vi" + }); debug!("$EDITOR is {editor}"); let editor = which::which(editor).map_err(InvalidEditorPath)?; debug!("$EDITOR's full path is {editor:?}");