myshell-py 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- myshell/__init__.py +1 -0
- myshell/__main__.py +5 -0
- myshell/aliases.py +22 -0
- myshell/arith.py +273 -0
- myshell/builtins.py +490 -0
- myshell/cli.py +155 -0
- myshell/compat.py +14 -0
- myshell/completion.py +137 -0
- myshell/coreutils.py +837 -0
- myshell/editor.py +218 -0
- myshell/errors.py +41 -0
- myshell/executor.py +752 -0
- myshell/expand.py +704 -0
- myshell/guide.py +609 -0
- myshell/heredoc.py +26 -0
- myshell/highlight.py +227 -0
- myshell/history.py +123 -0
- myshell/jobs.py +254 -0
- myshell/lexer.py +246 -0
- myshell/more_builtins.py +544 -0
- myshell/parser.py +437 -0
- myshell/paths.py +18 -0
- myshell/progress.py +98 -0
- myshell/prompt.py +197 -0
- myshell/quiz.py +148 -0
- myshell/state.py +107 -0
- myshell_py-0.2.0.dist-info/METADATA +195 -0
- myshell_py-0.2.0.dist-info/RECORD +30 -0
- myshell_py-0.2.0.dist-info/WHEEL +4 -0
- myshell_py-0.2.0.dist-info/entry_points.txt +2 -0
myshell/completion.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"""Tab completion for commands, paths and variables."""
|
|
2
|
+
import os
|
|
3
|
+
import re
|
|
4
|
+
from typing import List, Optional, Tuple
|
|
5
|
+
|
|
6
|
+
from . import history
|
|
7
|
+
from .builtins import builtin_names
|
|
8
|
+
from .state import ShellState
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
ESCAPE_CHARS = " \t\n\\'\"$&;|<>*?[](){}#`!"
|
|
12
|
+
COMMAND_PREFIXES = ("|", "&&", "||", ";", "&")
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def escape_word(text: str) -> str:
|
|
16
|
+
return "".join("\\" + c if c in ESCAPE_CHARS else c for c in text)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def unescape_word(text: str) -> str:
|
|
20
|
+
return re.sub(r"\\(.)", r"\1", text)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
readline = history.readline_module() # None when there is no readline
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class Completer:
|
|
27
|
+
"""Completes command names, file paths and $variables for readline."""
|
|
28
|
+
|
|
29
|
+
def __init__(self, state: ShellState):
|
|
30
|
+
self.state = state
|
|
31
|
+
self.matches: List[str] = []
|
|
32
|
+
|
|
33
|
+
def __call__(self, text: str, index: int) -> Optional[str]:
|
|
34
|
+
if index == 0:
|
|
35
|
+
self.matches = self.candidates(text)
|
|
36
|
+
return self.matches[index] if index < len(self.matches) else None
|
|
37
|
+
|
|
38
|
+
def candidates(self, text: str) -> List[str]:
|
|
39
|
+
before = readline.get_line_buffer()[:readline.get_begidx()]
|
|
40
|
+
return self.candidates_for(text, before)
|
|
41
|
+
|
|
42
|
+
def candidates_for(self, text: str, before: str) -> List[str]:
|
|
43
|
+
"""Matches for the word `text`, which follows the line part `before`."""
|
|
44
|
+
if text.startswith("$"):
|
|
45
|
+
return self.variable_matches(text)
|
|
46
|
+
before = before.rstrip()
|
|
47
|
+
at_command = (before == "" or (before.endswith(COMMAND_PREFIXES)
|
|
48
|
+
and not before.endswith(">")))
|
|
49
|
+
if at_command and "/" not in text and not text.startswith("~"):
|
|
50
|
+
return self.command_matches(text)
|
|
51
|
+
return self.path_matches(text)
|
|
52
|
+
|
|
53
|
+
def variable_matches(self, text: str) -> List[str]:
|
|
54
|
+
names = set(os.environ) | set(self.state.variables)
|
|
55
|
+
return sorted("$" + n for n in names if ("$" + n).startswith(text))
|
|
56
|
+
|
|
57
|
+
def command_matches(self, text: str) -> List[str]:
|
|
58
|
+
functions = getattr(self.state, "functions", None) or {}
|
|
59
|
+
names = {n for n in builtin_names() + list(self.state.aliases) + list(functions)
|
|
60
|
+
if n.startswith(text)}
|
|
61
|
+
for directory in os.environ.get("PATH", "").split(os.pathsep):
|
|
62
|
+
try:
|
|
63
|
+
entries = os.listdir(directory or ".")
|
|
64
|
+
except OSError:
|
|
65
|
+
continue
|
|
66
|
+
for entry in entries:
|
|
67
|
+
if entry.startswith(text) and entry not in names:
|
|
68
|
+
path = os.path.join(directory, entry)
|
|
69
|
+
if os.path.isfile(path) and os.access(path, os.X_OK):
|
|
70
|
+
names.add(entry)
|
|
71
|
+
return sorted(names)
|
|
72
|
+
|
|
73
|
+
def path_matches(self, text: str) -> List[str]:
|
|
74
|
+
raw = unescape_word(text)
|
|
75
|
+
if raw == "~":
|
|
76
|
+
return ["~/"]
|
|
77
|
+
base = os.path.basename(raw)
|
|
78
|
+
prefix = raw[:len(raw) - len(base)]
|
|
79
|
+
directory = os.path.expanduser(prefix) if prefix else "."
|
|
80
|
+
try:
|
|
81
|
+
entries = sorted(os.listdir(directory))
|
|
82
|
+
except OSError:
|
|
83
|
+
return []
|
|
84
|
+
result = []
|
|
85
|
+
for name in entries:
|
|
86
|
+
if not name.startswith(base):
|
|
87
|
+
continue
|
|
88
|
+
if name.startswith(".") and not base.startswith("."):
|
|
89
|
+
continue # hide dotfiles unless asked
|
|
90
|
+
suffix = "/" if os.path.isdir(os.path.join(directory, name)) else ""
|
|
91
|
+
result.append(escape_word(prefix + name).replace("\\~", "~") + suffix)
|
|
92
|
+
return result
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
WORD_DELIMS = " \t\n;|&<>"
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def word_start(line: str) -> int:
|
|
99
|
+
"""Index where the word at the end of `line` begins. A backslash keeps the next
|
|
100
|
+
character in the word, so "my\\ fi" is one word."""
|
|
101
|
+
start = 0
|
|
102
|
+
i = 0
|
|
103
|
+
while i < len(line):
|
|
104
|
+
c = line[i]
|
|
105
|
+
if c == "\\":
|
|
106
|
+
i += 2
|
|
107
|
+
continue
|
|
108
|
+
i += 1
|
|
109
|
+
if c in WORD_DELIMS:
|
|
110
|
+
start = i
|
|
111
|
+
return min(start, len(line))
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def complete_line(state: ShellState, line_before_cursor: str) -> Tuple[int, List[str]]:
|
|
115
|
+
"""Completions for the word ending at the cursor, without readline.
|
|
116
|
+
Returns (index where that word starts, candidates that replace it)."""
|
|
117
|
+
start = word_start(line_before_cursor)
|
|
118
|
+
text = line_before_cursor[start:]
|
|
119
|
+
try:
|
|
120
|
+
found = Completer(state).candidates_for(text, line_before_cursor[:start])
|
|
121
|
+
except Exception:
|
|
122
|
+
found = []
|
|
123
|
+
return start, found
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def setup_readline(state: ShellState) -> None:
|
|
127
|
+
if readline is None:
|
|
128
|
+
return
|
|
129
|
+
try:
|
|
130
|
+
readline.set_completer_delims(" \t\n;|&<>")
|
|
131
|
+
readline.set_completer(Completer(state))
|
|
132
|
+
if "libedit" in (readline.__doc__ or ""): # macOS
|
|
133
|
+
readline.parse_and_bind("bind ^I rl_complete")
|
|
134
|
+
else:
|
|
135
|
+
readline.parse_and_bind("tab: complete")
|
|
136
|
+
except Exception:
|
|
137
|
+
pass
|