Skip to content

Commit

Permalink
Added -c Clear Cache command line optino
Browse files Browse the repository at this point in the history
Updated date in LICENSE file
Added a holiday test, but couldn't really get it to work - it's commented out
Added the `-c` command to the README and the Help
  • Loading branch information
frossm committed Dec 26, 2023
1 parent 4a30dfa commit 600c486
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 21 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Option | Description
-n # | Set the number of calendars per row when displaying a year. The default is 3.
-D | Start in Debug Mode which will display additional debugging data. Normally not used.
-d | Display local county holidays in the calendar
-c | Clear the holiday cache on the local computer and exit. The next time they are needed they will be downloaded from the Internet
-v | Display the current version and copyright of the program and exit. `-v` will also query GitHub and display the latest release version.
-z | Disable colored output
-h or -? | Display the help page
Expand Down
2 changes: 1 addition & 1 deletion 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.7.0</version>
<version>2.7.1</version>
<packaging>jar</packaging>

<name>cal</name>
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.7.0'
version: '2.7.1'
summary: Command line calendar display
description: |
fCal is a command line calendar utility. It will display a
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/org/fross/cal/CommandLineArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

import java.util.ArrayList;
import java.util.List;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;

import org.fross.library.Debug;
import org.fross.library.GitHub;
Expand Down Expand Up @@ -61,6 +63,9 @@ public class CommandLineArgs {
@Parameter(names = { "-d", "--display-holidays" }, description = "Display local country holidays in the calendar")
protected boolean clDisplayHolidays = false;

@Parameter(names = { "-c", "--clear-cache" }, description = "Clear the holiday cache from the local computer")
protected boolean clClearCache = 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 @@ -121,6 +126,11 @@ public static void ProcessCommandLine(String[] argv) {
Holidays.setDisplayHolidays(true);
}

// Clear the holiday cache in the Java preferences system
if (cli.clClearCache == true) {
clearCache();
}

// Show Help and Exit
if (cli.clHelp == true) {
Help.display();
Expand Down Expand Up @@ -188,4 +198,20 @@ public static int queryMonthToUse() {
public static int queryYearToUse() {
return yearToUse;
}

/**
* clearCache(): Clear the holiday preferences cache
*
*/
public static void clearCache() {
Preferences prefHolidayCache = Preferences.userRoot().node("/org/fross/cal/holidays");
try {
prefHolidayCache.removeNode();
Output.printColorln(Ansi.Color.CYAN, "Clearing the local holiday cache");
System.exit(0);

} catch (BackingStoreException ex) {
Output.printColorln(Ansi.Color.RED, "ERROR: Could not clear the holiday cache");
}
}
}
1 change: 1 addition & 0 deletions src/main/java/org/fross/cal/Help.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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, " -c Clear the holiday cache on the local computer and exit");
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 Down
27 changes: 10 additions & 17 deletions src/main/java/org/fross/cal/Holidays.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
public class Holidays {
private static boolean holidaysAreEnabled = false;
private static Locale locale = Locale.getDefault();
private static TreeMap<String, String> countryMap = new TreeMap<>();
protected static TreeMap<String, String> countryMap = new TreeMap<>();
protected static TreeMap<String, String> holidays = new TreeMap<>();

/**
Expand All @@ -65,11 +65,10 @@ public static TreeMap<String, String> getHolidays(int year) {
// Debug
Output.debugPrintln("Locale Information: ");
Output.debugPrintln(" - ISO3 Code: " + locale.getISO3Country());
Output.debugPrintln(" - ISO2 Code: " + countryMap.get(locale.getISO3Country()));
Output.debugPrintln(" - ISO2 Code: " + queryISO2CountryCode());
Output.debugPrintln(" - Name: " + locale.getDisplayCountry());

// Build the URL and fetch the data
// https://date.nager.at/api/v3/publicholidays/2023/US
URL = URL + year + "/" + countryMap.get(locale.getISO3Country());

} catch (Exception Ex) {
Expand Down Expand Up @@ -266,21 +265,15 @@ public static boolean queryHolidaysEnabled() {
return holidaysAreEnabled;
}

public static int queryLengthOfLongestHolidayName() {
int longestHolidayLength = 0;
try {
for (String key : holidays.keySet()) {
if (holidays.get(key).length() > longestHolidayLength) {
longestHolidayLength = holidays.get(key).length();
}
}
} catch (Exception ex) {

}

return longestHolidayLength;
/**
* queryISO2CountryCode(): Return the 2 character local country code
*
* @return
*/
public static String queryISO2CountryCode() {
return countryMap.get(locale.getISO3Country());
}

/**
* buildCountryCodedMap(): Build a simple map with the ISO3 county code mapped to it's ISO2 value
*
Expand Down
36 changes: 35 additions & 1 deletion src/test/java/org/fross/cal/HolidaysTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,40 @@
import org.junit.jupiter.api.Test;

class HolidaysTest {
// // Testing clearing the local holiday cache
// @Test
// void clearHolidayCacheTest() {
// int year = org.fross.library.Date.getCurrentYear();
//
// // Firstly, ensure we have something in the cache
// Holidays.getHolidays(year);
//
// // Get the 2 character country code
// String ISO2 = Holidays.queryISO2CountryCode();
//
// // Make sure the cached items are present
// Preferences prefHolidayCache = Preferences.userRoot().node("/org/fross/cal/holidays/" + ISO2 + "/" + year);
//
// String[] cacheKeys = {};
// try {
// // Verify that we have something in the cache
// cacheKeys = prefHolidayCache.keys();
// assertTrue(cacheKeys.length > 0);
//
// // Clear the cache
// CommandLineArgs.clearCache();
//
// // Verify the cache is empty
// cacheKeys = prefHolidayCache.keys();
// assertFalse(cacheKeys.length > 0);
//
// } catch (BackingStoreException ex) {
// // Having issues getting to the holiday cache. Display an error and continue to getting them from the Internet
// Output.printColorln(Ansi.Color.RED, "Unable to access the holiday cache");
// fail();
// }
//
// }

// Test holiday list - US
@Test
Expand Down Expand Up @@ -75,7 +109,7 @@ void monthHolidayListTestUS() {
Output.println("Current locale set to: '" + Locale.getDefault().getDisplayCountry() + "' -- Skipping Test");
return;
}

StringBuilder sb = Holidays.queryHolidayListMonth(4, 2025);
assertEquals("2025-04-18 | Good Friday", sb.toString().trim());

Expand Down

0 comments on commit 600c486

Please sign in to comment.