diff options
| author | John Estabrook <jestabro@vyos.io> | 2023-11-16 13:45:43 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-11-16 13:45:43 -0600 |
| commit | d4f9d6ce726ea4d4fff6eecc16678d7a45a0b555 (patch) | |
| tree | bad8b2fbfd9e325d9e6338bb8abd6608a2537c04 /python/vyos/utils/io.py | |
| parent | c8ba5dccfa9b02533c6536903ecacd3ddb04351e (diff) | |
| parent | e036f783bc85e4d2bad5f5cbfd688a03a352223e (diff) | |
| download | veeos-1x-d4f9d6ce726ea4d4fff6eecc16678d7a45a0b555.tar.gz veeos-1x-d4f9d6ce726ea4d4fff6eecc16678d7a45a0b555.zip | |
Merge pull request #1768 from zdc/T4516-sagitta
image: T4516: Added system image tools
Diffstat (limited to 'python/vyos/utils/io.py')
| -rw-r--r-- | python/vyos/utils/io.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/python/vyos/utils/io.py b/python/vyos/utils/io.py index 8790cbaac..74099b502 100644 --- a/python/vyos/utils/io.py +++ b/python/vyos/utils/io.py @@ -13,6 +13,8 @@ # You should have received a copy of the GNU Lesser General Public # License along with this library. If not, see <http://www.gnu.org/licenses/>. +from typing import Callable + def print_error(str='', end='\n'): """ Print `str` to stderr, terminated with `end`. @@ -72,3 +74,26 @@ def is_dumb_terminal(): """Check if the current TTY is dumb, so that we can disable advanced terminal features.""" import os return os.getenv('TERM') in ['vt100', 'dumb'] + +def select_entry(l: list, list_msg: str = '', prompt_msg: str = '', + list_format: Callable = None,) -> str: + """Select an entry from a list + + Args: + l (list): a list of entries + list_msg (str): a message to print before listing the entries + prompt_msg (str): a message to print as prompt for selection + + Returns: + str: a selected entry + """ + en = list(enumerate(l, 1)) + print(list_msg) + for i, e in en: + if list_format: + print(f'\t{i}: {list_format(e)}') + else: + print(f'\t{i}: {e}') + select = ask_input(prompt_msg, numeric_only=True, + valid_responses=range(1, len(l)+1)) + return next(filter(lambda x: x[0] == select, en))[1] |
