Skip to content

Commit

Permalink
Add a setting for the date-format string.
Browse files Browse the repository at this point in the history
Fixes #4
  • Loading branch information
ensonic authored and kernicPanel committed Jul 18, 2022
1 parent 1045449 commit a4b52c4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,7 @@ An option is available to prepend the generated filename with the current date.
https://user-images.githubusercontent.com/720491/178076594-a29e99ac-e81d-426d-8bca-ff4e8bfc7013.mp4




Another option lets you customize the date format. The default template is
`yyyy-MM-DD_` which would expand to e.g. `2022-07-31_`.
All the possible patterns are docuemnted
[here](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html#patterns).
25 changes: 22 additions & 3 deletions src/main/java/com/kernicpanel/RandomizerExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.UnsupportedTemporalTypeException;
import java.util.Arrays;
import java.util.Locale;
import java.util.Random;
Expand All @@ -16,6 +17,8 @@ public class RandomizerExtension extends ControllerExtension {
Faker faker = new Faker();

private SettableBooleanValue useDate;
private SettableStringValue dateFormatTemplate;
private static final String DEFAULT_DATE_FORMAT_TEMPLATE = "yyyy-MM-dd_";

private ControllerHost host;
private DocumentState documentState;
Expand Down Expand Up @@ -51,6 +54,16 @@ public void init() {

useDate =
host.getPreferences().getBooleanSetting("Prepend date for filename", "Random name", true);
dateFormatTemplate =
host.getPreferences().getStringSetting("Format string for date prefix", "Random name", 15, DEFAULT_DATE_FORMAT_TEMPLATE);
dateFormatTemplate.addValueObserver(value -> {
try {
LocalDate.now().format(DateTimeFormatter.ofPattern(value));
} catch (IllegalArgumentException | UnsupportedTemporalTypeException e) {
dateFormatTemplate.set(DEFAULT_DATE_FORMAT_TEMPLATE);
host.showPopupNotification("Invalid date format template.");
}
});

documentState
.getSignalSetting("Select", "Randomize browser selection", "Select random item")
Expand Down Expand Up @@ -98,9 +111,15 @@ private NoArgsCallback randomName() {

if (useDate.get()) {
LocalDate dateObj = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String date = dateObj.format(formatter);
generatedString = date + "_" + generatedString;
String formattedDate;
try {
formattedDate = dateObj.format(DateTimeFormatter.ofPattern(dateFormatTemplate.get()));
} catch (IllegalArgumentException | UnsupportedTemporalTypeException e) {
// Should not happen, unless e.g. the config data got corrupted.
dateFormatTemplate.set(DEFAULT_DATE_FORMAT_TEMPLATE);
formattedDate = dateObj.format(DateTimeFormatter.ofPattern(dateFormatTemplate.get()));
}
generatedString = formattedDate + generatedString;
}

filenameOutput.set(generatedString.replace(" ", "_").toLowerCase(Locale.ROOT));
Expand Down

0 comments on commit a4b52c4

Please sign in to comment.