From f702aae72fd4d218ead1bd639d99b999198fef3b Mon Sep 17 00:00:00 2001 From: "Joseph T. Lyons" Date: Sun, 10 May 2020 20:35:24 -0400 Subject: [PATCH] =?UTF-8?q?Don't=20include=20year=20and=20month=20by=20def?= =?UTF-8?q?ault,=20adds=20an=20option=20to=20display=20th=E2=80=A6=20(#174?= =?UTF-8?q?5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Don't include year and month by default, adds an option to display the quarters of the year * Add a test for cal that checks that year requested is in the return * rustfmt the cal tests --- crates/nu-cli/src/commands/cal.rs | 74 +++++++++++++++++++---------- crates/nu-cli/tests/commands/cal.rs | 20 ++++++-- docs/commands/cal.md | 7 ++- 3 files changed, 72 insertions(+), 29 deletions(-) diff --git a/crates/nu-cli/src/commands/cal.rs b/crates/nu-cli/src/commands/cal.rs index 2c0f41b1c8..6f54968c4a 100644 --- a/crates/nu-cli/src/commands/cal.rs +++ b/crates/nu-cli/src/commands/cal.rs @@ -3,7 +3,7 @@ use chrono::{DateTime, Datelike, Local, NaiveDate}; use nu_errors::ShellError; use nu_protocol::Dictionary; -use crate::commands::WholeStreamCommand; +use crate::commands::{command::EvaluatedWholeStreamCommandArgs, WholeStreamCommand}; use indexmap::IndexMap; use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value}; @@ -16,16 +16,19 @@ impl WholeStreamCommand for Cal { fn signature(&self) -> Signature { Signature::build("cal") + .switch("year", "Display the year column", Some('y')) + .switch("quarter", "Display the quarter column", Some('q')) + .switch("month", "Display the month column", Some('m')) + .named( + "full-year", + SyntaxShape::Int, + "Display a year-long calendar for the specified year", + None, + ) .switch( "month-names", "Display the month names instead of integers", - Some('m'), - ) - .named( - "year", - SyntaxShape::Int, - "Display a year-long calendar for the specified year", - Some('y'), + None, ) } @@ -48,14 +51,12 @@ pub fn cal(args: CommandArgs, registry: &CommandRegistry) -> Result = Some(current_day); - let year_option = args.get("year"); let mut year_value = current_year as u64; - if let Some(year) = year_option { + if let Some(year) = args.get("full-year") { if let Ok(year_u64) = year.as_u64() { year_value = year_u64; } @@ -72,7 +73,7 @@ pub fn cal(args: CommandArgs, registry: &CommandRegistry) -> Result Result, - should_show_month_names: bool, + args: &EvaluatedWholeStreamCommandArgs, ) { for month_number in 1..=12 { let (day_start_offset, number_of_days_in_month, chosen_date_is_valid) = @@ -136,7 +137,7 @@ fn add_year_to_table( new_current_day_option, day_start_offset, number_of_days_in_month, - should_show_month_names, + &args, ); } } @@ -150,7 +151,7 @@ fn add_month_to_table( _current_day_option: Option, // Can be used in the future to display current day day_start_offset: usize, number_of_days_in_month: usize, - should_show_month_names: bool, + args: &EvaluatedWholeStreamCommandArgs, ) { let day_limit = number_of_days_in_month + day_start_offset; let mut day_count: usize = 1; @@ -165,18 +166,34 @@ fn add_month_to_table( "saturday", ]; + let should_show_year_column = args.has("year"); + let should_show_month_column = args.has("month"); + let should_show_quarter_column = args.has("quarter"); + let should_show_month_names = args.has("month-names"); + while day_count <= day_limit { let mut indexmap = IndexMap::new(); - indexmap.insert("year".to_string(), UntaggedValue::int(year).into_value(tag)); + if should_show_year_column { + indexmap.insert("year".to_string(), UntaggedValue::int(year).into_value(tag)); + } - let month_value = if should_show_month_names { - UntaggedValue::string(get_month_name(month)).into_value(tag) - } else { - UntaggedValue::int(month).into_value(tag) - }; + if should_show_quarter_column { + indexmap.insert( + "quarter".to_string(), + UntaggedValue::int(get_quarter_number(month)).into_value(tag), + ); + } - indexmap.insert("month".to_string(), month_value); + if should_show_month_column { + let month_value = if should_show_month_names { + UntaggedValue::string(get_month_name(month)).into_value(tag) + } else { + UntaggedValue::int(month).into_value(tag) + }; + + indexmap.insert("month".to_string(), month_value); + } for day in &days_of_the_week { let value = if (day_count <= day_limit) && (day_count > day_start_offset) { @@ -195,6 +212,15 @@ fn add_month_to_table( } } +fn get_quarter_number(month_number: u32) -> u8 { + match month_number { + 1..=3 => 1, + 4..=6 => 2, + 7..=9 => 3, + _ => 4, + } +} + fn get_month_name(month_number: u32) -> String { let month_name = match month_number { 1 => "january", diff --git a/crates/nu-cli/tests/commands/cal.rs b/crates/nu-cli/tests/commands/cal.rs index 16a0d7640c..82875ab7ce 100644 --- a/crates/nu-cli/tests/commands/cal.rs +++ b/crates/nu-cli/tests/commands/cal.rs @@ -1,11 +1,25 @@ use nu_test_support::{nu, pipeline}; +#[test] +fn cal_full_year() { + let actual = nu!( + cwd: ".", pipeline( + r#" + cal -y --full-year 2010 | first | to json + "# + )); + + let first_week_2010_json = r#"{"year":2010,"sunday":null,"monday":null,"tuesday":null,"wednesday":null,"thurday":null,"friday":1,"saturday":2}"#; + + assert_eq!(actual.out, first_week_2010_json); +} + #[test] fn cal_february_2020_leap_year() { let actual = nu!( cwd: ".", pipeline( r#" - cal -my 2020 | where month == "february" | to json + cal -ym --full-year 2020 --month-names | where month == "february" | to json "# )); @@ -19,7 +33,7 @@ fn cal_friday_the_thirteenths_in_2015() { let actual = nu!( cwd: ".", pipeline( r#" - cal -ym 2015 | where friday == 13 | count + cal --full-year 2015 | where friday == 13 | count "# )); @@ -31,7 +45,7 @@ fn cal_rows_in_2020() { let actual = nu!( cwd: ".", pipeline( r#" - cal -y 2020 | count + cal --full-year 2020 | count "# )); diff --git a/docs/commands/cal.md b/docs/commands/cal.md index 1d52349f5e..c11ace38f1 100644 --- a/docs/commands/cal.md +++ b/docs/commands/cal.md @@ -4,8 +4,11 @@ Use `cal` to display a calendar. ## Flags -* `-m`, `--month-names`: Display the month names instead of integers -* `-y`, `--year`: Display a year-long calendar for the specified year +* `-y`, `--year`: Display the year column +* `-q`, `--quarter`: Display the quarter column +* `-m`, `--month`: Display the month column +* `--full-year` \: Display a year-long calendar for the specified year +* `--month-names`: Display the month names instead of integers ## Examples