Skip to content

Commit

Permalink
feat(cli): default to all courses if the input is blank when promptin…
Browse files Browse the repository at this point in the history
…g the user for the courses to sync

closes #7
  • Loading branch information
thetric committed Apr 5, 2017
1 parent 208502e commit fe3b061
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Course> showAndSaveCourseSelection(Collection<Course> allCourses) {
println ''
println ">>> ${resourceBundle.getString('sync.courses.available')}"
allCourses.eachWithIndex { Course course, int i ->
println "\t${i + 1} ${course.name} (ID: ${course.id})"
}
List<Integer> 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<Integer> 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 {
Expand Down
2 changes: 1 addition & 1 deletion ui/cli/src/main/resources/ilias-cli.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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}
2 changes: 1 addition & 1 deletion ui/cli/src/main/resources/ilias-cli_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fe3b061

Please sign in to comment.