From 7a02354513acad9e315b4f4f736b77a08de363a6 Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Sat, 17 Mar 2012 19:27:27 +0100 Subject: Auto-select last remaining item of filtered list See #231. --- src/ui.c | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) (limited to 'src/ui.c') diff --git a/src/ui.c b/src/ui.c index acba151..104ee04 100644 --- a/src/ui.c +++ b/src/ui.c @@ -330,14 +330,17 @@ static PianoStation_t **BarSortedStations (PianoStation_t *unsortedStations, /* let user pick one station * @param app handle + * @param stations that should be listed * @param prompt string * @param called if input was not a number + * @param auto-select if only one station remains after filtering * @return pointer to selected station or NULL */ PianoStation_t *BarUiSelectStation (BarApp_t *app, PianoStation_t *stations, - const char *prompt, BarUiSelectStationCallback_t callback) { + const char *prompt, BarUiSelectStationCallback_t callback, + bool autoselect) { PianoStation_t **sortedStations = NULL, *retStation = NULL; - size_t stationCount, i; + size_t stationCount, i, lastDisplayed, displayCount; char buf[100]; if (stations == NULL) { @@ -352,6 +355,7 @@ PianoStation_t *BarUiSelectStation (BarApp_t *app, PianoStation_t *stations, app->settings.sortOrder); do { + displayCount = 0; for (i = 0; i < stationCount; i++) { const PianoStation_t *currStation = sortedStations[i]; /* filter stations */ @@ -361,26 +365,33 @@ PianoStation_t *BarUiSelectStation (BarApp_t *app, PianoStation_t *stations, currStation->isQuickMix ? 'Q' : ' ', !currStation->isCreator ? 'S' : ' ', currStation->name); + ++displayCount; + lastDisplayed = i; } } BarUiMsg (&app->settings, MSG_QUESTION, prompt); - if (BarReadlineStr (buf, sizeof (buf), &app->input, - BAR_RL_DEFAULT) == 0) { - free (sortedStations); - return NULL; - } + if (autoselect && displayCount == 1 && stationCount != 1) { + /* auto-select last remaining station */ + BarUiMsg (&app->settings, MSG_NONE, "%i\n", lastDisplayed); + retStation = sortedStations[lastDisplayed]; + } else { + if (BarReadlineStr (buf, sizeof (buf), &app->input, + BAR_RL_DEFAULT) == 0) { + break; + } - if (isnumeric (buf)) { - unsigned long selected = strtoul (buf, NULL, 0); - if (selected < stationCount) { - retStation = sortedStations[selected]; + if (isnumeric (buf)) { + unsigned long selected = strtoul (buf, NULL, 0); + if (selected < stationCount) { + retStation = sortedStations[selected]; + } } - } - /* hand over buffer to external function if it was not a station number */ - if (retStation == NULL && callback != NULL) { - callback (app, buf); + /* hand over buffer to external function if it was not a station number */ + if (retStation == NULL && callback != NULL) { + callback (app, buf); + } } } while (retStation == NULL); -- cgit v1.2.3