path completion
This commit is contained in:
parent
1a816166b6
commit
99ad0407b8
@ -1,6 +1,10 @@
|
|||||||
from kivy.uix.label import Label
|
from kivy.uix.label import Label
|
||||||
from kivy.core.window import Window
|
from kivy.core.window import Window
|
||||||
from kivy.graphics import Color,Rectangle
|
from kivy.graphics import Color,Rectangle
|
||||||
|
import glob
|
||||||
|
import os.path
|
||||||
|
|
||||||
|
from tools import common_substr
|
||||||
|
|
||||||
class Command_prompt(Label):
|
class Command_prompt(Label):
|
||||||
|
|
||||||
@ -47,15 +51,18 @@ class Command_prompt(Label):
|
|||||||
|
|
||||||
|
|
||||||
def on_key_down(self, keyboard, keycode, text, modifiers):
|
def on_key_down(self, keyboard, keycode, text, modifiers):
|
||||||
print(keycode,text,modifiers)
|
#print(keycode,text,modifiers)
|
||||||
if self.insert:
|
if self.insert:
|
||||||
# process modifiers
|
# process modifiers
|
||||||
mods=self.process_modifiers(modifiers)
|
mods=self.process_modifiers(modifiers)
|
||||||
|
|
||||||
|
# clear status bar
|
||||||
|
self.app.status_bar.text=""
|
||||||
|
|
||||||
# process command
|
# process command
|
||||||
if keycode[1]=="enter":
|
if keycode[1]=="enter":
|
||||||
self.insert=False
|
self.insert=False
|
||||||
self.parse_command()
|
self.parse_argv()
|
||||||
self.set_text("")
|
self.set_text("")
|
||||||
|
|
||||||
# move
|
# move
|
||||||
@ -76,6 +83,10 @@ class Command_prompt(Label):
|
|||||||
elif mods==['c'] and text=="k":
|
elif mods==['c'] and text=="k":
|
||||||
self.delete_all_forward()
|
self.delete_all_forward()
|
||||||
|
|
||||||
|
# tab completion
|
||||||
|
elif keycode[1]=="tab":
|
||||||
|
self.tab_complete()
|
||||||
|
|
||||||
def on_textinput(self,window,text):
|
def on_textinput(self,window,text):
|
||||||
#print(text)
|
#print(text)
|
||||||
if self.insert:
|
if self.insert:
|
||||||
@ -165,10 +176,13 @@ class Command_prompt(Label):
|
|||||||
|
|
||||||
|
|
||||||
# parse text as argv
|
# parse text as argv
|
||||||
def parse_command(self):
|
def parse_argv(self):
|
||||||
# init
|
# init
|
||||||
self.argv=[""]
|
self.argv=[""]
|
||||||
|
|
||||||
|
# return position of cursor
|
||||||
|
cursor=(0,0)
|
||||||
|
|
||||||
# whether inside quotes
|
# whether inside quotes
|
||||||
single_quoted=False
|
single_quoted=False
|
||||||
double_quoted=False
|
double_quoted=False
|
||||||
@ -199,5 +213,41 @@ class Command_prompt(Label):
|
|||||||
# reset backslash
|
# reset backslash
|
||||||
backslashed=False
|
backslashed=False
|
||||||
|
|
||||||
|
# record position of cursor
|
||||||
|
if self.cursor==i+1:
|
||||||
|
cursor=(len(self.argv)-1,len(self.argv[len(self.argv)-1]))
|
||||||
|
|
||||||
|
|
||||||
|
return cursor
|
||||||
|
|
||||||
|
|
||||||
|
# tab completion
|
||||||
|
def tab_complete(self):
|
||||||
|
# parse argv
|
||||||
|
cursor=self.parse_argv()
|
||||||
|
|
||||||
|
# write and edit commands
|
||||||
|
if self.argv[0]=="w" or self.argv[0]=="e":
|
||||||
|
# check that cursor is in first argument
|
||||||
|
if cursor[0]==1:
|
||||||
|
# complete filesystem path
|
||||||
|
self.append_text_at_cursor(self.complete_path(self.argv[1],cursor[1]))
|
||||||
|
|
||||||
|
# complete filesystem path
|
||||||
|
def complete_path(self,base,end):
|
||||||
|
paths=glob.glob(glob.escape(base[:end])+"*")
|
||||||
|
print(glob.escape(base[:end])+"*", paths)
|
||||||
|
if len(paths)==0:
|
||||||
|
return ""
|
||||||
|
elif len(paths)==1:
|
||||||
|
# append '/' to directories
|
||||||
|
if os.path.isdir(paths[0]):
|
||||||
|
return paths[0][end:]+"/"
|
||||||
|
else:
|
||||||
|
return paths[0][end:]
|
||||||
|
else:
|
||||||
|
# display in status bar
|
||||||
|
self.app.status_bar.text=""
|
||||||
|
for path in paths:
|
||||||
|
self.app.status_bar.text+="\""+path+"\" "
|
||||||
|
return common_substr(paths)[end:]
|
||||||
|
13
tools.py
13
tools.py
@ -9,3 +9,16 @@ def isint_nonzero(x):
|
|||||||
if abs(x)<1e-11:
|
if abs(x)<1e-11:
|
||||||
return False
|
return False
|
||||||
return abs(round(x)-x)<1e-11
|
return abs(round(x)-x)<1e-11
|
||||||
|
|
||||||
|
# find the common substring in a list of strings
|
||||||
|
def common_substr(strings):
|
||||||
|
out=""
|
||||||
|
if len(strings)==0:
|
||||||
|
return out
|
||||||
|
for i in range(len(strings[0])):
|
||||||
|
for j in range(1,len(strings)):
|
||||||
|
if strings[j][i]!=strings[0][i]:
|
||||||
|
return out
|
||||||
|
out+=strings[0][i]
|
||||||
|
|
||||||
|
return out
|
||||||
|
Loading…
Reference in New Issue
Block a user