- Do not log the tty control characters for the color.
[polysh.git] / gsh / completion.py
blob19e2ef9634374aa1e5844882c69fd62842a8f8fe
1 # This program is free software; you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation; either version 2 of the License, or
4 # (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU Library General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15 # See the COPYING file for license information.
17 # Copyright (c) 2008 Guillaume Chazarain <guichaz@gmail.com>
19 import glob
20 import os
21 import readline
23 from gsh.control_commands_helpers import complete_control_command
24 from gsh.control_commands_helpers import expand_local_path
26 def complete_local_path(path):
27 def get_suffix(p):
28 if os.path.isdir(p):
29 return '/'
30 return ''
31 path = expand_local_path(path)
32 paths = [p + get_suffix(p) for p in glob.glob(path + '*')]
33 return paths
35 def remove_dupes(words):
36 added = set()
37 results = list()
38 for w in words:
39 stripped = w.rstrip('/ ')
40 if stripped not in added:
41 added.add(stripped)
42 results.append(w)
43 return results
45 def read_commands_in_path():
46 commands = set()
48 for path in (os.getenv('PATH') or '').split(':'):
49 if path:
50 try:
51 listing = os.listdir(path)
52 except OSError:
53 pass
54 else:
55 commands |= set(listing)
56 return list(commands)
58 # All the words that have been typed in gsh. Used by the completion mechanism.
59 history_words = set()
61 # When listing possible completions, the complete() function is called with
62 # an increasing state parameter until it returns None. Cache the completion
63 # list instead of regenerating it for each completion item.
64 completion_results = None
66 # Commands in $PATH, used for the completion of the first word
67 user_commands_in_path = read_commands_in_path()
69 try:
70 import ctypes.util
71 lib_readline = ctypes.cdll.LoadLibrary(ctypes.util.find_library("readline"))
72 rl_completion_append_character = ctypes.c_char.in_dll(lib_readline,
73 "rl_completion_append_character")
74 except Exception:
75 class rl_completion_append_character:
76 pass
79 def complete(text, state):
80 """On tab press, return the next possible completion"""
81 rl_completion_append_character.value = '\0'
82 global completion_results
83 if state == 0:
84 line = readline.get_line_buffer()
85 if line.startswith(':'):
86 # Control command completion
87 completion_results = complete_control_command(line, text)
88 else:
89 if line.startswith('!') and text and line.startswith(text):
90 dropped_exclam = True
91 text = text[1:]
92 else:
93 dropped_exclam = False
94 completion_results = []
95 # Complete local paths
96 completion_results += complete_local_path(text)
97 # Complete from history
98 l = len(text)
99 completion_results += [w + ' ' for w in history_words if \
100 len(w) > l and w.startswith(text)]
101 if readline.get_begidx() == 0:
102 # Completing first word from $PATH
103 completion_results += [w + ' ' for w in user_commands_in_path \
104 if len(w) > l and w.startswith(text)]
105 completion_results = remove_dupes(completion_results)
106 if dropped_exclam:
107 completion_results = ['!' + r for r in completion_results]
109 if state < len(completion_results):
110 return completion_results[state]
111 completion_results = None
113 def add_to_history(cmd):
114 words = [w for w in cmd.split() if len(w) > 1]
115 history_words.update(words)
116 if len(history_words) > 10000:
117 del history_words[:-10000]
119 def remove_last_history_item():
120 """The user just typed a password..."""
121 last = readline.get_current_history_length() - 1
122 readline.remove_history_item(last)
124 def install_completion_handler():
125 readline.set_completer(complete)
126 readline.parse_and_bind('tab: complete')
127 readline.set_completer_delims(' \t\n')