rhowaldt

zhuxuegao_dm.py

Sep 20th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.57 KB | None | 0 0
  1. #!/usr/bin/python
  2. # a python+ncurses DM script for LinuxBBQ Zhuxuegao
  3.  
  4. import curses
  5. import sys
  6. import subprocess
  7. sys.stdout.write("\x1b]2;LinuxBBQ Zhuxuegao DM\x07") # set the terminal window title
  8.  
  9. myscreen = curses.initscr()
  10. myscreen.box()
  11. myscreen.keypad(1)
  12. myscreen.refresh()
  13.  
  14. maxheight,maxwidth = myscreen.getmaxyx()
  15.  
  16. curses.start_color()
  17. curses.use_default_colors()
  18. curses.noecho()
  19. curses.cbreak()
  20. curses.curs_set(0)
  21. curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_WHITE)
  22. curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLACK)
  23.  
  24. cursor_vpos = 1 # default to position 1
  25. current_select = 1 # default to selection 1
  26. numkeys = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}
  27. keylist = [ord(numkey) for numkey in numkeys] # create ascii-list
  28.  
  29. # TITLE
  30. title = "LinuxBBQ Zhuxuegao: What do you want?"
  31. title_length = len(title)
  32. title_hpos = (maxwidth - title_length) / 2
  33. title_vpos = (maxheight - 13) / 2
  34. myscreen.addstr(title_vpos,title_hpos,title)
  35.  
  36. # LIST OF SELECTIONS
  37. choosebox_sel = {
  38. 1: "dwm",
  39. 2: "scrotwm",
  40. 3: "i3",
  41. 4: "pensiwm",
  42. 5: "anuswm",
  43. 6: "Exit (tty)",
  44. 7: "Reboot",
  45. 8: "Shutdown",
  46. }
  47.  
  48. # LIST OF COMMANDS TO EXECUTE FOR EACH SELECTION
  49. choosebox_exe = {
  50. 1: "echo 'dwm' >> zhuxuegao_dm_log.txt",
  51. 2: "echo 'scrotwm' >> zhuxuegao_dm_log.txt",
  52. 3: "echo 'i3' >> zhuxuegao_dm_log.txt",
  53. 4: "echo 'pensiwm' >> zhuxuegao_dm_log.txt",
  54. 5: "echo 'anuswm' >> zhuxuegao_dm_log.txt",
  55. 6: "echo 'exit (tty)' >> zhuxuegao_dm_log.txt",
  56. 7: "echo 'reboot' >> zhuxuegao_dm_log.txt",
  57. 8: "echo 'shutdown' >> zhuxuegao_dm_log.txt"
  58. }
  59.  
  60. choosebox_hpos = (maxwidth - 30) / 2
  61. choosebox_vpos = (maxheight - 9) / 2
  62. choosebox = curses.newwin(11,30,choosebox_vpos,choosebox_hpos)
  63. choosebox_ref = { # reference for relationship between selection and vpos
  64. 1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 7, 7: 8, 8: 9}
  65.  
  66. # function for drawing the choosebox
  67. def draw_choosebox():
  68.     for i in range(1,9):
  69.         choosebox.addstr(choosebox_ref[i],1,str(i) + ". " + str(choosebox_sel[i]))
  70.  
  71.     choosebox.box()
  72.     choosebox.refresh()
  73.  
  74. draw_choosebox()
  75. choosebox.chgat(cursor_vpos,1,28,curses.A_REVERSE)
  76. choosebox.refresh()
  77.  
  78. # function for navigating using arrowkeys (up/down)
  79. def arrow_navigate(vpos,select,key):
  80.     if key == 'down':
  81.         if select < 8:
  82.             select = select + 1
  83.             vpos = choosebox_ref[select]
  84.         else:
  85.             select = 1
  86.             vpos = 1
  87.     elif key == 'up':
  88.         if select > 1:
  89.             select = select - 1
  90.             vpos = choosebox_ref[select]
  91.         else:
  92.             select = 9
  93.             vpos = 11
  94.  
  95.     choosebox.clrtoeol() # erase the content, then redraw it
  96.     draw_choosebox()
  97.     choosebox.chgat(vpos,1,28,curses.A_REVERSE)
  98.     choosebox.refresh()
  99.     return vpos,select
  100.  
  101. # function for navigating using numberkeys (1-9)
  102. def number_navigate(vpos,select,key):
  103.     select = key
  104.     vpos = choosebox_ref[select]
  105.     choosebox.clrtoeol()
  106.     draw_choosebox()
  107.     choosebox.chgat(vpos,1,28,curses.A_REVERSE)
  108.     choosebox.refresh()
  109.     return vpos,select
  110.  
  111. # function for making a selection
  112. def make_selection(select):
  113.     subprocess.Popen(choosebox_exe[select], shell=True, stdout=subprocess.PIPE)
  114.    
  115. # CAPTURE KEYPRESSES
  116. while True:
  117.     key = myscreen.getch()
  118.     if key == ord('q'): break # 'q' = quit
  119.     elif key == curses.KEY_UP:
  120.         cursor_vpos,current_select = arrow_navigate(cursor_vpos,current_select,'up')
  121.     elif key == curses.KEY_DOWN:
  122.         cursor_vpos,current_select = arrow_navigate(cursor_vpos,current_select,'down')
  123.     elif key in keylist:
  124.         cursor_vpos,current_select = number_navigate(cursor_vpos,current_select,int(chr(key)))
  125.     elif key == ord('\n'): # ENTER
  126.         make_selection(current_select)
  127.         #break
  128.  
  129. # _TODO: make it resizable.
  130. curses.endwin()
Advertisement
Add Comment
Please, Sign In to add comment