summaryrefslogtreecommitdiffstats
path: root/src/modules
diff options
context:
space:
mode:
authorJohn Letey <30328854+johnletey@users.noreply.github.com>2019-09-10 18:54:40 +0100
committerKevin Song <chipbuster@users.noreply.github.com>2019-09-10 12:54:40 -0500
commitf9a45140459c9f155a2be46c2c51d3c186bb3175 (patch)
treeaf2d8a5a21d4261ca10c7ba37efc1b40ee7c2278 /src/modules
parent7d02f718c85834eed558d80fdecbebbb7f8513b0 (diff)
feat: Implement the prompt module for time (#138)
Add a module which displays the current time in a format requested by the user. Disabled by default.
Diffstat (limited to 'src/modules')
-rw-r--r--src/modules/mod.rs2
-rw-r--r--src/modules/time.rs105
2 files changed, 107 insertions, 0 deletions
diff --git a/src/modules/mod.rs b/src/modules/mod.rs
index 83a7d1c40..34f1cc12b 100644
--- a/src/modules/mod.rs
+++ b/src/modules/mod.rs
@@ -15,6 +15,7 @@ mod package;
mod python;
mod ruby;
mod rust;
+mod time;
mod username;
#[cfg(feature = "battery")]
@@ -44,6 +45,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"jobs" => jobs::module(context),
"nix_shell" => nix_shell::module(context),
"hostname" => hostname::module(context),
+ "time" => time::module(context),
_ => {
eprintln!("Error: Unknown module {}. Use starship module --list to list out all supported modules.", module);
diff --git a/src/modules/time.rs b/src/modules/time.rs
new file mode 100644
index 000000000..d76a5c1e1
--- /dev/null
+++ b/src/modules/time.rs
@@ -0,0 +1,105 @@
+use ansi_term::Color;
+use chrono::offset::TimeZone;
+use chrono::{DateTime, Local};
+
+use super::{Context, Module};
+
+/// Outputs the current time
+pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
+ let mut module = context.new_module("time");
+
+ // Remove when logic for disabled by default exists
+ if module.config_value_bool("disabled").unwrap_or(true) {
+ return None;
+ }
+
+ let module_style = module
+ .config_value_style("style")
+ .unwrap_or_else(|| Color::Yellow.bold());
+ module.set_style(module_style);
+
+ // Load module settings
+ let is_12hr = module.config_value_bool("12hr").unwrap_or(false);
+
+ let default_format = if is_12hr { "%r" } else { "%T" };
+ let time_format = module
+ .config_value_str("format")
+ .unwrap_or(default_format)
+ .to_owned();
+
+ log::trace!(
+ "Timer module is enabled with format string: {}",
+ time_format
+ );
+
+ let local: DateTime<Local> = Local::now();
+ let formatted_time_string = format_time(&time_format, local);
+ module.new_segment("time", &formatted_time_string);
+ module.get_prefix().set_value("at ");
+
+ Some(module)
+}
+
+/// Format a given time into the given string. This function should be referentially
+/// transparent, which makes it easy to test (unlike anything involving the actual time)
+fn format_time(time_format: &str, localtime: DateTime<Local>) -> String {
+ localtime.format(time_format).to_string()
+}
+
+/* Because we cannot do integration tests on the time module, these unit
+tests become extra important */
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ const FMT_12: &str = "%r";
+ const FMT_24: &str = "%T";
+
+ #[test]
+ fn test_midnight_12hr() {
+ let time = Local.ymd(2014, 7, 8).and_hms(0, 0, 0);
+ let formatted = format_time(FMT_12, time);
+ assert_eq!(formatted, "12:00:00 AM");
+ }
+
+ #[test]
+ fn test_midnight_24hr() {
+ let time = Local.ymd(2014, 7, 8).and_hms(0, 0, 0);
+ let formatted = format_time(FMT_24, time);
+ assert_eq!(formatted, "00:00:00");
+ }
+
+ #[test]
+ fn test_noon_12hr() {
+ let time = Local.ymd(2014, 7, 8).and_hms(12, 0, 0);
+ let formatted = format_time(FMT_12, time);
+ assert_eq!(formatted, "12:00:00 PM");
+ }
+
+ #[test]
+ fn test_noon_24hr() {
+ let time = Local.ymd(2014, 7, 8).and_hms(12, 0, 0);
+ let formatted = format_time(FMT_24, time);
+ assert_eq!(formatted, "12:00:00");
+ }
+
+ #[test]
+ fn test_arbtime_12hr() {
+ let time = Local.ymd(2014, 7, 8).and_hms(15, 36, 47);
+ let formatted = format_time(FMT_12, time);
+ assert_eq!(formatted, "03:36:47 PM");
+ }
+
+ #[test]
+ fn test_arbtime_24hr() {
+ let time = Local.ymd(2014, 7, 8).and_hms(15, 36, 47);
+ let formatted = format_time(FMT_24, time);
+ assert_eq!(formatted, "15:36:47");
+ }
+
+ fn test_format_with_paren() {
+ let time = Local.ymd(2014, 7, 8).and_hms(15, 36, 47);
+ let formatted = format_time("[%T]", time);
+ assert_eq!(formatted, "[15:36:47]");
+ }
+}