Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import curses
- def main(stdscr):
- ################ Menu contents/lists
- LsMenuItemsStart = []
- LsMenuItemsOption2 = []
- ################
- IntLoopCount = 0
- IntSpaceBetweenMenuItems = 2
- IntCurrentSelection = 2 # Default item in menu to be selected, 1 (first thing in list) is title
- curses.curs_set(0) # Removes cursor
- BoolStartMenu = True
- IntLegalY, IntLegalX = stdscr.getmaxyx()
- class MenuItem:
- def __init__(self, StrContent, IntOrder, LsToUse,
- BoolIsPercent=False, IntPercentage=0,
- BoolToggle=False, BoolToggleValue=False):
- self.BoolToggle = BoolToggle
- self.StrContent = StrContent
- self.IntOrder = IntOrder
- self.LsToUse = LsToUse
- self.BoolIsPercent = BoolIsPercent
- self.IntPercentage = IntPercentage
- self.BoolToggleValue = BoolToggleValue
- self.StrSize = len(StrContent)
- if BoolIsPercent == False:
- LsToUse.insert(IntOrder, StrContent)
- if BoolIsPercent == True:
- IntPercentage = int(round(IntPercentage, -1)) # Should round IntPercentage to the closest 10
- self.StrSize = len(StrContent) # Stores size of string for later use if the percentage wants to be changed
- StrBar = "[" + ("#" * (int(IntPercentage / 10))) + ("-" * abs(int((IntPercentage / 10)) - 10)) + "]"
- StrContent = StrContent + " " + StrBar
- LsToUse.insert(IntOrder, StrContent)
- if BoolToggle == True:
- self.ChangeToggle(IntOrder, BoolToggleValue)
- def RemoveItem(self, StrContent):
- self.LsToUse.remove(self.StrContent)
- def ChangePercentage(self, IntIndex, IntPercentageChange):
- self.IntIndexInList = IntIndex
- self.IntPercentageToChangeTo = IntPercentageChange
- self.IntPercentage = IntPercentageChange
- StrInIndex = self.LsToUse.pop(IntIndex)
- IntPercentage = int(round(IntPercentageChange, -1)) # Should round IntPercentage to the closest 10
- if self.BoolIsPercent == False: # Checks just incase the string doesn't allready have a bar
- StrBar = "[" + ("#" * (int(IntPercentageChange / 10))) + ("-" * abs(int((IntPercentageChange / 10)) - 10)) + "]"
- StrNewString = StrInIndex + " " + StrBar
- self.LsToUse.insert(IntIndex, StrNewString)
- else:
- StrBar = "[" + ("#" * (int(IntPercentageChange / 10))) + ("-" * abs(int((IntPercentageChange / 10)) - 10)) + "]"
- StrNewString = StrInIndex[:self.StrSize] + " " + StrBar
- self.LsToUse.insert(IntIndex, StrNewString)
- def ChangeToggle(self, IntIndex, BoolToggleValue):
- StrInIndex = self.LsToUse.pop(IntIndex)
- if BoolToggleValue == True:
- StrNewString = StrInIndex[:self.StrSize] + " [#]"
- self.BoolToggleValue = True
- elif BoolToggleValue == False:
- StrNewString = StrInIndex[:self.StrSize] + " []"
- self.BoolToggleValue = False
- self.LsToUse.insert(IntIndex, StrNewString)
- stdscr.clear()
- ##### Start Menu
- Title = MenuItem("Script", 0, LsMenuItemsStart)
- Option1 = MenuItem("Option 1", 1, LsMenuItemsStart, True, 50)
- Option2 = MenuItem("Option 2", 2, LsMenuItemsStart)
- OptionToggle = MenuItem("Option Toggle", 3, LsMenuItemsStart, False, 0, True, True)
- OptionExit = MenuItem("Exit", 4, LsMenuItemsStart)
- # Otions 6-32 are just so I can test my program when more items then can fit on screen are presented
- Option6 = MenuItem("Option6", 5, LsMenuItemsStart)
- Option7 = MenuItem("Option7", 6, LsMenuItemsStart)
- Option8 = MenuItem("Option8", 7, LsMenuItemsStart)
- Option9 = MenuItem("Option9", 8, LsMenuItemsStart)
- Option10 = MenuItem("Option10", 9, LsMenuItemsStart)
- Option11 = MenuItem("Option11", 10, LsMenuItemsStart)
- Option12 = MenuItem("Option12", 11, LsMenuItemsStart)
- Option13 = MenuItem("Option13", 12, LsMenuItemsStart)
- Option14 = MenuItem("Option14", 13, LsMenuItemsStart)
- Option15 = MenuItem("Option15", 14, LsMenuItemsStart)
- Option16 = MenuItem("Option16", 15, LsMenuItemsStart)
- Option17 = MenuItem("Option17", 16, LsMenuItemsStart)
- Option18 = MenuItem("Option18", 17, LsMenuItemsStart)
- Option19 = MenuItem("Option19", 18, LsMenuItemsStart)
- Option20 = MenuItem("Option20", 19, LsMenuItemsStart)
- Option21 = MenuItem("Option21", 20, LsMenuItemsStart, True, 50)
- Option22 = MenuItem("Option22", 21, LsMenuItemsStart, False, 0, True, True)
- Option23 = MenuItem("Option23", 22, LsMenuItemsStart)
- Option24 = MenuItem("Option24", 23, LsMenuItemsStart)
- Option25 = MenuItem("Option25", 24, LsMenuItemsStart)
- Option26 = MenuItem("Option26", 25, LsMenuItemsStart)
- Option27 = MenuItem("Option27", 26, LsMenuItemsStart)
- Option28 = MenuItem("Option28", 27, LsMenuItemsStart)
- Option29 = MenuItem("Option29", 28, LsMenuItemsStart)
- Option30 = MenuItem("Option30", 29, LsMenuItemsStart)
- Option31 = MenuItem("Option31", 30, LsMenuItemsStart)
- Option32 = MenuItem("Option32", 31, LsMenuItemsStart)
- ##### Option 2 menu
- TitleOption2 = MenuItem("Script", 0, LsMenuItemsOption2)
- Option4 = MenuItem("Option 4", 1, LsMenuItemsOption2)
- Option5 = MenuItem("Opyion 5", 2, LsMenuItemsOption2)
- Back = MenuItem("Back", 3, LsMenuItemsOption2)
- def CreateMenu(LsMenuItems):
- stdscr.clear()
- IntLoopCount = 0
- IntNextItemY = 0
- IntCurrentPage = 1
- IntThisPageLoopCount = 0
- for Item in LsMenuItems: # Sets up start menu
- IntLoopCount = IntLoopCount + 1
- if IntNextItemY < IntLegalY - 1:
- if IntLoopCount == 1 and IntCurrentPage == 1: # Title
- stdscr.addstr(0, 0, Item, curses.A_BOLD)
- IntNextItemY = IntLoopCount + IntSpaceBetweenMenuItems + 1
- continue # Skip rest of current iteration
- if IntLoopCount == IntCurrentSelection:
- curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
- stdscr.addstr(IntNextItemY, 0, Item, curses.color_pair(1))
- else:
- stdscr.addstr(IntNextItemY, 0, Item)
- IntNextItemY = IntNextItemY + IntSpaceBetweenMenuItems
- else:
- stdscr.addstr(IntLegalY - 1, 0, "~")
- if IntLoopCount == IntCurrentSelection: # Goes to next page
- stdscr.clear()
- IntCurrentPage = IntCurrentPage + 1
- IntNextItemY = 0
- stdscr.addstr(IntNextItemY, 0, Item, curses.color_pair(1))
- IntNextItemY = IntNextItemY + IntSpaceBetweenMenuItems
- while True:
- ############################ Start Menu
- while BoolStartMenu == True:
- CreateMenu(LsMenuItemsStart)
- stdscr.refresh()
- IntKey = stdscr.getch() # Wait for user input
- if IntKey == ord("q"): # ord("letter") returns ASCII of letter
- exit()
- if IntKey == curses.KEY_UP and IntCurrentSelection > 2:
- IntCurrentSelection = IntCurrentSelection - 1
- elif IntKey == curses.KEY_DOWN and IntCurrentSelection < len(LsMenuItemsStart):
- IntCurrentSelection = IntCurrentSelection + 1
- elif IntKey == curses.KEY_LEFT:
- if IntCurrentSelection == 2 and Option1.IntPercentage is not 0 and Option1.BoolIsPercent == True:
- Option1.ChangePercentage(1, Option1.IntPercentage - 10)
- if IntCurrentSelection == 21 and Option21.IntPercentage is not 0 and Option21.BoolIsPercent == True:
- Option21.ChangePercentage(20, Option21.IntPercentage - 10)
- elif IntKey == curses.KEY_RIGHT:
- if IntCurrentSelection == 2 and Option1.IntPercentage is not 100 and Option1.BoolIsPercent == True:
- Option1.ChangePercentage(1, Option1.IntPercentage + 10)
- elif IntCurrentSelection == 21 and Option21.IntPercentage is not 100 and Option21.BoolIsPercent == True:
- Option21.ChangePercentage(20, Option21.IntPercentage + 10)
- elif IntKey == 32: # ASCII code 32 is space bar
- if IntCurrentSelection == 4 and OptionToggle.BoolToggleValue == True:
- OptionToggle.ChangeToggle(3, False)
- elif IntCurrentSelection == 4 and OptionToggle.BoolToggleValue == False:
- OptionToggle.ChangeToggle(3, True)
- if IntCurrentSelection == 22 and Option22.BoolToggleValue == True:
- Option22.ChangeToggle(21, False)
- elif IntCurrentSelection == 22 and Option22.BoolToggleValue == False:
- Option22.ChangeToggle(21, True)
- elif IntKey == 10: # ASCII code 10 is the enter key, curses.KEY_ENTER doesn't work for some reason
- ##### Most code for actually "doing stuff" depending on what item is pressed should go here
- if IntCurrentSelection == 5: # Adjust number depending on where the "exit" or "back" button is
- exit()
- if IntCurrentSelection == 3:
- BoolStartMenu = False
- BoolOption2Menu = True
- ############################ Option 2 menu
- while BoolOption2Menu == True:
- stdscr.clear()
- CreateMenu(LsMenuItemsOption2)
- stdscr.refresh()
- IntKey = stdscr.getch() # Wait for user input
- if IntKey == ord("q"): # ord("letter") returns ASCII of letter
- exit()
- if IntKey == curses.KEY_UP and IntCurrentSelection > 2:
- IntCurrentSelection = IntCurrentSelection - 1
- elif IntKey == curses.KEY_DOWN and IntCurrentSelection < len(LsMenuItemsOption2):
- IntCurrentSelection = IntCurrentSelection + 1
- elif IntKey == 10: # ASCII code 10 is the enter key, curses.KEY_ENTER doesn't work for some reason
- ##### Most code for actually "doing stuff" depending on what item is pressed should go here
- if IntCurrentSelection == 4: # Adjust number depending on where the "exit" or "back" button is
- BoolStartMenu = True
- BoolOption2Menu = False
- if IntCurrentSelection == 3:
- pass
- # Does initialisation described at https://docs.python.org/3/howto/curses.html and also handles reseting
- # the terminal if an error occurs
- curses.wrapper(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement