Skip to content
Browse files

added menu and menu player.

going to introduce add action now
  • Loading branch information...
1 parent fc33be5 commit 7619d9f2ce83dee4142f7994a468a422c4f6ba81 Alexander Bernauer committed
Showing with 115 additions and 47 deletions.
  1. +68 −0 player.py
  2. +47 −47 voctrain.py
View
68 player.py
@@ -0,0 +1,68 @@
+import sys
+from getch import getch
+
+class Option(object):
+ __slots__=['text', 'key', 'action']
+
+ def __init__(self, text, key, action):
+ self.text = text
+ assert key.islower() or key.isdigit(), "keys must be lower case or digit"
+ self.key = key
+ self.action = action
+
+class Menu(object):
+ def __init__(self, header, options=(), quit=True, delim=', ', footer=": ", default=None):
+ self.header = header
+ self.default = None
+ self.delim = delim
+ self.footer = footer
+ self.options = []
+ self.keys = {}
+ for text, key, action in options:
+ self.addOption(Option(text, key, action))
+ if quit:
+ self.addQuitOption()
+ if default:
+ self.setDefault(default)
+
+ def setDefault(self, key):
+ assert not self.default, "default flag already set for differnt option"
+ self.default = key
+
+ def addOption(self, option):
+ assert not self.keys.has_key(option.key), "key already exists for different option"
+ self.keys[option.key] = option
+ self.options.append(option)
+
+ def addQuitOption(self):
+ self.addOption(Option("quit", "q", lambda : True))
+
+def play(menuFactory):
+ while True:
+ menu = menuFactory()
+ sys.stdout.write(menu.header + "\n")
+ choices = []
+ for option in menu.options:
+ if option.key == menu.default:
+ pkey = option.key.upper()
+ else:
+ pkey = option.key
+
+ if option.key in option.text:
+ choices.append(option.text.replace(option.key, "(%s)" % pkey, 1))
+ else:
+ choices.append("%s(%s)" % (option.text, pkey))
+ sys.stdout.write(menu.delim.join(choices) + menu.footer)
+ sys.stdout.flush()
+ choice = getch()
+ if choice == '\r' and menu.default:
+ action = menu.keys[menu.default].action
+ else:
+ try:
+ action = menu.keys[choice.lower()].action
+ except KeyError:
+ sys.stdout.write("invalid choice\n")
+ continue
+ quit = action()
+ if quit:
+ break
View
94 voctrain.py
@@ -4,9 +4,11 @@
import subprocess
import sys
import random
+from functools import partial
from getch import getch
from grep import grep
from config import Config
+from player import *
def getDictPath():
return os.path.join(os.environ['HOME'], '.voctrain')
@@ -44,7 +46,7 @@ def display(level, word):
sys.stdout.write("-" * 80 + "\n")
def setLevel(level, word, newLevel):
- assert (newLevel > Config.minLevel and newLevel < Config.maxLevel)
+ assert (newLevel > Config.minLevel and newLevel < Config.maxLevel), "new level is %d" % newLevel
file = getFile(level, word)
newFile = getFile(newLevel, word)
@@ -77,38 +79,33 @@ def train(level):
sys.stdout.write(word)
sys.stdin.readline()
display(level, word)
-
- while True:
- sys.stdout.write("correct? (y)es/(N)o/(q)uit/(e)dit) ")
- choice = getch()
- if choice == '\r' or choice == 'n' or choice == 'N':
- if level != Config.minLevel:
- setLevel(level, word, level-1)
- break
- elif choice == 'y' or choice == 'Y':
- if level != Config.maxLevel:
- setLevel(level, word, level+1)
- break
- elif choice == 'q' or choice == 'Q':
- return
- elif choice == 'e' or choice == 'E':
- edit(level, word)
-
-def selectLevel():
- while True:
- sys.stdout.write("dictionary overview:\n")
- for i in range(Config.minLevel, Config.maxLevel+1):
- sys.stdout.write("level %2d: %3d words\n" % (i, count(i)))
-
- sys.stdout.write("select level (q to quit): ")
- choice = sys.stdin.readline().strip()
- if choice == 'q' or choice == 'Q':
- return
- try:
- level = int(choice)
- train(level)
- except ValueError, e:
- pass
+
+ train.quitFlag = False
+ def quit():
+ train.quitFlag = True
+ return True
+
+ def correct():
+ if level != Config.maxLevel:
+ setLevel(level, word, level+1)
+ return 1
+
+ def incorrect():
+ if level != Config.minLevel:
+ setLevel(level, word, level-1)
+ return 1
+
+ def menu():
+ return Menu("correct?", (
+ ("yes", "y", correct),
+ ("no", "n", incorrect),
+ ("edit", "e", partial(edit, level, word)),
+ ("quit", "q", quit),
+ ), quit=False, default='n')
+
+ play(menu)
+ if train.quitFlag:
+ break
def add():
sys.stdout.write("enter new word: ")
@@ -134,17 +131,20 @@ def add():
f.close()
edit(Config.minLevel, word)
-def menu():
- sys.stdout.write("Welcome to voctrain!\n")
- while True:
- sys.stdout.write("What do you want to do? (s)elect Level, (a)dd a word, (q)uit: ")
- sys.stdout.flush()
- choice = getch()
- if choice == 's' or choice == 'S':
- selectLevel()
- elif choice == 'a' or choice == 'A':
- add()
- elif choice == 'q' or choice == 'Q':
- return
-
-menu()
+
+def selectLevelMenu():
+ menu = Menu("select level", quit=False, delim='\n', footer='\n> ')
+ for i in range(Config.minLevel, Config.maxLevel+1):
+ menu.addOption(Option("level %d [%d words]" % (i, count(i)), str(i), partial(train, i)))
+ menu.addQuitOption()
+ return menu
+
+def mainMenu():
+ return Menu("main menu", (
+ ("select level", "s", partial(play, selectLevelMenu)),
+ ("add word", "a", add),
+ ))
+
+sys.stdout.write("Welcome to voctrain!\n")
+play(mainMenu)
+sys.stdout.write("Good bye.\n")

0 comments on commit 7619d9f

Please sign in to comment.
Something went wrong with that request. Please try again.