1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use std::fmt;
use std::env::var;
use std::process::Command;

pub mod colors;
pub use crate::colors::Colors;

#[derive(Debug)]
pub struct Title {
	username: String,
	hostname: String
}

pub fn username() -> String {
	var("USERNAME").unwrap()
}

#[cfg(target_family = "unix")]
pub fn hostname() -> Result<String, ()> {
	Ok(var("HOSTNAME").unwrap())
}

#[cfg(target_family = "windows")]
pub fn hostname() -> Result<String, ()> {
	let output = Command::new("hostname")
		.output();

	match output {
		Ok(output) => return Ok(String::from_utf8(output.stdout).unwrap()),
		Err(_) => return Err(()),
	}
}

pub fn title() -> Title {
	let username = username();
	let hostname = hostname().unwrap_or("Unknown".to_string());

	Title {
		username,
		hostname
	}
}

#[cfg(target_family = "unix")]
pub fn os() -> Result<String, ()> {
	let output = Command::new("lsb_release")
		.arg("-sd")
		.output();

	match output {
		Ok(output) => return Ok(String::from_utf8(output.stdout).unwrap()),
		Err(_) => return Err(()),
	}
}

#[cfg(target_family = "windows")]
pub fn os() -> Result<String, ()> {
	let output = Command::new("wmic")
		.args(&["os", "get", "Caption"])
		.output();

	match output {
		Ok(output_) => {
			let output = String::from_utf8(output_.stdout).unwrap();
			let pat: Vec<&str> = output.split_terminator("\r\r\n").collect();
			let os = pat[1];
			return Ok(os.trim().to_string().split_off(10))
		},
		Err(_) => return Err(()),
	}
}

#[cfg(target_family = "unix")]
pub fn kernel() -> Result<String, ()> {
	let output = Command::new("uname")
		.arg("-r")
		.output();

	match output {
		Ok(output) => return Ok(String::from_utf8(output.stdout).unwrap()),
		Err(_) => return Err(()),
	}
}

#[cfg(target_family = "windows")]
pub fn kernel() -> Result<String, ()> {
	let output = Command::new("wmic")
		.args(&["os", "get", "Version"])
		.output();

	match output {
		Ok(output_) => {
			let output = String::from_utf8(output_.stdout).unwrap();
			let pat: Vec<&str> = output.split_terminator("\r\r\n").collect();
			let os = pat[1];
			return Ok(os.trim().to_string())
		},
		Err(_) => return Err(()),
	}
}

#[cfg(target_family = "unix")]
pub fn de() -> Result<String, ()> {
	Ok("Unknown".to_string())
}

#[cfg(target_family = "windows")]
pub fn de() -> Result<String, ()> {
	let os = os().unwrap();
	let pat: Vec<&str> = os.split_terminator(" ").collect();
	if pat[1].trim() == "7" {
		Ok("Aero".to_string())
	} else {
		Ok("Metro".to_string())
	}
}

impl fmt::Display for Title {
	fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
		write!(f, "{}@{}", self.username, self.hostname)
	}
}

#[macro_export]
macro_rules! printr {
	() => (print!("\x1b[0m"));
	($($arg:tt)*) => ({
		print!("{}\x1b[0m", format_args!($($arg)*));
	})
}

#[macro_export]
macro_rules! printlnr {
	() => (println!("\x1b[0m"));
	($($arg:tt)*) => ({
		println!("{}\x1b[0m", format_args!($($arg)*));
	})
}