Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # a python+ncurses DM script for LinuxBBQ Zhuxuegao
- import curses
- import sys
- import subprocess
- sys.stdout.write("\x1b]2;LinuxBBQ Zhuxuegao DM\x07") # set the terminal window title
- myscreen = curses.initscr()
- myscreen.box()
- myscreen.keypad(1)
- myscreen.refresh()
- maxheight,maxwidth = myscreen.getmaxyx()
- curses.start_color()
- curses.use_default_colors()
- curses.noecho()
- curses.cbreak()
- curses.curs_set(0)
- curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
- curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
- cursor_vpos = 1 # default to position 1
- current_select = 1 # default to selection 1
- numkeys = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}
- keylist = [ord(numkey) for numkey in numkeys] # create ascii-list
- # TITLE
- title = "LinuxBBQ Zhuxuegao: What do you want?"
- title_length = len(title)
- title_hpos = (maxwidth - title_length) / 2
- title_vpos = (maxheight - 13) / 2
- myscreen.addstr(title_vpos,title_hpos,title)
- # LIST OF SELECTIONS
- choosebox_sel = {
- 1: "dwm",
- 2: "scrotwm",
- 3: "i3",
- 4: "pensiwm",
- 5: "anuswm",
- 6: "Exit (tty)",
- 7: "Reboot",
- 8: "Shutdown",
- }
- # LIST OF COMMANDS TO EXECUTE FOR EACH SELECTION
- choosebox_exe = {
- 1: "echo 'dwm' >> zhuxuegao_dm_log.txt",
- 2: "echo 'scrotwm' >> zhuxuegao_dm_log.txt",
- 3: "echo 'i3' >> zhuxuegao_dm_log.txt",
- 4: "echo 'pensiwm' >> zhuxuegao_dm_log.txt",
- 5: "echo 'anuswm' >> zhuxuegao_dm_log.txt",
- 6: "echo 'exit (tty)' >> zhuxuegao_dm_log.txt",
- 7: "echo 'reboot' >> zhuxuegao_dm_log.txt",
- 8: "echo 'shutdown' >> zhuxuegao_dm_log.txt"
- }
- choosebox_hpos = (maxwidth - 30) / 2
- choosebox_vpos = (maxheight - 9) / 2
- choosebox = curses.newwin(11,30,choosebox_vpos,choosebox_hpos)
- choosebox_ref = { # reference for relationship between selection and vpos
- 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 7, 7: 8, 8: 9}
- # function for drawing the choosebox
- def draw_choosebox():
- for i in range(1,9):
- choosebox.addstr(choosebox_ref[i],1,str(i) + ". " + str(choosebox_sel[i]))
- choosebox.box()
- choosebox.refresh()
- draw_choosebox()
- choosebox.chgat(cursor_vpos,1,28,curses.A_REVERSE)
- choosebox.refresh()
- # function for navigating using arrowkeys (up/down)
- def arrow_navigate(vpos,select,key):
- if key == 'down':
- if select < 8:
- select = select + 1
- vpos = choosebox_ref[select]
- else:
- select = 1
- vpos = 1
- elif key == 'up':
- if select > 1:
- select = select - 1
- vpos = choosebox_ref[select]
- else:
- select = 9
- vpos = 11
- choosebox.clrtoeol() # erase the content, then redraw it
- draw_choosebox()
- choosebox.chgat(vpos,1,28,curses.A_REVERSE)
- choosebox.refresh()
- return vpos,select
- # function for navigating using numberkeys (1-9)
- def number_navigate(vpos,select,key):
- select = key
- vpos = choosebox_ref[select]
- choosebox.clrtoeol()
- draw_choosebox()
- choosebox.chgat(vpos,1,28,curses.A_REVERSE)
- choosebox.refresh()
- return vpos,select
- # function for making a selection
- def make_selection(select):
- subprocess.Popen(choosebox_exe[select], shell=True, stdout=subprocess.PIPE)
- # CAPTURE KEYPRESSES
- while True:
- key = myscreen.getch()
- if key == ord('q'): break # 'q' = quit
- elif key == curses.KEY_UP:
- cursor_vpos,current_select = arrow_navigate(cursor_vpos,current_select,'up')
- elif key == curses.KEY_DOWN:
- cursor_vpos,current_select = arrow_navigate(cursor_vpos,current_select,'down')
- elif key in keylist:
- cursor_vpos,current_select = number_navigate(cursor_vpos,current_select,int(chr(key)))
- elif key == ord('\n'): # ENTER
- make_selection(current_select)
- #break
- # _TODO: make it resizable.
- curses.endwin()
Advertisement
Add Comment
Please, Sign In to add comment