Commits

Ronny Pfannschmidt  committed f9778f9

parts of the input fsm, will continue later

  • Participants
  • Parent commits 01196f7

Comments (0)

Files changed (2)

File tests/test_input.py

+
+from vimish.input import InputMachine, normal
+import re
+
+def split_input(input):
+    return re.findall('<[\w-]+?>|[\w$]', input)
+
+class CheckInputMachine(InputMachine):
+    def input(self, input):
+        for press in split_input(input):
+            result = self.keypress(press)
+            if result is not None:
+                yield result
+
+def test_split_input():
+    data = split_input("d$yVldiw20gg$<Ctrl-w><Ctrl-W>")
+    assert data[:2] == ['d', '$']
+    assert data[2:5] == ['y', 'V', 'l']
+    assert data[5:12] == ['d', 'i', 'w', '2', '0', 'g', 'g']
+    assert data[12] == '$'
+    assert data[-2:] == ['<Ctrl-w>', '<Ctrl-W>']
+
+    assert split_input("a a") == ['a', 'a']
+
+
+
+def test_state():
+    machine = InputMachine(normal)
+    
+
+

File vimish/input.py

+
+
+class Match(object):
+    pass
+
+
+
+class numbered(object):
+    def __init__(self, *commands, **kw):
+        self.commands = commands,
+        self.args = kw
+
+    def match(self,input):
+        for cmd, tree in self.commands:
+            if cmd == input:
+                #XXX: aliases
+                return Match(next_tree=tree)
+
+        return Match(reset=any(
+                        cmd.startswith(input)
+                        for cmd, _ in self.commands))
+
+
+
+def alias(x):
+    return x
+remove = copy = alias
+word = full_word = eol = line = alias
+select = alias
+
+normal = numbered(
+    ('d', numbered(
+        ('d', alias('Vl')),
+        ('w', select(word)),
+        ('iw', select(full_word)),
+        ('$', select(eol)),
+        ('Vl', select(line)),
+        call=remove)), 
+
+    ('y', numbered(
+        ('y', alias('Vl')),
+        ('w', select(word)),
+        ('iw', select(full_word)),
+        ('$', select(eol)),
+        ('Vl', select(line)),
+        call=copy)),
+
+
+)
+
+
+
+class InputMachine(object):
+    def __init__(self, tree):
+        self.tree = tree
+        self.current = tree
+        self.input = ""
+
+
+    def keypress(self, press):
+        #XXX: this is ugly painfull shit
+        if press == '<Esc>':
+            self.current = self.tree
+            self.input = ""
+        else:
+            self.input += press
+
+        match = self.current.match(''.join(self.input))
+        if match.next_tree is not None:
+            self.current = match.next_tree
+        elif match.reset:
+            self.current = self.tree
+            self.input.clear()
+        elif match.final:
+            return match
+
+