Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mathew-fleisch
Last active July 2, 2021 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mathew-fleisch/383c156353324ec3910def665178df0a to your computer and use it in GitHub Desktop.
Save mathew-fleisch/383c156353324ec3910def665178df0a to your computer and use it in GitHub Desktop.
#!/bin/python3
input = [
["price", "display price of item"],
["owner", "show owner of item"],
["update", "sync cache with database"],
["delete", "delete item"],
["disassociate", "remove item from owner"]
]
# This function should display a menu of key/value pairs
# in a justified format, for viewing in a terminal/console
# Example Input:
# input = [
# ["price", "display price of item"],
# ["owner", "show owner of item"],
# ["update", "sync cache with database"],
# ["delete", "delete item"],
# ["disassociate", "remove item from owner"]
# ]
# Example Output:
# ###########################################
# # price - display price of item #
# # owner - show owner of item #
# # update - sync cache with database #
# # delete - delete item #
# # disassociate - remove item from owner #
# ###########################################
def display_input(input):
# Loop through input
leftBoarder = "# "
rightBoarder = " #"
divider = " - "
keyColumnMax = 0
valueColumnMax = 0
columnsMax = 0
for item in input:
tKey = item[0]
tValue = item[1]
if len(tKey) > keyColumnMax:
keyColumnMax = len(tKey)
if len(tValue) > valueColumnMax:
valueColumnMax = len(tValue)
columnsMax = len(leftBoarder) + keyColumnMax + len(divider) + valueColumnMax + len(rightBoarder)
print("#"*columnsMax)
for item in input:
tKey = item[0]
tValue = item[1]
print(leftBoarder + tKey.rjust(keyColumnMax) + divider + tValue.ljust(valueColumnMax) + rightBoarder)
print("#"*columnsMax)
display_input(input)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment