Actually encode to the terminals locale, if we can.
[curses-crossword.git] / curses-crossword.py
index 5554f3036fd7b68b7b6e101fd3f27ab968965028..6d8791a64cfeda7e96a2fcfc145c74270431c898 100644 (file)
@@ -1,8 +1,12 @@
 #!/usr/bin/python
 
 import curses
+import curses.ascii
 import locale
 import codecs
+import getopt
+import os
+import sys
 
 locale.setlocale(locale.LC_ALL, '')
 code = locale.getpreferredencoding()
@@ -26,7 +30,30 @@ except:
     for number in range(0,10):
         superscript_numbers[str(number)] = str(number)
 
-crossworddata = codecs.open("g2-20090701.txt", "r", "utf-8").read()
+filename = None
+
+try:
+    (options, args) = getopt.getopt(sys.argv[1:], "f:", "file=")
+except getopt.GetoptError, err:
+    print str(err)
+    sys.exit(2)
+
+for option in options:
+    if option[0] == "-f" or option[0] == "--file":
+        filename = option[1]
+
+if not filename and len(args) > 0:
+    filename = args[0]
+
+if not filename:
+    sys.stderr.write("No crossword file specified, exiting.\n")
+    sys.exit(0)
+
+if os.path.exists(filename) and os.path.isfile(filename):
+    crossworddata = codecs.open(filename, "r", "utf-8").read()
+else:
+    sys.stderr.write("Couldn't open file %s\n" %(filename))
+    sys.exit(0)
 
 def parsecrossword(crossworddata):
     ingrid = False
@@ -57,13 +84,13 @@ def parsecrossword(crossworddata):
                     parts = line.split()
                     question_number = int(parts[0])
                     clue = " ".join(parts[1:])
-                    crossword["across"][int(question_number)] = clue
+                    crossword["across"][int(question_number)] = clue.encode(code)
             if indown:
                 if line != "":
                     parts = line.split()
                     question_number = int(parts[0])
                     clue = " ".join(parts[1:])
-                    crossword["down"][int(question_number)] = clue
+                    crossword["down"][int(question_number)] = clue.encode(code)
     num_cols = len(crossword["grid"][0])
     num_rows = len(crossword["grid"])
 
@@ -156,10 +183,91 @@ def crossword(stdscr, crossworddata):
         curx += 4
     curx -= 1
     stdscr.addch(cury, curx, curses.ACS_LRCORNER)
+    # draw the clues in
+    cury = (len(crossword["grid"]) * 2) + 1
+    curx = 0
+    stdscr.addstr(cury, curx, "Across")
+    cury += 1
+    for cluenumber in crossword["across"].keys():
+        stdscr.addstr(cury, curx, "%3s: %s" %(str(cluenumber), crossword["across"][cluenumber]))
+        cury += 1
+
+    cury += 1
+    stdscr.addstr(cury, curx, "Down")
+    cury += 1
+    for cluenumber in crossword["down"].keys():
+        stdscr.addstr(cury, curx, "%3s: %s" %(str(cluenumber), crossword["down"][cluenumber]))
+        cury += 1
+
+    curx = 3
+    cury = 1
+    gridx = 0
+    gridy = 0
+
+    while crossword["grid"][gridy][gridx] == "x":
+        curx += 4
+        gridx += 1
+
+    stdscr.move(cury,curx)
+
     while 1:
         c = stdscr.getch()
-        if c == ord('q'):
+        if c == curses.ascii.ESC:
             break
+        if c == curses.KEY_RIGHT:
+            if gridx < (len(crossword["grid"][0]) - 1):
+                gridx += 1
+                curx += 4
+                while gridx < (len(crossword["grid"][0]) -1) \
+                    and crossword["grid"][gridy][gridx] == "x":
+                    gridx += 1
+                    curx += 4
+                while crossword["grid"][gridy][gridx] == "x":
+                    gridx -= 1
+                    curx -= 4
+                stdscr.move(cury, curx)
+        if c == curses.KEY_LEFT:
+            if gridx > 0:
+                curx -= 4
+                gridx -= 1
+                while gridx > 0 \
+                    and crossword["grid"][gridy][gridx] == "x":
+                        gridx -= 1
+                        curx -= 4
+                while crossword["grid"][gridy][gridx] == "x":
+                    gridx += 1
+                    curx += 4
+                stdscr.move(cury, curx)
+        if c == curses.KEY_UP:
+            if gridy > 0:
+                gridy -= 1
+                cury -= 2
+                while gridy > 0 \
+                    and crossword["grid"][gridy][gridx] == "x":
+                        gridy -= 1
+                        cury -= 2
+                while crossword["grid"][gridy][gridx] == "x":
+                    gridy += 1
+                    cury += 2
+                stdscr.move(cury, curx)
+        if c == curses.KEY_DOWN:
+            if gridy < (len(crossword["grid"]) - 1):
+                gridy += 1
+                cury += 2
+                while gridy < (len(crossword["grid"]) - 1) \
+                    and crossword["grid"][gridy][gridx] == "x":
+                    gridy += 1
+                    cury += 2
+                while crossword["grid"][gridy][gridx] == "x":
+                    gridy -= 1
+                    cury -= 2
+                stdscr.move(cury, curx)
+        if curses.ascii.isalpha(c) or c == ord(" "):
+            stdscr.addch(cury, curx, c)
+            stdscr.move(cury, curx)
+        if c == curses.KEY_BACKSPACE or c == curses.KEY_DC:
+            stdscr.addch(cury, curx, ord(" "))
+            stdscr.move(cury, curx)
+
 
 curses.wrapper(crossword, crossworddata)
-print parsecrossword(crossworddata)