Default to vi if $EDITOR isn't set

This commit is contained in:
Karcsesz 2024-02-14 21:46:57 +01:00
parent e74134d9ad
commit 93d942b8a7

View file

@ -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<str> + Display) -> Result<String, EditorSpawnError> {
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:?}");