yazi_shared/
env.rs

1use std::fmt::{Display, Formatter};
2
3pub static LOG_LEVEL: crate::SyncCell<LogLevel> = crate::SyncCell::new(LogLevel::None);
4
5#[inline]
6pub fn env_exists(name: &str) -> bool { std::env::var_os(name).is_some_and(|s| !s.is_empty()) }
7
8#[inline]
9pub fn in_wsl() -> bool {
10	#[cfg(target_os = "linux")]
11	{
12		std::fs::read("/proc/sys/kernel/osrelease")
13			.is_ok_and(|b| b.windows(11).any(|w| w == b"-microsoft-"))
14	}
15	#[cfg(not(target_os = "linux"))]
16	{
17		false
18	}
19}
20
21#[inline]
22pub fn in_ssh_connection() -> bool {
23	env_exists("SSH_CLIENT") || env_exists("SSH_TTY") || env_exists("SSH_CONNECTION")
24}
25
26// LogLevel
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub enum LogLevel {
29	None,
30	Error,
31	Warn,
32	Info,
33	Debug,
34}
35
36impl LogLevel {
37	#[inline]
38	pub fn is_none(self) -> bool { self == Self::None }
39}
40
41impl From<String> for LogLevel {
42	fn from(mut s: String) -> Self {
43		s.make_ascii_uppercase();
44		match s.as_str() {
45			"ERROR" => Self::Error,
46			"WARN" => Self::Warn,
47			"INFO" => Self::Info,
48			"DEBUG" => Self::Debug,
49			_ => Self::None,
50		}
51	}
52}
53
54impl AsRef<str> for LogLevel {
55	fn as_ref(&self) -> &str {
56		match self {
57			Self::None => "yazi=NONE",
58			Self::Error => "yazi=ERROR",
59			Self::Warn => "yazi=WARN",
60			Self::Info => "yazi=INFO",
61			Self::Debug => "yazi=DEBUG",
62		}
63	}
64}
65
66impl Display for LogLevel {
67	fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.as_ref()) }
68}