Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ use {
kitty::KittyProtocolGuard,
semantic_prompt::{Osc133ClickEventsMarkers, SemanticPromptMarkers},
},
utils::{
environment::{term_supports_ansi, var_os},
text_manipulation,
},
utils::text_manipulation,
AbbrExpandContext, AutoPairAction, AutoPairContext, AutoPairs, Direction, EditCommand,
ExampleHighlighter, Highlighter, LineBuffer, Menu, MenuEvent, MouseButton, Prompt,
PromptHistorySearch, ReedlineMenu, Signal, UndoBehavior, ValidationResult, Validator,
Expand Down Expand Up @@ -555,9 +552,10 @@ impl Reedline {
///
/// ANSI coloring requires both the Reedline configuration to enable it and
/// terminal support; `TERM=dumb` takes precedence over `with_ansi_colors(true)`.
/// The painter holds the terminal policy, so the colors and the escapes it
/// skips cannot disagree.
fn effective_ansi_coloring(&self) -> bool {
let term = var_os("TERM");
self.use_ansi_coloring && term_supports_ansi(term.as_deref())
self.use_ansi_coloring && !self.painter.term_is_dumb()
}

/// A builder which enables or disables the use of ansi coloring in the prompt
Expand Down
6 changes: 6 additions & 0 deletions src/painting/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,12 @@ impl Painter {
}
}

/// Whether the terminal declared itself `TERM=dumb`, as of construction
/// or the last resize.
pub(crate) fn term_is_dumb(&self) -> bool {
self.term_is_dumb
}

/// Computes the right prompt position when the terminal can position it.
fn compute_right_prompt(
&self,
Expand Down
5 changes: 3 additions & 2 deletions src/prompt/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
//! visual, helix's select) would otherwise have nowhere to sit.

use crate::prompt::base::PromptHelixMode;
use crate::utils::environment::var_os;
use crate::{Prompt, PromptEditMode, PromptHistorySearch, PromptHistorySearchStatus, PromptViMode};

use {
Expand Down Expand Up @@ -148,8 +149,8 @@ fn get_working_dir() -> Result<String, std::io::Error> {
let cwd = env::current_dir()?;
// `USERPROFILE` on Windows, `HOME` elsewhere. Avoids `env::home_dir()`,
// which is buggy on Windows before 1.85 (above our 1.63 MSRV).
let home = crate::utils::environment::var_os("USERPROFILE")
.or_else(|| crate::utils::environment::var_os("HOME"))
let home = var_os("USERPROFILE")
.or_else(|| var_os("HOME"))
.map(std::path::PathBuf::from);
Ok(format_working_dir(&cwd, home.as_deref()))
}
Expand Down
19 changes: 6 additions & 13 deletions src/utils/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,22 @@ pub(crate) fn term_is_dumb(term: Option<&OsStr>) -> bool {
term == Some(OsStr::new("dumb"))
}

/// Whether ANSI coloring is appropriate for the declared terminal.
///
/// An unset or non-dumb `TERM` preserves the configured Reedline behavior.
pub(crate) fn term_supports_ansi(term: Option<&OsStr>) -> bool {
!term_is_dumb(term)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn term_dumb_does_not_support_ansi() {
assert!(!term_supports_ansi(Some(OsStr::new("dumb"))));
fn term_dumb_is_dumb() {
assert!(term_is_dumb(Some(OsStr::new("dumb"))));
}

#[test]
fn regular_term_supports_ansi() {
assert!(term_supports_ansi(Some(OsStr::new("xterm-256color"))));
fn regular_term_is_not_dumb() {
assert!(!term_is_dumb(Some(OsStr::new("xterm-256color"))));
}

#[test]
fn unset_term_does_not_disable_ansi() {
assert!(term_supports_ansi(None));
fn unset_term_is_not_dumb() {
assert!(!term_is_dumb(None));
}
}
Loading