From fe3b061d5ee3de674b85f206516e3ce6fd798454 Mon Sep 17 00:00:00 2001 From: thetric Date: Wed, 5 Apr 2017 19:16:03 +0200 Subject: [PATCH] feat(cli): default to all courses if the input is blank when prompting the user for the courses to sync closes #7 --- .../cli/IliasCliController.groovy | 26 +++++++++++++------ .../src/main/resources/ilias-cli.properties | 2 +- .../main/resources/ilias-cli_de.properties | 2 +- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/ui/cli/src/main/groovy/com/github/thetric/iliasdownloader/cli/IliasCliController.groovy b/ui/cli/src/main/groovy/com/github/thetric/iliasdownloader/cli/IliasCliController.groovy index 8245f82..ade757e 100644 --- a/ui/cli/src/main/groovy/com/github/thetric/iliasdownloader/cli/IliasCliController.groovy +++ b/ui/cli/src/main/groovy/com/github/thetric/iliasdownloader/cli/IliasCliController.groovy @@ -106,21 +106,31 @@ final class IliasCliController { println ">>> ${resourceBundle.getString('sync.finished')}" } + /** + * Prompts the user to select the positions of the {@link Course}s to sync. If the input is empty (default) all + * courses are selected. Otherwise the space separated positions (1 based) are taken from the {@code allCourses} argument. + * @param allCourses {@link Course}s to select from + * @return the {@link Course}s to sync + */ private Collection showAndSaveCourseSelection(Collection allCourses) { println '' println ">>> ${resourceBundle.getString('sync.courses.available')}" allCourses.eachWithIndex { Course course, int i -> println "\t${i + 1} ${course.name} (ID: ${course.id})" } - List courseIndices = consoleService. - readLine('sync.courses', resourceBundle.getString('sync.courses.prompt')) - .split(/\s+/) - .collect { Integer.parseInt it } - .collect { it - 1 } - if (courseIndices.any { it < 0 || it > allCourses.size() }) { - throw new CourseSelectionOutOfRange() + def courseSelection = consoleService.readLine('sync.courses', resourceBundle.getString('sync.courses.prompt')) + final trimmedSelection = courseSelection.trim() + if (!trimmedSelection) { + return allCourses + } else { + List courseIndices = courseSelection.split(/\s+/) + .collect { Integer.parseInt it } + .collect { it - 1 } + if (courseIndices.any { it < 0 || it > allCourses.size() }) { + throw new CourseSelectionOutOfRange() + } + return courseIndices.collect { allCourses[it] } } - return courseIndices.collect { allCourses[it] } } private static final class CourseSelectionOutOfRange extends RuntimeException { diff --git a/ui/cli/src/main/resources/ilias-cli.properties b/ui/cli/src/main/resources/ilias-cli.properties index 99c2621..c8e17c3 100644 --- a/ui/cli/src/main/resources/ilias-cli.properties +++ b/ui/cli/src/main/resources/ilias-cli.properties @@ -12,5 +12,5 @@ login.error = Login failed sync.started = Sync started sync.finished = Sync finished sync.courses.available=Available courses: -sync.courses.prompt=Enter the numbers of the courses to sync (separate by a space) +sync.courses.prompt=Enter the numbers of the courses to sync (separate by a space) or leave blank to select all sync.courses.prompt.errors.out-of-range=Invalid course selection! The selection must contain indices between 1 and {0} diff --git a/ui/cli/src/main/resources/ilias-cli_de.properties b/ui/cli/src/main/resources/ilias-cli_de.properties index 0ea6490..02e72a2 100644 --- a/ui/cli/src/main/resources/ilias-cli_de.properties +++ b/ui/cli/src/main/resources/ilias-cli_de.properties @@ -13,5 +13,5 @@ sync.started = Sync gestartet sync.finished = Sync beendet sync.courses.available=Gefundene Kurse: -sync.courses.prompt=Geben Sie die Nummern der zu synchronisierenden Kurse an (durch ein Leerzeichen getrennt) +sync.courses.prompt=Geben Sie die Nummern der zu synchronisierenden Kurse an (durch ein Leerzeichen getrennt) oder drücken Sie Enter, um direkt alle Kurse auszuwählen sync.courses.prompt.errors.out-of-range=Die Auswahl muss Indexe zwischen 1 und {0} beinhalten