Johann Woelper 9 years ago
parent
commit
b263f7e00e
2 changed files with 112 additions and 28 deletions
  1. 66 25
      pshell/pshell
  2. 46 3
      pshell/ttytest.py

+ 66 - 25
pshell/pshell

@@ -5,7 +5,10 @@ import readline
 import subprocess
 from plumbum import local
 
-def vi():
+
+readline.parse_and_bind('tab:complete')
+
+def vi(*args):
     subprocess.call('vi')
 
 def ll(*args):
@@ -16,18 +19,17 @@ def cd(*args):
     # TODO: no param, go to home
     # param is - : go back
     print args
-    if len(args) == 0:
+    if len(args[0]) == 0:
         args = '/home/woelper'
     else:
         args = args[0]
     print 'cd to', args[0]
     local.cwd.chdir(args[0])
 
-def exec_if_builtin(string):
-    print 'STR', string
-    string = string.split()
-    command = string[0]
-    parameters = string[1:]
+def exec_if_builtin(strlist):
+    print 'STR', strlist
+    command = strlist[0]
+    parameters = strlist[1:]
     possibles = globals().copy()
     possibles.update(locals())
     method = possibles.get(command)
@@ -37,32 +39,71 @@ def exec_if_builtin(string):
     print 'Did not detect a function by that name'
 
 
-def as_plumbum(string):
+def as_plumbum(strlist):
     print 'Running cmd through plumbum'
     # TODO: until pipe or special char
     # split the string
-    string = string.split()
     #print string
-    command = string[0]
-    parameters = string[1:]
-    print command, parameters
-    print local[command][parameters]()
+    command = strlist[0]
+    parameters = strlist[1:]
+    #print command, parameters
+    try:
+        print local[command][parameters]()
+    except:
+        print 'Could not execute command'
 
-# line for line
-while True:
-    
-    
+def as_python(string):
+    print 'running native python code'
+    exec(string)
+    try:
+        exec(string)
+    except:
+        print 'error in command'
+
+def get_exec_method(string):
+    # native : assignments
+    # '=' is in first command or equals second
+    exec(input_string)
     """
+    strlist = string.split()
+    if '=' in strlist[0]:
+        exec(string)
+        return
+    else:
+        if len(strlist) > 1:
+            if '=' in strlist[1]:
+                exec(string)
+                return
     
-    input_string = raw_input(local.cwd + ' >> ')
-    
-    # was the string a regular line of python?
-    if exec_if_builtin(input_string) is not True:
+    if not exec_if_builtin(strlist):
+        exec(string)
         try:
-            exec(input_string)
+            exec(string)
         except:
+            print 'probably NOT native code.'
+    """
+
+def is_assignment(strlist):
+    if '=' in strlist[0]:
+        return True
+    if len(strlist) > 1:
+        if '=' in strlist[1]:
+            return True
+
+# line for line
+while True:
+    # we need to exec here so definitions stay global
+    input_string = raw_input(local.cwd + ' $ ')
+    
+    #if get_exec_method(input_string)
+    strlist = input_string.split()
+    if is_assignment(strlist):
+        exec(input_string)
+ 
+    else:        
+        if not exec_if_builtin(strlist):
             try:
-                as_plumbum(input_string)
+                exec(input_string)
             except:
-                print 'command not found:', input_string
-    """ 
+                print 'probably NOT native code.'
+                as_plumbum(strlist)

+ 46 - 3
pshell/ttytest.py

@@ -3,7 +3,36 @@ import select
 import tty
 import termios
 import os
+import readline
 
+
+
+addrs = ['a@a.com', 'digger@pansen.com']
+
+def mytool():
+    print 'MT'
+    
+
+
+
+def completer(text, state):
+    options = [x for x in addrs if x.startswith(text)]
+    try:
+        return options[state]
+    except IndexError:
+        return None
+        
+
+#readline.set_completer(completer)
+readline.parse_and_bind('tab:complete')
+
+
+while True:
+    a = raw_input('$ ')
+    print a
+
+
+"""
 #print "#> ",
 os.system("stty cbreak -echo")
 
@@ -11,14 +40,28 @@ cmdstring = ''
 while True: 
     #print '---'
     a = sys.stdin.read(1)
-    cmdstring = cmdstring + a
-    sys.stdout.write('> ' + cmdstring)
+    
+    #sys.stdout.write(a)
     sys.stdout.write('\r')
+    if a == '\n':
+        #print 'ENTER'
+        a = ''
+        cmdstring = ''
+        print 's'
+        sys.stdout.write('\r')
+    elif a == '\010':
+        print 'ENTER'
+        a = ''
+        sys.stdout.write('\r')
+    else:
+        cmdstring = cmdstring + a    
+    
+    sys.stdout.write('$ ' + cmdstring)
     sys.stdout.flush()
 
 os.system("stty -cbreak echo")
 
-
+"""
 
 """
 fd = sys.stdin.fileno()