From 99ad0407b812ac74c6ef4e5cd49dc097ca637339 Mon Sep 17 00:00:00 2001 From: Ian Jauslin Date: Sat, 16 Oct 2021 02:09:53 -0400 Subject: [PATCH] path completion --- command_prompt.py | 56 ++++++++++++++++++++++++++++++++++++++++++++--- tools.py | 13 +++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/command_prompt.py b/command_prompt.py index ab1538b..6d0492e 100644 --- a/command_prompt.py +++ b/command_prompt.py @@ -1,6 +1,10 @@ from kivy.uix.label import Label from kivy.core.window import Window from kivy.graphics import Color,Rectangle +import glob +import os.path + +from tools import common_substr class Command_prompt(Label): @@ -47,15 +51,18 @@ class Command_prompt(Label): def on_key_down(self, keyboard, keycode, text, modifiers): - print(keycode,text,modifiers) + #print(keycode,text,modifiers) if self.insert: # process modifiers mods=self.process_modifiers(modifiers) + # clear status bar + self.app.status_bar.text="" + # process command if keycode[1]=="enter": self.insert=False - self.parse_command() + self.parse_argv() self.set_text("") # move @@ -76,6 +83,10 @@ class Command_prompt(Label): elif mods==['c'] and text=="k": self.delete_all_forward() + # tab completion + elif keycode[1]=="tab": + self.tab_complete() + def on_textinput(self,window,text): #print(text) if self.insert: @@ -165,10 +176,13 @@ class Command_prompt(Label): # parse text as argv - def parse_command(self): + def parse_argv(self): # init self.argv=[""] + # return position of cursor + cursor=(0,0) + # whether inside quotes single_quoted=False double_quoted=False @@ -199,5 +213,41 @@ class Command_prompt(Label): # reset backslash 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:] diff --git a/tools.py b/tools.py index a9ef4e9..06e36a0 100644 --- a/tools.py +++ b/tools.py @@ -9,3 +9,16 @@ def isint_nonzero(x): if abs(x)<1e-11: return False 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