-
Notifications
You must be signed in to change notification settings - Fork 3
/
tab_complete.py
170 lines (136 loc) · 5.29 KB
/
tab_complete.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import os
import readline
import shlex
<<<<<<< 55a23e4d0277ec2f24ad8098cdefb347290511fb
=======
import sys
def stderr(*strings):
print(*strings, file=sys.stdout)
>>>>>>> added xrandr_setup.sh and tab_complete.py
def prep_readline( completer ):
readline.set_completer_delims( '\n\t;' )
readline.parse_and_bind( 'tab: complete' )
<<<<<<< 55a23e4d0277ec2f24ad8098cdefb347290511fb
readline.set_completer( completer )
=======
readline.set_completer( completer.complete )
readline.set_completion_display_matches_hook(completer.display_matches)
>>>>>>> added xrandr_setup.sh and tab_complete.py
def _list_dir( directory ):
files = []
for filename in os.listdir( directory ):
path = os.path.join( directory, filename )
if os.path.isdir( path ):
files.append( '{}{}'.format( path, os.sep ) )
files.append( filename )
return files
<<<<<<< 55a23e4d0277ec2f24ad8098cdefb347290511fb
=======
>>>>>>> added xrandr_setup.sh and tab_complete.py
def norm_join_path(dirname, filename ):
path = os.path.join( dirname, filename )
return os.path.normpath( path )
def expand_path( path ):
for function in ( os.path.expandvars, os.path.expanduser, os.path.normpath ):
path = function( path )
return path
<<<<<<< 55a23e4d0277ec2f24ad8098cdefb347290511fb
def _complete_path( path ):
=======
def _complete_path( path, state = None ):
>>>>>>> added xrandr_setup.sh and tab_complete.py
if not path:
return _list_dir('.')
path = expand_path( path )
dirname, path_fragment = os.path.split( path )
dirname = dirname or '.'
guesses = [ norm_join_path( dirname, guess ) for guess in _list_dir( dirname ) if guess.startswith( path_fragment ) ]
if os.path.isdir( path ):
os.path.split()
return [ norm_join_path( path, guess ) for guess in _list_dir( path ) ]
if not guesses or len( guesses ) == 1:
return path + os.sep
return guesses
class Completer:
<<<<<<< 55a23e4d0277ec2f24ad8098cdefb347290511fb
def __init__( self, commands ):
self.commands = commands
self._state = self._text = self._line = None
def complete( self, text, state ):
if not text or [ x for x in self._commands.keys() if x.startswith( text ) ]:
return [ x for x in self.commands.keys() ][state]
return self._complete_command( text, state )
def _complete_command( self, text, state ):
args = shlex.split( text )
if len(args) > 1:
command = args.pop()
else:
command = text
if command.strip() in self.commands:
return self.commands[command.strip()]( args )
guesses = [ guess + ' ' for guess in self.commands if guess.startswith( command ) ]
guesses.append( )
return guesses
=======
def __init__( self, commands, subcompleters = None ):
self.commands = commands
self.subcompleters = subcompleters
self._state = self._text = self._line = None
def complete(self, text, state):
return self._get_guesses(text, state)[state]
def _get_guesses( self, text, state ):
args = shlex.split( text )
if text:
args = shlex.split( text )
if args:
if self._is_command( args[0] ):
stderr('command')
return self._complete_command( args, state )
else:
stderr('fallback')
self._fallback(args, state)
cmds = [ x for x in self.commands.keys() ]
stderr(cmds)
return cmds
def _is_command(self, first_string):
return [ x for x in self.commands.keys() if x.startswith( first_string ) or x == first_string ]
def _complete_command( self, args, state ):
command = args.pop().strip()
if command.strip() in self.commands:
return self.commands[command.strip()]( args )
guesses = [ guess + ' ' for guess in self.commands if guess.startswith( command ) ]
return guesses
def _fallback(self, args, state):
for completer in self.subcompleters:
yield completer(args, state)
def display_matches(self, substitution, matches, longest_match_length):
line_buffer = readline.get_line_buffer()
columns = os.environ.get('COLUMNS', 80)
print()
guess_template = '{:<' + str(int(max(map(len, matches)) * 1.2)) + '}'
guesses = ''
for match in matches:
match = guess_template.format(match[len(substitution):])
if len(guesses + match) > columns:
print(guesses)
guesses = ''
guesses += match
if guesses:
print(guesses)
print('> ', line_buffer, end='', flush=True)
>>>>>>> added xrandr_setup.sh and tab_complete.py
def _complete_hello( args ):
if not args or not [ x for x in args if x ]:
return './'
else:
path = args[0]
return _complete_path( path )
if __name__ == '__main__':
<<<<<<< 55a23e4d0277ec2f24ad8098cdefb347290511fb
comp = Completer( {'hello': _complete_hello } )
prep_readline( comp.complete )
=======
comp = Completer( {'hello': _complete_hello }, (_complete_path,) )
prep_readline( comp )
>>>>>>> added xrandr_setup.sh and tab_complete.py
input( 'hello tabcomplete>>> ' )