-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix/progress - custom terminal menu (#35)
- Loading branch information
1 parent
ae405b5
commit 3f596ed
Showing
7 changed files
with
66 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,67 @@ | ||
from simple_term_menu import TerminalMenu | ||
import time | ||
from colors import * | ||
from colorama import Fore, Back, Style, init, deinit | ||
|
||
# initialize colorama | ||
init() | ||
|
||
def show_menu(options): | ||
# settings for the single-choice menu | ||
terminal_menu = TerminalMenu( | ||
options, menu_cursor_style=("fg_green", "bold"), | ||
menu_highlight_style=("fg_green", "bold") | ||
) | ||
|
||
# show to menu and stores the user input to choice | ||
menu_entry_index = terminal_menu.show() | ||
choice = options[menu_entry_index] | ||
def show_menu(options, length=54): | ||
continue_action = True | ||
|
||
return choice | ||
# Get how many choices there are | ||
choice_number = len(options) | ||
|
||
# Print the box with the choices displayed | ||
print("╭" + int(length) * '─' + '╮') | ||
|
||
count = 0 | ||
while count < choice_number: | ||
number_of_spaces_remaining = (length - 2) - (2 + len(list(str(options[count])))) | ||
print( | ||
"│ " + COLOR_GREEN + COLOR_STYLE_BRIGHT + str(count) + | ||
'> ' + COLOR_RESET_ALL + str(options[count]) + int(number_of_spaces_remaining) * ' ' + '│' | ||
) | ||
|
||
count += 1 | ||
|
||
print("╰" + int(length) * '─' + '╯') | ||
|
||
while continue_action: | ||
error_happened = False | ||
# Get user's input and return the value | ||
# of the user's input. Also check if the | ||
# input is valid, if not, return warning | ||
get_input = input('$ ') | ||
try: | ||
input_type = str(type(int(get_input))) | ||
except Exception as error: | ||
input_type = '...' | ||
|
||
# Check if the input is an integer | ||
if input_type != "<class 'int'>": | ||
print(COLOR_YELLOW + f"Options '{get_input}' is not valid!" + COLOR_RESET_ALL) | ||
time.sleep(.5) | ||
error_happened = True | ||
|
||
# Check if the input is in the choices | ||
if not error_happened: | ||
if int(get_input) < 0 or int(get_input) > choice_number: | ||
print(COLOR_YELLOW + f"Options '{get_input}' is not valid!" + COLOR_RESET_ALL) | ||
time.sleep(.5) | ||
error_happened = True | ||
|
||
# Get the corresponding choice, depending | ||
# on the input number | ||
|
||
if not error_happened: | ||
user_input = options[int(get_input)] | ||
continue_action = False | ||
else: | ||
user_input = None | ||
|
||
return user_input | ||
|
||
|
||
# deinitialize colorama | ||
deinit() |