use crate::terminal::detect::{TerminalDetector, TerminalMode};
pub struct TerminalCapabilities {
detector: TerminalDetector,
#[allow(dead_code)]
width: Option<usize>,
}
impl TerminalCapabilities {
pub fn new() -> Self {
let detector = TerminalDetector::new();
let width = Self::detect_width();
Self { detector, width }
}
#[allow(dead_code)]
pub fn with_detector(detector: TerminalDetector) -> Self {
let width = Self::detect_width();
Self { detector, width }
}
#[allow(dead_code)]
pub fn width(&self) -> usize {
self.width.unwrap_or(80)
}
#[allow(dead_code)]
pub fn supports_animation(&self) -> bool {
self.detector.is_tty() && !Self::is_fun_disabled()
}
#[allow(dead_code)]
fn is_fun_disabled() -> bool {
if let Ok(val) = std::env::var("NO_FUN") {
!val.is_empty() && val != "0"
} else {
false
}
}
#[allow(dead_code)]
pub fn supports_emoji(&self) -> bool {
self.detector.is_tty() && self.detector.supports_unicode()
}
#[allow(dead_code)]
pub fn supports_colors(&self) -> bool {
self.detector.should_use_colors()
}
pub fn supports_unicode_trees(&self) -> bool {
self.detector.supports_unicode()
}
pub fn tree_chars(&self) -> TreeChars {
if self.supports_unicode_trees() {
TreeChars::unicode()
} else {
TreeChars::ascii()
}
}
#[allow(dead_code)]
pub fn progress_chars(&self) -> ProgressChars {
if self.detector.supports_unicode() {
ProgressChars::unicode()
} else {
ProgressChars::ascii()
}
}
fn detect_width() -> Option<usize> {
if let Ok(cols) = std::env::var("COLUMNS") {
cols.parse().ok()
} else {
Some(80)
}
}
#[allow(dead_code)]
pub fn output_mode(&self) -> TerminalMode {
self.detector.output_mode()
}
}
impl Default for TerminalCapabilities {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)]
pub struct TreeChars {
pub branch: &'static str,
pub last_branch: &'static str,
pub vertical: &'static str,
pub empty: &'static str,
}
impl TreeChars {
pub fn unicode() -> Self {
Self {
branch: "├─ ",
last_branch: "└─ ",
vertical: "│ ",
empty: " ",
}
}
pub fn ascii() -> Self {
Self {
branch: "|-- ",
last_branch: "`-- ",
vertical: "| ",
empty: " ",
}
}
}
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct ProgressChars {
pub filled: &'static str,
pub empty: &'static str,
pub filled_md: &'static str,
pub empty_md: &'static str,
}
impl ProgressChars {
pub fn unicode() -> Self {
Self {
filled: "█",
empty: "░",
filled_md: "▰",
empty_md: "▱",
}
}
pub fn ascii() -> Self {
Self {
filled: "#",
empty: "-",
filled_md: "#",
empty_md: "-",
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_tree_chars_unicode() {
let caps = TerminalCapabilities::new();
if caps.supports_unicode_trees() {
let chars = caps.tree_chars();
assert_eq!(chars.vertical, "│ ");
assert_eq!(chars.branch, "├── ");
assert_eq!(chars.last_branch, "└── ");
assert_eq!(chars.empty, " ");
}
}
}