24 lines
923 B
Rust
24 lines
923 B
Rust
use ansi_term::ANSIGenericString;
|
||
use ansi_term::Colour::RGB;
|
||
use std::env::var; // For environment variables
|
||
|
||
pub const SSH_SYMBOL: &str = "\u{276F}"; // SSH indicator symbol: "❯"
|
||
pub const SSH_COL: ansi_term::Colour = RGB(255, 149, 0); // In SSH session
|
||
pub const NORMIE_COL: ansi_term::Colour = RGB(82, 82, 82); // Non-SSH session
|
||
const SSH_ENV_VARS: [&str; 2] = ["SSH_TTY", "SSH_CONNECTION"]; // Environment variables normally present in SSH sessions
|
||
|
||
/// Checks if current session is an SSH session
|
||
fn is_ssh_session() -> bool {
|
||
SSH_ENV_VARS.iter().any(|&var_name| var(var_name).is_ok()) // any() iterates through the iter and stops at first `true`. Equal to `||`(`or` condition)
|
||
}
|
||
|
||
/// SSH shell indicator
|
||
pub fn indicator() -> ANSIGenericString<'static, str> {
|
||
let is_ssh: bool = is_ssh_session();
|
||
if is_ssh {
|
||
SSH_COL.paint(SSH_SYMBOL)
|
||
} else {
|
||
NORMIE_COL.paint(SSH_SYMBOL)
|
||
}
|
||
}
|