diff options
author | Lars-Dominik Braun <lars@6xq.net> | 2012-03-17 19:27:27 +0100 |
---|---|---|
committer | Lars-Dominik Braun <lars@6xq.net> | 2012-03-17 19:27:27 +0100 |
commit | 7a02354513acad9e315b4f4f736b77a08de363a6 (patch) | |
tree | 86042261e9c1bba2d773f4817a3c862ce12f09c9 /src/ui.c | |
parent | e6a87e15043767c0f1f00a62110e4bbb53de4ece (diff) | |
download | pianobar-7a02354513acad9e315b4f4f736b77a08de363a6.tar.gz pianobar-7a02354513acad9e315b4f4f736b77a08de363a6.tar.bz2 pianobar-7a02354513acad9e315b4f4f736b77a08de363a6.zip |
Auto-select last remaining item of filtered list
See #231.
Diffstat (limited to 'src/ui.c')
-rw-r--r-- | src/ui.c | 41 |
1 files changed, 26 insertions, 15 deletions
@@ -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); |