]> git.phdru.name Git - m_lib.git/blobdiff - m_lib/tty_menu.py
Fix raw_input for Py3 compatibility
[m_lib.git] / m_lib / tty_menu.py
index 35ba43d1ba49d5db64763f6c0105c4e4ecb238fd..06e756e157c78edf2883976da04629bc3576ccdb 100755 (executable)
@@ -1,13 +1,17 @@
 #! /usr/bin/env python
-"""tty menus
-
-   Written by Broytman, Mar 1998. Copyright (C) 1997 PhiloSoft Design.
-"""
+"""tty menus"""
 
 
+from __future__ import print_function
 import string
 
 
+try:
+   raw_input
+except NameError:  # Python 3
+   raw_input = input
+
+
 def hmenu(prompt, astring):
    """
       Writes prompt and read result
@@ -28,7 +32,7 @@ def vmenu(item_list, prompt, format = "%d. %s"):
       returns selected number. Returns -1, if user enter non-numeric string.
    """
    for i in range(len(item_list)):
-      print format % (i, item_list[i])
+      print(format % (i, item_list[i]))
    print
 
    result = raw_input(prompt)
@@ -43,14 +47,14 @@ def vmenu(item_list, prompt, format = "%d. %s"):
 
 def test():
    result = hmenu("Select: d)aily, w)eekly, m)onthly, c)ancel: ", "dwmc")
-   print "Answer is '%s'\n" % result
+   print("Answer is '%s'\n" % result)
 
    os_list = ["DOS", "Windows", "UNIX"]
    result = vmenu(os_list, "Select OS: ")
    if 0 <= result < len(os_list):
-      print "Answer is '%s'\n" % os_list[result]
+      print("Answer is '%s'\n" % os_list[result])
    else:
-      print "Wrong selection"
+      print("Wrong selection")
 
 if __name__ == "__main__":
    test()