> This is a GUI version of the two-key input method I prototyped last
> summer, with an eight-way pie menu.

Here's a three-key that appears to have
been a response to one of your earlier
input method prototypes.  I've tried to
tune it for my keyboard's autorepeat,
but my alphabetical order skills aren't
good enough for it to be that useful.

-Dave

#!/usr/bin/python
#
# eviscerate Kragen's four-key input method, and instead try
# a linear search method with three keys:
#
#  ',': search backward
#  '.': search forward
#  ' ': accept
#
# shuttling rate increases with multiple searches in the same
# direction, and decreases when the direction changes
#
import os, sys

def chomp(line):
    if line.endswith('\n'): return chomp(line[:-1])
    return line

def read_wordlist(filename):
    infile = open(filename)
    words = []
    for line in infile.xreadlines():
        word = chomp(line)
        words.append(word)
    infile.close()
    return words

def bound(val, low, high):
    i = int(val)
    if i < low:         i = low
    if i >= high:       i = high
    return i

class cursor:
    def __init__(self, wordlist):
        self.wordlist = wordlist
        self.max   = len(self.wordlist)
        self.scale = int(self.max/100)
        self.pos   = int(self.max/2)
        self.dir   = 0
        self.tick  = 0
    def options(self):
        return '[%s]' % self.get()
    def get(self):
        return self.wordlist[self.pos]
    def accel(self, how):
        if self.dir == how:
                self.tick = self.tick + 1
                self.scale = bound(self.base * (1.02 ** self.tick),
                                1, int(self.max/10))
        else:
                self.scale = bound(self.scale / 2,
                                1, int(self.max/10))
                self.dir = how
                self.base = self.scale
                self.tick = 0
        self.pos = bound(self.pos + how * self.scale, 0, self.max-1)

def main():
    print "reading wordlist..."
    wordlist = read_wordlist('/usr/dict/words')
    os.system('stty cbreak -echo')
    backspace, delete = '\b', '\177'
    try:
        while 1:
            c = cursor(wordlist)
            msg = ''
            while 1: 
                msg += c.options()
                sys.stdout.write(msg)
                char = sys.stdin.read(1)
                sys.stdout.write((backspace + ' ' + backspace) * len(msg))
                msg = ''
                if char == ' ':
                    break
                elif char == ',':
                    c.accel(-1)
                elif char == '.':
                    c.accel(+1)
                else:
                    msg = "unknown char '" + char + "' "
            sys.stdout.write(c.get() + ' ')
    finally:
        sys.stdout.write("exiting...")
        sys.stdout.flush()
        os.system('stty -cbreak echo')

if __name__ == '__main__': main()

Reply via email to