diff --git a/README.md b/README.md index 0c0922a..e5035f1 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/src/main/java/com/kernicpanel/RandomizerExtension.java b/src/main/java/com/kernicpanel/RandomizerExtension.java index 1af1861..cd3a2a5 100644 --- a/src/main/java/com/kernicpanel/RandomizerExtension.java +++ b/src/main/java/com/kernicpanel/RandomizerExtension.java @@ -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; @@ -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; @@ -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") @@ -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));