Skip to content

Conversation

benzea
Copy link

@benzea benzea commented Jul 31, 2025

When showing a list of options, it is useful to have the default choice highlighted. So push the choices into variables and print them from function when the default choice is known. Then, print the default choice in bold.

I think this is may be a nice touch for the UI.

When showing a list of options, it is useful to have the default choice
highlighted. So push the choices into variables and print them from
function when the default choice is known. Then, print the default
choice in bold.

Signed-off-by: Benjamin Berg <[email protected]>
Copy link
Member

@michaelolbrich michaelolbrich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea, but the implementation is not quite correct.

}

print_choices() {
local i default choice
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

choice is unused.

fi
local i=0 def=0 choice
# shellcheck disable=SC2034
local -a choices choices_descs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you can declare local variable in the middle of a function inside a loop, they will not actually go out of scope at the end of the loop body.
So if the user gives an invalid response, all the choices are shown twice.

You need to explicitly initialize the variables, e.g. choices=()

while [ -z "${topic}" ]; do
local i=0 choice default
# shellcheck disable=SC2034
local -a choices choice_descs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants