From 370fbe971379d4f833158d41f2c95cb669fb6dbf Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Sat, 16 Sep 2023 13:58:52 -0500 Subject: Allow config file to load custom data files The parameters "custom_unit_file", "custom_dimension_file" and "custom_exception_file" can now be used to load custom unit, dimension and exception files. Specify them more than once to load multiple files. I haven't yet added this to the GUI, and I probably won't, because you already need to be able to edit text files to create this, so having a GUI won't make it any more intuitive. --- src/main/java/sevenUnitsGUI/Presenter.java | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) (limited to 'src/main/java/sevenUnitsGUI') diff --git a/src/main/java/sevenUnitsGUI/Presenter.java b/src/main/java/sevenUnitsGUI/Presenter.java index 3ecfba6..e471b01 100644 --- a/src/main/java/sevenUnitsGUI/Presenter.java +++ b/src/main/java/sevenUnitsGUI/Presenter.java @@ -21,7 +21,6 @@ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -73,16 +72,16 @@ public final class Presenter { if (System.getProperty("os.name").startsWith("Windows")) { final String envFolder = System.getenv("LOCALAPPDATA"); if (envFolder == null || "".equals(envFolder)) { - configDir = Paths.get(System.getenv("USERPROFILE"), "AppData", "Local", "SevenUnits"); + configDir = Path.of(System.getenv("USERPROFILE"), "AppData", "Local", "SevenUnits"); } else { - configDir = Paths.get(envFolder, "SevenUnits"); + configDir = Path.of(envFolder, "SevenUnits"); } } else { final String envFolder = System.getenv("XDG_CONFIG_HOME"); if (envFolder == null || "".equals(envFolder)) { - configDir = Paths.get(System.getenv("HOME"), ".config", "SevenUnits"); + configDir = Path.of(System.getenv("HOME"), ".config", "SevenUnits"); } else { - configDir = Paths.get(envFolder, "SevenUnits"); + configDir = Path.of(envFolder, "SevenUnits"); } } @@ -99,6 +98,13 @@ public final class Presenter { return Optional.of(configDir.resolve("config.txt")); } + /** Gets a Path from a pathname in the config file. */ + private static Path pathFromConfig(String pathname) { + return CONFIG_FILE.map(configFile -> + configFile.getParent().resolve(pathname) + ).orElse(Path.of(pathname)); + } + /** * Adds default units and dimensions to a database. * @@ -615,6 +621,17 @@ public final class Presenter { switch (param) { // set manually to avoid the unnecessary saving of the non-manual // methods + case "custom_dimension_file": + this.database.loadDimensionFile(pathFromConfig(value)); + break; + case "custom_exception_file": + Files.lines(pathFromConfig(value)) + .map(Presenter::withoutComments) + .forEach(this.metricExceptions::add); + break; + case "custom_unit_file": + this.database.loadUnitsFile(pathFromConfig(value)); + break; case "number_display_rule": this.numberDisplayRule = StandardDisplayRules .getStandardRule(value); -- cgit v1.2.3