Command line supports history
This commit is contained in:
parent
b6ec04cc9a
commit
f3bb2626ae
|
@ -12,6 +12,7 @@ from bt.message import *
|
|||
import cline.loop
|
||||
import cline.addch
|
||||
import cline.message
|
||||
import cline.history
|
||||
#import settings
|
||||
|
||||
|
||||
|
@ -25,6 +26,7 @@ class Plugin(plugin.Plugin):
|
|||
|
||||
def open(self):
|
||||
write("Starting CLI", True)
|
||||
cline.history.read()
|
||||
|
||||
# Register our run function and close function
|
||||
register("run", cline.loop.loop)
|
||||
|
@ -32,19 +34,28 @@ class Plugin(plugin.Plugin):
|
|||
|
||||
# Initialize a dictionary for key press inputs
|
||||
settings.set("keyinput", dict())
|
||||
settings.get("keyinput")[259] = cline.history.prev
|
||||
settings.get("keyinput")[258] = cline.history.next
|
||||
settings.get("keyinput")[127] = cline.addch.backspace
|
||||
settings.get("keyinput")[10] = cline.addch.enter
|
||||
|
||||
|
||||
def close(self):
|
||||
cline.loop.quit()
|
||||
cline.history.save()
|
||||
write("CLI has been stopped", True)
|
||||
settings.delete("keyinput")
|
||||
|
||||
|
||||
def run(self,args):
|
||||
if args==None or len(args)<2:
|
||||
if args==None or len(args)<1:
|
||||
write(self.usage)
|
||||
|
||||
if args[0]=="prompt":
|
||||
settings.set("prompt",args[1]+" ")
|
||||
settings.set("prompt",args[1]+" ")
|
||||
elif args[0]=="maxhist":
|
||||
settings.set("maxhist",int(args[1]))
|
||||
elif args[0]=="history":
|
||||
cline.history.show()
|
||||
else:
|
||||
write(self.usage)
|
|
@ -2,4 +2,4 @@ __author__="bjschuma"
|
|||
__date__ ="$Dec 19, 2009 9:13:05 PM$"
|
||||
|
||||
|
||||
__all__ = ["addch", "check", "loop", "message"]
|
||||
__all__ = ["addch", "check","history", "loop", "message"]
|
|
@ -24,6 +24,8 @@ def addch(input):
|
|||
keys = settings.get("keyinput")
|
||||
if (c in keys.keys()) == True:
|
||||
input = keys[c](input)
|
||||
#else:
|
||||
# write(c)
|
||||
|
||||
return input
|
||||
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
#! /usr/bin/python
|
||||
|
||||
# To change this template, choose Tools | Templates
|
||||
# and open the template in the editor.
|
||||
|
||||
__author__="bjschuma"
|
||||
__date__ ="$Jan 29, 2010 9:59:20 AM$"
|
||||
|
||||
import settings
|
||||
from bt.file import *
|
||||
|
||||
global hist
|
||||
global idx
|
||||
hist = []
|
||||
idx = 0
|
||||
|
||||
|
||||
def insert(input):
|
||||
global hist,idx
|
||||
|
||||
if len(hist)>0 and (hist[len(hist)-1] == input):
|
||||
idx = len(hist)
|
||||
return
|
||||
|
||||
hist += [input]
|
||||
if len(hist) > settings.get("maxhist"):
|
||||
hist.pop(0)
|
||||
idx = len(hist)
|
||||
|
||||
|
||||
def getfile(flags='r'):
|
||||
dir = settings.get("appdir")
|
||||
file = join(dir,"history")
|
||||
return fopen(file,flags)
|
||||
|
||||
|
||||
def save():
|
||||
global hist
|
||||
out = getfile('w')
|
||||
for line in hist:
|
||||
out.write(line+"\n")
|
||||
out.close()
|
||||
|
||||
|
||||
def read():
|
||||
global hist,idx
|
||||
fin = getfile()
|
||||
|
||||
if fin == None:
|
||||
return
|
||||
|
||||
for line in fin:
|
||||
hist+=[line.strip()]
|
||||
idx = len(hist)
|
||||
|
||||
|
||||
def show():
|
||||
write(hist)
|
||||
|
||||
|
||||
def prev(input):
|
||||
global hist,idx
|
||||
if idx > 0:
|
||||
idx -= 1
|
||||
return settings.get("prompt") + hist[idx]
|
||||
|
||||
|
||||
def next(input):
|
||||
global hist,idx
|
||||
|
||||
if idx < len(hist):
|
||||
idx += 1
|
||||
prompt = settings.get("prompt")
|
||||
|
||||
if idx == len(hist):
|
||||
return prompt
|
||||
else:
|
||||
return prompt + hist[idx]
|
|
@ -38,11 +38,13 @@ def init():
|
|||
settings.set("prompt", ">>> ")
|
||||
else:
|
||||
settings.set("prompt",settings.get("prompt")+" ")
|
||||
|
||||
if settings.has("maxhist") == False:
|
||||
settings.set("maxhist",50)
|
||||
|
||||
settings.set("clinex", 0)
|
||||
settings.set("cliney", 0)
|
||||
#settings.set("write", insert)
|
||||
register("write", insert)
|
||||
#settings.set("everyloop")
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -56,7 +56,7 @@ def disp(string, y=-1):
|
|||
|
||||
if y == -1:
|
||||
y = settings.get("cliney")
|
||||
space = " " * ( len(string)+1 )
|
||||
space = " " * settings.get("maxyx")[1]
|
||||
stdscr.addstr(y, 0, space)
|
||||
else:
|
||||
stdscr.leaveok(True)
|
||||
|
@ -64,7 +64,7 @@ def disp(string, y=-1):
|
|||
l = len(string)
|
||||
if l == 0:
|
||||
return
|
||||
space = " " * settings.get("maxyx")[0]
|
||||
space = " " * settings.get("maxyx")[1]
|
||||
stdscr.addstr(y, 0, space)
|
||||
|
||||
|
||||
|
|
|
@ -8,7 +8,8 @@ __date__ ="$Dec 19, 2009 11:38:46 PM$"
|
|||
|
||||
from manager import manager
|
||||
import settings
|
||||
from bt.message import write
|
||||
#from bt.message import write
|
||||
import history
|
||||
|
||||
|
||||
# Check for valid input
|
||||
|
@ -25,5 +26,7 @@ def run(input):
|
|||
else:
|
||||
manager.run(input)
|
||||
|
||||
history.insert(input)
|
||||
|
||||
# Account for the prompt changing when the manager runs
|
||||
return settings.get("prompt")
|
||||
|
|
Loading…
Reference in New Issue