Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- #-------------------------------------------------------------------------------
- #This program is free software: you can redistribute it and/or modify
- #it under the terms of the GNU General Public License as published by
- #the Free Software Foundation, either version 3 of the License, or
- #(at your option) any later version.
- #
- #This program is distributed in the hope that it will be useful,
- #but WITHOUT ANY WARRANTY; without even the implied warranty of
- #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- #GNU General Public License for more details.
- #
- #You should have received a copy of the GNU General Public License
- #along with this program. If not, see <http://www.gnu.org/licenses/>.
- #-------------------------------------------------------------------------------
- #
- # Python Source Code was orignally written by
- #
- # -*- coding: utf-8 -*-
- # Topmenu and the submenus are based of the example found at this location http://blog.skeltonnetworks.com/2010/03/python-curses-custom-menu/
- # The rest of the work was done by Matthew Bennett and he requests you keep these two mentions when you reuse the code :-)
- # Basic code refactoring by Andrew Scheller
- #
- ########################################################################################################################
- # I decided to make GNU/SMIT menu using this code which has been modified to make it similar to SMIT but not entirely a clone of it.
- # This script will link with shell scripts as if you seen on SCO UNIX's sco admin menu
- #
- #
- # The shell scripts that connects to this menu are done by Vincent <ezarchproject@gmail.com>
- #
- #
- ##########################################################################################################3
- from time import sleep
- import curses, os #curses is the interface for capturing key presses on the menu, os launches the files
- screen = curses.initscr() #initializes a new window for capturing key presses
- curses.noecho() # Disables automatic echoing of key presses (prevents program from input each key twice)
- curses.cbreak() # Disables line buffering (runs each key as it is pressed rather than waiting for the return key to pressed)
- curses.start_color() # Lets you use colors when highlighting selected menu option
- screen.keypad(1) # Capture input from keypad
- # Change this to use different colors when highlighting
- curses.init_pair(1,curses.COLOR_BLACK, curses.COLOR_WHITE) # Sets up color pair #1, it does black text with white background
- h = curses.color_pair(1) #h is the coloring for a highlighted menu option
- n = curses.A_NORMAL #n is the coloring for a non highlighted menu option
- MENU = "menu"
- COMMAND = "command"
- EXITMENU = "exitmenu"
- menu_data = {
- 'title': "Systems Administration", 'type': MENU, 'subtitle': "Please move the cursor to the desired item and press Enter",
- 'options':[
- { 'title': "C - Process Viewer", 'type': COMMAND, 'command': 'top' },
- { 'title': "M - Filesystem Management", 'type':COMMAND,'command':'filesystem/fsmenu.py'},
- { 'title': "M - User Management", 'type': COMMAND, 'command': 'users/usermenu.py' },
- { 'title': "M - Init Manager", 'type': MENU, 'subtitle': "Select INIT type for your system",
- 'options': [
- {'title': "M - Systemd (Common on GNU/Linux Distros)", 'type': COMMAND,'command':'systemd/sysdmenu.py' },
- {'title': "M - OpenRC (GNU/Linux that does not use systemd)", 'type': COMMAND, 'command': 'openrc/openrcmenu.py' },
- {'title': "M - System V Init (Slackware and Old GNU/Linux)", 'type': COMMAND, 'command': 'sysvinit/sysvinitmenu.py' }
- ]
- },
- { 'title': "C - Shell", 'type':COMMAND,'command':'/bin/bash'},
- { 'title': "I - About GNU/SMIT", 'type':COMMAND,'command':'cat doc/Gsmit.txt | more'},
- ]
- }
- # This function displays the appropriate menu and returns the option selected
- def runmenu(menu, parent):
- # work out what text to display as the last menu option
- if parent is None:
- lastoption = "Exit"
- else:
- lastoption = "[(..)]" % parent
- optioncount = len(menu['options']) # how many options in this menu
- pos=0 #pos is the zero-based index of the hightlighted menu option. Every time runmenu is called, position returns to 0, when runmenu ends the position is returned and tells the program what opt$
- oldpos=None # used to prevent the screen being redrawn every time
- x = None #control for while loop, let's you scroll through options until return key is pressed then returns pos to program
- # Loop until return key is pressed
- while x !=ord('\n'):
- if pos != oldpos:
- oldpos = pos
- screen.border(0)
- screen.addstr(1,30, menu['title'], curses.A_BOLD) # Title for this menu
- screen.addstr(3,2, menu['subtitle'], curses.A_BOLD) #Subtitle for this menu
- # Display all the menu items, showing the 'pos' item highlighted
- for index in range(optioncount):
- textstyle = n
- if pos==index:
- textstyle = h
- #screen.addstr(5+index,4, "%d - %s" % (index+1, menu['options'][index]['title']), textstyle)
- screen.addstr(5+index,4, "%s" % (menu['options'][index]['title']), textstyle)
- # Now display Exit/Return at bottom of menu
- textstyle = n
- if pos==optioncount:
- textstyle = h
- #screen.addstr(5+optioncount,4, "%d - %s" % (optioncount+1, lastoption), textstyle)
- screen.addstr(6+optioncount,4, "%s" % (lastoption), textstyle)
- #Added by Vincent
- screen.addstr(20,2, "M = Menu/Submenu C = Command I = Information ", curses.A_NORMAL)
- screen.addstr(22,2, "Enter=Do", curses.A_BOLD)
- screen.refresh()
- # finished updating screen
- x = screen.getch() # Gets user input
- # What is user input?
- if x >= ord('1') and x <= ord(str(optioncount+1)):
- pos = x - ord('0') - 1 # convert keypress back to a number, then subtract 1 to get index
- elif x == 258: # down arrow
- if pos < optioncount:
- pos += 1
- else: pos = 0
- elif x == 259: # up arrow
- if pos > 0:
- pos += -1
- else: pos = optioncount
- # return index of the selected item
- return pos
- # This function calls showmenu and then acts on the selected item
- def processmenu(menu, parent=None):
- optioncount = len(menu['options'])
- exitmenu = False
- while not exitmenu: #Loop until the user exits the menu
- getin = runmenu(menu, parent)
- if getin == optioncount:
- exitmenu = True
- elif menu['options'][getin]['type'] == COMMAND:
- curses.def_prog_mode() # save curent curses environment
- os.system('reset')
- screen.clear() #clears previous screen
- os.system(menu['options'][getin]['command']) # run the command
- screen.clear() #clears previous screen on key press and updates display based on pos
- curses.reset_prog_mode() # reset to 'current' curses environment
- curses.curs_set(1) # reset doesn't do this right
- curses.curs_set(0)
- elif menu['options'][getin]['type'] == MENU:
- screen.clear() #clears previous screen on key press and updates display based on pos
- processmenu(menu['options'][getin], menu) # display the submenu
- screen.clear() #clears previous screen on key press and updates display based on pos
- elif menu['options'][getin]['type'] == EXITMENU:
- exitmenu = True
- # Main program
- processmenu(menu_data)
- curses.endwin() #VITAL! This closes out the menu system and returns you to the bash prompt.
- os.system('clear')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement