PLUTO
menu Namespace Reference

Classes

class  gb
 The class gb contains global variables and pointers to ease up accessibility in coding the functions of this module. More...
 

Functions

def SetTitle
 
def Print
 Print a message on the screen. More...
 
def Prompt (message)
 Prompt a message, wait for any key to be pressed. More...
 
def ShowMenu
 Show menu using entries (1st column) and default (2nd column). More...
 
def UpDown (entries, default, inc)
 Allow the cursor to move up and down in the list. More...
 
def LeftRight (entries, default, options, inc)
 Allow left/right keys to switch options in the second column and change default values. More...
 
def Browse
 Browse a menu with entries (1st column) and default (2nd column, optional) Note: with Python > 2.5 we had some troubles initializing curses more than once. More...
 
def Insert (entries, default)
 Similar to Browse, but allow the user to directly input the default values by a reading a string. More...
 
def RestoreScreen ()
 Restore screen back to shell functionality. More...
 
def CursesIsActive ()
 Return 1 if curses have been activated. More...
 
def Print_no_curses (message, sleep, row)
 The next set of functions replicate the previous ones without using curses library. More...
 
def Prompt_no_curses (message)
 
def Browse_no_curses (entries, default, options)
 
def Insert_no_curses (entries, names)
 

Variables

int have_curses = 1
 
tuple q = raw_input()
 
tuple i = entries.index(x)
 
string opt_list = ''
 
tuple c = raw_input(">> choice ["+default[q]+"] ? ")
 
tuple newname = raw_input(">> new name ? ")
 

Function Documentation

def menu.Browse (   entries,
  default = [],
  options = [] 
)

Browse a menu with entries (1st column) and default (2nd column, optional) Note: with Python > 2.5 we had some troubles initializing curses more than once.

For this reason we prefer to initialize curses only at the beginning.

Definition at line 181 of file menu.py.

181 def Browse(entries, default=[], options=[]):
182 
183  gb.ncol = 1
184  if (len(default) > 0): gb.ncol = 2
185 
186  for x in sys.argv: # avoid curses library with the --no-curses option.
187  if (x == "--no-curses"):
188  return Browse_no_curses(entries, default, options)
189 
190 #
191 # window setup will be done just once.
192 #
193  if (gb.init == 0):
194  gb.scrn = curses.initscr()
195  curses.noecho()
196  curses.cbreak()
197  gb.scrn.keypad(1)
198  gb.init = 1
199  ShowMenu(entries, default)
200  while True:
201  # get user command
202  c = gb.scrn.getch()
203  try: cc = chr(c)
204  except: cc = 0
205 
206  if (c == 10):
207 # RestoreScreen()
208  return entries[gb.row-gb.rbeg]
209  elif (cc == 'q'):
210  RestoreScreen()
211 # curses.reset_shell_mode()
212  sys.exit()
213  elif (cc == 'u' or c == curses.KEY_UP): UpDown(entries, default, -1)
214  elif (cc == 'd' or c == curses.KEY_DOWN): UpDown(entries, default, 1)
215  elif (gb.ncol > 1):
216  if (cc == 'r' or c == curses.KEY_RIGHT):
217  LeftRight(entries, default, options, 1)
218  elif (cc == 'l' or c == curses.KEY_LEFT):
219  LeftRight(entries, default, options, -1)
220 
def RestoreScreen()
Restore screen back to shell functionality.
Definition: menu.py:287
def ShowMenu
Show menu using entries (1st column) and default (2nd column).
Definition: menu.py:98
def Browse_no_curses(entries, default, options)
Definition: menu.py:342
def LeftRight(entries, default, options, inc)
Allow left/right keys to switch options in the second column and change default values.
Definition: menu.py:155
def UpDown(entries, default, inc)
Allow the cursor to move up and down in the list.
Definition: menu.py:130
def Browse
Browse a menu with entries (1st column) and default (2nd column, optional) Note: with Python > 2...
Definition: menu.py:181

Here is the call graph for this function:

Here is the caller graph for this function:

def menu.Browse_no_curses (   entries,
  default,
  options 
)

Definition at line 342 of file menu.py.

342 def Browse_no_curses(entries, default, options):
343 #
344 #
def Browse_no_curses(entries, default, options)
Definition: menu.py:342

Here is the caller graph for this function:

def menu.CursesIsActive ( )

Return 1 if curses have been activated.

Definition at line 312 of file menu.py.

313 
314  return gb.init
315 
def CursesIsActive()
Return 1 if curses have been activated.
Definition: menu.py:312

Here is the caller graph for this function:

def menu.Insert (   entries,
  default 
)

Similar to Browse, but allow the user to directly input the default values by a reading a string.

Definition at line 227 of file menu.py.

227 def Insert(entries, default):
228 
229  gb.ncol = 2
230 
231  for x in sys.argv: # avoid curses library with the --no-curses option.
232  if (x == "--no-curses"):
233  return Insert_no_curses(entries, default)
234 
235  #
236  # window setup will be done just once.
237  #
238 
239  if (gb.init == 0):
240  gb.scrn = curses.initscr()
241  curses.noecho()
242  curses.cbreak()
243  gb.scrn.keypad(1)
244  gb.init = 1
245 
246 # entries = []
247 # for n in range(num): entries.append(repr(n))
248 
249 # RestoreScreen()
250 # print default
251 # sys.exit()
252 
253  ShowMenu(entries, default)
254  while True:
255  c = gb.scrn.getch() # get user command
256  try: cc = chr(c)
257  except: cc = 0
258 
259  if (c == 10):
260  return
261  elif (cc == 'q'):
262  RestoreScreen()
263  sys.exit()
264  elif (cc == 'u' or c == curses.KEY_UP): UpDown(entries, default, -1)
265  elif (cc == 'd' or c == curses.KEY_DOWN): UpDown(entries, default, 1)
266  elif (cc == 'r' or c == curses.KEY_RIGHT):
267  curses.echo()
268  gb.scrn.addstr(gb.row,gb.cbeg+gb.csep,' ')
269  gb.scrn.addstr(gb.row,gb.cbeg+gb.csep,'NAME or VALUE > ',curses.A_UNDERLINE)
270  new_name = gb.scrn.getstr()
271  i = gb.row-gb.rbeg
272  default.pop(i)
273  default.insert(i,new_name)
274  curses.noecho()
275  gb.scrn.clrtoeol()
276  ShowMenu(entries,default, gb.row)
277 
278 
def RestoreScreen()
Restore screen back to shell functionality.
Definition: menu.py:287
def ShowMenu
Show menu using entries (1st column) and default (2nd column).
Definition: menu.py:98
def Insert_no_curses(entries, names)
Definition: menu.py:385
def UpDown(entries, default, inc)
Allow the cursor to move up and down in the list.
Definition: menu.py:130
def Insert(entries, default)
Similar to Browse, but allow the user to directly input the default values by a reading a string...
Definition: menu.py:227

Here is the call graph for this function:

Here is the caller graph for this function:

def menu.Insert_no_curses (   entries,
  names 
)

Definition at line 385 of file menu.py.

385 def Insert_no_curses(entries, names):
386 #
387 #
def Insert_no_curses(entries, names)
Definition: menu.py:385

Here is the caller graph for this function:

def menu.LeftRight (   entries,
  default,
  options,
  inc 
)

Allow left/right keys to switch options in the second column and change default values.

Definition at line 155 of file menu.py.

155 def LeftRight(entries, default, options, inc):
156 
157  i = gb.row - gb.rbeg
158  idef = options[i].index(default[i])
159  nopt = len(options[i])
160  if (inc > 0): idef = idef + 1
161  if (inc < 0): idef = idef - 1
162  if (idef < 0): idef = nopt-1
163  if (idef == nopt): idef = 0
164 
165  default[i] = options[i][idef]
166  gb.scrn.addstr(gb.row, gb.cbeg , entries[i], curses.A_REVERSE)
167  gb.scrn.addstr(gb.row, gb.cbeg + gb.csep, default[i], curses.A_UNDERLINE)
168  gb.scrn.clrtoeol()
169  gb.scrn.refresh()
170 
def LeftRight(entries, default, options, inc)
Allow left/right keys to switch options in the second column and change default values.
Definition: menu.py:155

Here is the caller graph for this function:

def menu.Print (   message,
  sleep = 0.7,
  row = 1 
)

Print a message on the screen.

Definition at line 60 of file menu.py.

60 def Print (message, sleep=0.7,row=1):
61 
62  for x in sys.argv: # avoid curses library with the --no-curses option.
63  if (x == "--no-curses" or gb.init == 0):
64  Print_no_curses(message,sleep,row)
65  return
66 
67  if (gb.scrn == None): return # need this when using test_pluto.py script
68  if (row == 1): gb.scrn.erase()
69  gb.scrn.addstr(row,1,message, curses.A_BOLD)
70  gb.scrn.refresh()
71  time.sleep(sleep)
72 
def Print_no_curses(message, sleep, row)
The next set of functions replicate the previous ones without using curses library.
Definition: menu.py:324
def Print
Print a message on the screen.
Definition: menu.py:60

Here is the call graph for this function:

Here is the caller graph for this function:

def menu.Print_no_curses (   message,
  sleep,
  row 
)

The next set of functions replicate the previous ones without using curses library.

They are intended to provide a simpler way select options through a terminal-based replacement.

Definition at line 324 of file menu.py.

324 def Print_no_curses(message, sleep, row):
325 
326  global xglb
327 # if (row == 1): os.system("clear")
328  print message
329  time.sleep(sleep)
330 
def Print_no_curses(message, sleep, row)
The next set of functions replicate the previous ones without using curses library.
Definition: menu.py:324

Here is the caller graph for this function:

def menu.Prompt (   message)

Prompt a message, wait for any key to be pressed.

Definition at line 78 of file menu.py.

78 def Prompt (message):
79 
80  for x in sys.argv: # avoid curses library with the --no-curses option.
81  if (x == "--no-curses"):
82  Prompt_no_curses(message)
83  return
84 
85  gb.scrn.erase()
86  gb.scrn.addstr(1,1,message, curses.A_BOLD)
87  gb.scrn.refresh()
88  c = gb.scrn.getch()
89 
def Prompt_no_curses(message)
Definition: menu.py:332
def Prompt(message)
Prompt a message, wait for any key to be pressed.
Definition: menu.py:78

Here is the call graph for this function:

Here is the caller graph for this function:

def menu.Prompt_no_curses (   message)

Definition at line 332 of file menu.py.

332 def Prompt_no_curses (message):
333 #
334 #
def Prompt_no_curses(message)
Definition: menu.py:332

Here is the caller graph for this function:

def menu.RestoreScreen ( )

Restore screen back to shell functionality.

Note that RestoreScreen should be followed by sys.exit() in order to avoid troubleshooting observed with Python > 2.5

Definition at line 287 of file menu.py.

288 
289  for x in sys.argv: # avoid curses library with the --no-curses option.
290  if (x == "--no-curses"):
291  return
292 
293  curses.reset_shell_mode()
294  curses.nocbreak()
295  gb.scrn.keypad(0)
296  curses.echo()
297  curses.endwin()
298 
def RestoreScreen()
Restore screen back to shell functionality.
Definition: menu.py:287

Here is the caller graph for this function:

def menu.SetTitle (   title,
  subtitle = '' 
)

Definition at line 50 of file menu.py.

50 def SetTitle (title, subtitle = ''):
51 
52  gb.title = title
53  gb.subtitle = subtitle
54 
def SetTitle
Definition: menu.py:50

Here is the caller graph for this function:

def menu.ShowMenu (   entries,
  default,
  row = 0 
)

Show menu using entries (1st column) and default (2nd column).

row is an optional argument giving the line to be highlighted (default is first line)

Definition at line 98 of file menu.py.

98 def ShowMenu(entries, default, row=0):
99  # display title
100 
101  gb.scrn.clear()
102  gb.scrn.addstr(0,0,">> "+gb.title+" <<", curses.A_BOLD)
103  gb.rbeg = 3
104  if (len(gb.subtitle) > 1):
105  gb.scrn.addstr(2,0, gb.subtitle, curses.A_UNDERLINE)
106  gb.rbeg = 6
107 
108  lastrow = gb.rbeg
109  for ln in entries[0:len(entries)]:
110  indx = entries.index(ln)
111  gb.scrn.addstr(lastrow, gb.cbeg, ln)
112  if (gb.ncol == 2):
113  gb.scrn.addstr(lastrow, gb.cbeg + gb.csep, default[indx])
114  lastrow += 1
115 
116  if (row == 0 or gb.row < gb.rbeg or gb.row > lastrow):
117  gb.row = gb.rbeg # initial position
118 
119  n = gb.row - gb.rbeg
120  gb.scrn.addstr(gb.row, gb.cbeg , entries[n], curses.A_REVERSE)
121  if (gb.ncol == 2): gb.scrn.addstr(gb.row, gb.cbeg+gb.csep, default[n], curses.A_UNDERLINE)
122  gb.scrn.refresh()
123 
124 
def ShowMenu
Show menu using entries (1st column) and default (2nd column).
Definition: menu.py:98

Here is the caller graph for this function:

def menu.UpDown (   entries,
  default,
  inc 
)

Allow the cursor to move up and down in the list.

Definition at line 130 of file menu.py.

130 def UpDown(entries, default, inc):
131  tmp = gb.row + inc
132 
133  # ignore attempts to go off the edge of menu
134 
135  if tmp >= gb.rbeg and tmp < (gb.rbeg + len(entries)):
136  # unhighlight the current line by rewriting it in default attributes
137  gb.scrn.addstr(gb.row, gb.cbeg , entries[gb.row-gb.rbeg])
138  if (gb.ncol == 2): gb.scrn.addstr(gb.row, gb.cbeg + gb.csep, default[gb.row-gb.rbeg])
139  # highlight the previous/next line
140  gb.row = tmp
141  c1 = entries[gb.row-gb.rbeg]
142  if (gb.ncol == 2): c2 = default[gb.row-gb.rbeg]
143 
144  gb.scrn.addstr(gb.row, gb.cbeg , c1, curses.A_REVERSE)
145  if (gb.ncol == 2): gb.scrn.addstr(gb.row, gb.cbeg + gb.csep, c2, curses.A_UNDERLINE)
146  gb.scrn.refresh()
147 
148 
def UpDown(entries, default, inc)
Allow the cursor to move up and down in the list.
Definition: menu.py:130

Here is the caller graph for this function:

Variable Documentation

tuple menu.c = raw_input(">> choice ["+default[q]+"] ? ")

Definition at line 375 of file menu.py.

int menu.have_curses = 1

Definition at line 16 of file menu.py.

tuple menu.i = entries.index(x)

Definition at line 352 of file menu.py.

tuple menu.newname = raw_input(">> new name ? ")

Definition at line 408 of file menu.py.

string menu.opt_list = ''

Definition at line 369 of file menu.py.

tuple menu.q = raw_input()

Definition at line 339 of file menu.py.