diff options
-rw-r--r-- | src/args.rs | 40 | ||||
-rw-r--r-- | src/data.rs | 104 | ||||
-rw-r--r-- | src/main.rs | 71 | ||||
-rw-r--r-- | src/prompt.rs | 21 | ||||
-rw-r--r-- | src/system_info.rs | 49 |
5 files changed, 152 insertions, 133 deletions
diff --git a/src/args.rs b/src/args.rs new file mode 100644 index 0000000..90e33b7 --- /dev/null +++ b/src/args.rs @@ -0,0 +1,40 @@ +use clap; + +use colors; + +pub struct CommandLineOptions { + pub shell: colors::ShellType, + pub error_code: u8, +} + +pub fn parse() -> CommandLineOptions { + let matches = clap::App::new("fancy-prompt") + .about("Prints a fancy prompt") + .author(crate_authors!()) + .version(crate_version!()) + .arg(clap::Arg::with_name("prompt-escape") + .long("prompt-escape") + .value_name("SHELL") + .help("Produces escape sequence wrappers for the given shell") + .takes_value(true)) + .arg(clap::Arg::with_name("error-code") + .value_name("ERROR_CODE") + .help("The error code of the previously run command") + ) + .get_matches(); + + let shell = matches + .value_of("prompt-escape") + .map(|shell| colors::ShellType::from_str(shell)) + .unwrap_or(colors::ShellType::Unknown); + let error_code = matches + .value_of("error-code") + .map(|code| code.parse().expect("error code must be a u8")) + .unwrap_or(0); + + CommandLineOptions { + shell, + error_code, + } +} + diff --git a/src/data.rs b/src/data.rs new file mode 100644 index 0000000..be6ea9f --- /dev/null +++ b/src/data.rs @@ -0,0 +1,104 @@ +use chrono; +use hostname; +use term_size; +use std; +use users; + +use args; +use colors; +use power; +use vcs; + +pub struct PromptData { + pub shell: colors::ShellType, + pub error_code: u8, + pub hostname: Option<String>, + pub terminal_cols: Option<usize>, + pub pwd: Option<std::path::PathBuf>, + pub home: Option<std::path::PathBuf>, + pub user: Option<String>, + pub is_root: bool, + pub time: chrono::DateTime<chrono::Local>, + pub power_info: power::PowerInfo, + pub vcs_info: Option<Box<vcs::VcsInfo>>, +} + +pub fn collect(opts: args::CommandLineOptions) -> PromptData { + start_talking_about_time!("collecting data"); + + let hostname = hostname(); + talk_about_time!("hostname"); + let terminal_cols = terminal_cols(); + talk_about_time!("terminal_cols"); + let pwd = pwd(); + talk_about_time!("pwd"); + let home = home(); + talk_about_time!("home"); + let user = user(); + talk_about_time!("user"); + let is_root = is_root(); + talk_about_time!("is_root"); + let time = time(); + talk_about_time!("time"); + let power_info = power_info(); + talk_about_time!("power_info"); + let vcs_info = vcs_info(); + talk_about_time!("vcs_info"); + + stop_talking_about_time!(); + + PromptData { + shell: opts.shell, + error_code: opts.error_code, + hostname, + terminal_cols, + pwd, + home, + user, + is_root, + time, + power_info, + vcs_info, + } +} + +fn hostname() -> Option<String> { + hostname::get_hostname() +} + +fn terminal_cols() -> Option<usize> { + if let Some((w, _h)) = term_size::dimensions() { + Some(w) + } + else { + None + } +} + +fn pwd() -> Option<std::path::PathBuf> { + std::env::var("PWD").map(std::path::PathBuf::from).ok() +} + +fn home() -> Option<std::path::PathBuf> { + std::env::var("HOME").map(std::path::PathBuf::from).ok() +} + +fn user() -> Option<String> { + users::get_current_username() +} + +fn is_root() -> bool { + users::get_current_uid() == 0 +} + +fn time() -> chrono::DateTime<chrono::Local> { + chrono::Local::now() +} + +fn power_info() -> power::PowerInfo { + power::PowerInfo::new() +} + +fn vcs_info() -> Option<Box<vcs::VcsInfo>> { + vcs::detect() +} diff --git a/src/main.rs b/src/main.rs index 63f73ed..146e633 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,79 +12,18 @@ extern crate walkdir; #[macro_use] mod verbose; +mod args; mod colors; +mod data; mod power; mod prompt; -mod system_info; mod vcs; -fn collect_data() -> prompt::PromptData { - start_talking_about_time!("collecting data"); - - let matches = clap::App::new("fancy-prompt") - .about("Prints a fancy prompt") - .author(crate_authors!()) - .version(crate_version!()) - .arg(clap::Arg::with_name("prompt-escape") - .long("prompt-escape") - .value_name("SHELL") - .help("Produces escape sequence wrappers for the given shell") - .takes_value(true)) - .arg(clap::Arg::with_name("error-code") - .value_name("ERROR_CODE") - .help("The error code of the previously run command") - ) - .get_matches(); - - let shell = matches - .value_of("prompt-escape") - .map(|shell| colors::ShellType::from_str(shell)) - .unwrap_or(colors::ShellType::Unknown); - let error_code = matches - .value_of("error-code") - .map(|code| code.parse().expect("error code must be a u8")) - .unwrap_or(0); - talk_about_time!("command line arg parsing"); - - let hostname = system_info::hostname(); - talk_about_time!("hostname"); - let terminal_cols = system_info::terminal_cols(); - talk_about_time!("terminal_cols"); - let pwd = system_info::pwd(); - talk_about_time!("pwd"); - let home = system_info::home(); - talk_about_time!("home"); - let user = system_info::user(); - talk_about_time!("user"); - let is_root = system_info::is_root(); - talk_about_time!("is_root"); - let time = system_info::time(); - talk_about_time!("time"); - let power_info = system_info::power_info(); - talk_about_time!("power_info"); - let vcs_info = system_info::vcs_info(); - talk_about_time!("vcs_info"); - - stop_talking_about_time!(); - - prompt::PromptData { - shell, - error_code, - hostname, - terminal_cols, - pwd, - home, - user, - is_root, - time, - power_info, - vcs_info, - } -} - fn main() { start_talking_about_time!("main"); - let data = collect_data(); + let opts = args::parse(); + talk_about_time!("parsing args"); + let data = data::collect(opts); talk_about_time!("collecting data"); prompt::Prompt::new(data).display(); talk_about_time!("displaying data"); diff --git a/src/prompt.rs b/src/prompt.rs index 05598f5..706ed04 100644 --- a/src/prompt.rs +++ b/src/prompt.rs @@ -1,4 +1,3 @@ -use chrono; use regex; use std; use users; @@ -8,30 +7,16 @@ use std::os::linux::fs::MetadataExt; use std::os::unix::fs::PermissionsExt; use colors; -use power; +use data; use vcs; pub struct Prompt { colors: colors::Colors, - data: PromptData, -} - -pub struct PromptData { - pub shell: colors::ShellType, - pub error_code: u8, - pub hostname: Option<String>, - pub terminal_cols: Option<usize>, - pub pwd: Option<std::path::PathBuf>, - pub home: Option<std::path::PathBuf>, - pub user: Option<String>, - pub is_root: bool, - pub time: chrono::DateTime<chrono::Local>, - pub power_info: power::PowerInfo, - pub vcs_info: Option<Box<vcs::VcsInfo>>, + data: data::PromptData, } impl Prompt { - pub fn new(data: PromptData) -> Prompt { + pub fn new(data: data::PromptData) -> Prompt { let colors = colors::Colors::new(data.shell.clone()); Prompt { colors, diff --git a/src/system_info.rs b/src/system_info.rs deleted file mode 100644 index 25b5a67..0000000 --- a/src/system_info.rs +++ /dev/null @@ -1,49 +0,0 @@ -use chrono; -use hostname; -use term_size; -use std; -use users; - -use power; -use vcs; - -pub fn hostname() -> Option<String> { - hostname::get_hostname() -} - -pub fn terminal_cols() -> Option<usize> { - if let Some((w, _h)) = term_size::dimensions() { - Some(w) - } - else { - None - } -} - -pub fn pwd() -> Option<std::path::PathBuf> { - std::env::var("PWD").map(std::path::PathBuf::from).ok() -} - -pub fn home() -> Option<std::path::PathBuf> { - std::env::var("HOME").map(std::path::PathBuf::from).ok() -} - -pub fn user() -> Option<String> { - users::get_current_username() -} - -pub fn is_root() -> bool { - users::get_current_uid() == 0 -} - -pub fn time() -> chrono::DateTime<chrono::Local> { - chrono::Local::now() -} - -pub fn power_info() -> power::PowerInfo { - power::PowerInfo::new() -} - -pub fn vcs_info() -> Option<Box<vcs::VcsInfo>> { - vcs::detect() -} |