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 std::process::Command;
use tempfile::NamedTempFile; use tempfile::NamedTempFile;
use thiserror::Error; use thiserror::Error;
use tracing::{debug, instrument, trace}; use tracing::{debug, instrument, trace, warn};
/// Error type returned by `spawn_editor` /// Error type returned by `spawn_editor`
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum EditorSpawnError { pub enum EditorSpawnError {
#[error("$EDITOR environment variable isn't set")]
NoEditorEnv,
#[error("failed to parse out absolute path of editor: {0}")] #[error("failed to parse out absolute path of editor: {0}")]
InvalidEditorPath(which::Error), InvalidEditorPath(which::Error),
#[error("failed to create temporary file for edit buffer: {0}")] #[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> { pub fn spawn_editor(buffer: impl AsRef<str> + Display) -> Result<String, EditorSpawnError> {
use EditorSpawnError::*; use EditorSpawnError::*;
trace!("Input buffer: {buffer}"); 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}"); debug!("$EDITOR is {editor}");
let editor = which::which(editor).map_err(InvalidEditorPath)?; let editor = which::which(editor).map_err(InvalidEditorPath)?;
debug!("$EDITOR's full path is {editor:?}"); debug!("$EDITOR's full path is {editor:?}");