Skip to content

Commit

Permalink
Added holiday display with -d
Browse files Browse the repository at this point in the history
Updated libraries
 - jansi: 2.4.0 -> 2.4.1
 - junit: 5.10.0 -> 5.10.1
 Lookup country holidays per user's JVM locale
 Colorize those days
 Display a list of country holidays at the end
  • Loading branch information
frossm committed Dec 1, 2023
1 parent 2325834 commit ff2076e
Show file tree
Hide file tree
Showing 9 changed files with 330 additions and 20 deletions.
14 changes: 11 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.fross</groupId>
<artifactId>cal</artifactId>
<version>2.4.5</version>
<version>2.5.0</version>
<packaging>jar</packaging>

<name>cal</name>
Expand Down Expand Up @@ -202,17 +202,25 @@
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>2.4.0</version>
<version>2.4.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<version>5.10.1</version>
<scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>

<!-- Fross library functions: https://github.com/frossm/library -->
<dependency>
<groupId>org.fross</groupId>
<artifactId>library</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: fcal
version: '2.4.5'
version: '2.5.0'
summary: Command line calendar display
description: |
fCal is a command line calendar utility. It will display a
Expand Down
122 changes: 112 additions & 10 deletions src/main/java/org/fross/cal/Calendar.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/******************************************************************************
* Cal - A command line calendar utility
*
* Copyright (c) 2019-2022 Michael Fross
* Copyright (c) 2019-2024 Michael Fross
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -27,8 +27,10 @@
import static org.fusesource.jansi.Ansi.ansi;

import java.util.Arrays;
import java.util.TreeMap;

import org.fross.library.Date;
import org.fross.library.Format;
import org.fross.library.Output;
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.Ansi.Attribute;
Expand All @@ -43,9 +45,11 @@ public class Calendar {
"November", "December" };
static protected final Color TODAYHIGHLIGHT_FG = Ansi.Color.WHITE;
static protected final Color TODAYHIGHLIGHT_BG = Ansi.Color.BLUE;
static protected final Color HOLIDAYHIGHLIGHT_FG = Ansi.Color.BLUE;

// Class Variables
static int calsPerRow = DEFAULT_CALS_PER_ROW;
static TreeMap<String, String> holidayList = null;

/**
* setCalsPerRow(): Sets the number of calendars per row when showing an entire year
Expand Down Expand Up @@ -136,6 +140,10 @@ public static void printYear(int year) {
Output.debugPrintln(" 1 2 3 4 5 6 7 8 9 1\n"
+ "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");

if (Holidays.queryHolidaysEnabled() == true) {
holidayList = Holidays.getHolidays(year);
}

// Loop through the calendar rows
for (i = 0; i < 12; i = i + calsPerRow) {
// Initialize the arrays
Expand Down Expand Up @@ -170,6 +178,61 @@ public static void printYear(int year) {
// Put a new line between calendar rows
Output.println("");
}

// If display holidays is enabled, display the list after the calendar
if (Holidays.queryHolidaysEnabled() == true) {
int displayWidth = (CALENDARWIDTH + SPACESBETWEENCALS) * calsPerRow;
Output.debugPrintln("Width of calendar display for centering: " + displayWidth);

// Display the holiday display header
String header = Holidays.queryCountry() + " holidays for " + year;
try {
Output.printColorln(Ansi.Color.YELLOW, Format.CenterText(displayWidth, header));
} catch (Exception ex) {
Output.printColorln(Ansi.Color.YELLOW, header);
}

// List the holidays
Object[] keySet = holidayList.keySet().toArray();
String keyLeft = "";
String keyRight = "";
for (int l = 0; l < ((holidayList.size() + 1) / 2); l++) {
try {
keyLeft = keySet[l].toString();

// Process odd numbers and even numbers differently
if ((holidayList.size() % 2) == 0) {
keyRight = keySet[keySet.length / 2 + l].toString();
} else {
keyRight = keySet[(keySet.length + 1) / 2 + l].toString();
}

// Build output data removing the year as it's redundant
String outputLeft = keyLeft.substring(5) + "|" + holidayList.get(keyLeft);
String outputRight = keyRight.substring(5) + "|" + holidayList.get(keyRight);

// Shorten the holiday name if it's longer than 1/2 the display width
if (outputLeft.length() > (displayWidth / 2 - 1)) {
outputLeft = outputLeft.substring(0, displayWidth / 2 - 5);
outputLeft = outputLeft + "..>";
}

// Display the left item and the spacer
Output.printColor(Ansi.Color.CYAN, outputLeft);
Output.print(" ".repeat((displayWidth / 2) - outputLeft.length()));

// Display the Right column item
Output.printColorln(Ansi.Color.CYAN, outputRight);

} catch (ArrayIndexOutOfBoundsException ex) {
// Display the left side and nothing on the right for odd number of holidays
Output.printColor(Ansi.Color.CYAN, keyLeft + ":" + holidayList.get(keyLeft));

} catch (IllegalArgumentException ex) {
Output.printColorln(Ansi.Color.RED, "ERROR: Could not display holiday list correctly");
}
}
}
}

/**
Expand Down Expand Up @@ -228,22 +291,31 @@ public static String[] getCalDays(int month, int year) {
returnStringLen[row] = returnString[row].length();

// Create the day strings. After 7 days start a new line.
for (int i = 1; i <= daysInMonth[month]; i++) {
// If we get to today's date, add colorization as long as the 'disable color' flag is not set
if (month == Date.getCurrentMonth() && year == Date.getCurrentYear() && i == Date.getCurrentDay() && Output.queryColorEnabled() == true) {
String today = ansi().a(Attribute.INTENSITY_BOLD).fg(TODAYHIGHLIGHT_FG).bg(TODAYHIGHLIGHT_BG).a(String.format("%2d", i)).reset().toString();
returnString[row] += String.format("%s ", today);

for (int day = 1; day <= daysInMonth[month]; day++) {
// Build the colorized days
String colorizedDay = "";

// Colorize Today
if (month == Date.getCurrentMonth() && year == Date.getCurrentYear() && day == Date.getCurrentDay()) {
colorizedDay = ColorizeDay(day, TODAYHIGHLIGHT_FG, TODAYHIGHLIGHT_BG);
returnString[row] += String.format("%s ", colorizedDay);

// If holiday display is on, check to see if the current day we're processing is one
} else if (Holidays.queryHolidaysEnabled() == true
&& holidayList.get(year + "-" + String.format("%02d", month) + "-" + String.format("%02d", day)) != null) {
colorizedDay = ColorizeDay(day, HOLIDAYHIGHLIGHT_FG);
returnString[row] += String.format("%s ", colorizedDay);

// No colorization needed
} else {
// Add the day to the month's row
returnString[row] += String.format("%2d ", i);
returnString[row] += String.format("%2d ", day);
}

// Update the length of the row's length with 3 spaces (a day's width)
returnStringLen[row] += 3;

// Start a new row after 7 days or if we are at the end of the month after padding
if (((i + firstDayOfMon) % 7 == 0) || (i == daysInMonth[month])) {
if (((day + firstDayOfMon) % 7 == 0) || (day == daysInMonth[month])) {
// Ensure that the array element is padded with space characters
if (returnStringLen[row] < CALENDARWIDTH) {
returnString[row] += " ".repeat(CALENDARWIDTH - returnStringLen[row] + 1);
Expand All @@ -263,4 +335,34 @@ public static String[] getCalDays(int month, int year) {
return returnString;
}

/**
* ColorizeDay(): Return colorized day with the provided foreground
*
* @param day
* @param fg
* @return
*/
public static String ColorizeDay(int day, Ansi.Color fg) {
if (Output.queryColorEnabled() == true) {
return ansi().a(Attribute.INTENSITY_BOLD).fg(fg).a(String.format("%2d", day)).reset().toString();
} else {
return String.valueOf(day);
}
}

/**
* ColorizeDay(): Return colorized day with the provided foreground & background
*
* @param day
* @param fg
* @param bg
* @return
*/
public static String ColorizeDay(int day, Ansi.Color fg, Ansi.Color bg) {
if (Output.queryColorEnabled() == true) {
return ansi().a(Attribute.INTENSITY_BOLD).fg(fg).bg(bg).a(String.format("%2d", day)).reset().toString();
} else {
return String.valueOf(day);
}
}
}
10 changes: 9 additions & 1 deletion src/main/java/org/fross/cal/CommandLineArgs.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/******************************************************************************
* Cal - A command line calendar utility
*
* Copyright (c) 2019-2022 Michael Fross
* Copyright (c) 2019-2024 Michael Fross
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -58,6 +58,9 @@ public class CommandLineArgs {
@Parameter(names = { "-D", "--debug" }, description = "Turn on Debug mode to display extra program information")
protected boolean clDebug = false;

@Parameter(names = { "-d", "--display-holidays" }, description = "Display local country holidays in the calendar")
protected boolean clDisplayHolidays = false;

@Parameter(names = { "-n", "--num" }, description = "Number of calendar months to display per row")
protected int clNum = Calendar.DEFAULT_CALS_PER_ROW;

Expand Down Expand Up @@ -113,6 +116,11 @@ public static void ProcessCommandLine(String[] argv) {
Output.enableColor(false);
}

// Display local county holidays in the calendar
if (cli.clDisplayHolidays == true) {
Holidays.setDisplayHolidays(true);
}

// Show Help and Exit
if (cli.clHelp == true) {
Help.display();
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/org/fross/cal/Help.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/******************************************************************************
* Cal - A command line calendar utility
*
* Copyright (c) 2019-2022 Michael Fross
* Copyright (c) 2019-2024 Michael Fross
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -50,6 +50,7 @@ public static void display() {

Output.printColorln(Ansi.Color.YELLOW, "\nCommand Line Options:");
Output.printColorln(Ansi.Color.WHITE, " -n # Number of calendars per row in Year view. Default: " + Calendar.DEFAULT_CALS_PER_ROW);
Output.printColorln(Ansi.Color.WHITE, " -d Display local county holidays in the calendar");
Output.printColorln(Ansi.Color.WHITE, " -D Start in debug mode");
Output.printColorln(Ansi.Color.WHITE, " -v Display the program version and latest GitHub Cal release");
Output.printColorln(Ansi.Color.WHITE, " -z Disable colorized output");
Expand All @@ -63,11 +64,16 @@ public static void display() {

Output.printColorln(Ansi.Color.YELLOW, "\nExamples:");
Output.printColorln(Ansi.Color.WHITE, " java -jar cal.jar Display the current year");
Output.printColorln(Ansi.Color.WHITE, " java -jar cal.jar -n 4 Display the current year with 4 months per row");
Output.printColorln(Ansi.Color.WHITE, " java -jar cal.jar -n 4 -d Display the current year with 4 months per row with holidays");
Output.printColorln(Ansi.Color.WHITE, " java -jar cal.jar 9 Display September of the current year");
Output.printColorln(Ansi.Color.WHITE, " java -jar cal.jar 2022 Display the entire year 2022");
Output.printColorln(Ansi.Color.WHITE, " java -jar cal.jar 9 2022 Display only September of 2022");
Output.printColorln(Ansi.Color.WHITE, " java -jar cal.jar -D 6 Display June of current year in debug mode");
Output.printColorln(Ansi.Color.WHITE, " java -jar cal.jar -h Show this help information");

Output.printColorln(Ansi.Color.YELLOW, "\nNotes:");
Output.printColorln(Ansi.Color.WHITE, " For a list of supported countries for use with 'Display Holidays' option see:");
Output.printColorln(Ansi.Color.WHITE, " - https://date.nager.at/Country");

}
}
Loading

0 comments on commit ff2076e

Please sign in to comment.