fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Johannes AltmanningerUse globals for color variables, react to light/dark mode190d367bc

master
14.5 KiB425 linesraw
1"""pexpect_helper provides a wrapper around the pexpect module.
2
3This module exposes a single class SpawnedProc, which wraps pexpect.spawn().
4This exposes a pseudo-tty, which fish or another process may talk to.
5The send() function may be used to send data to fish, and the expect_* family
6of functions may be used to match what is output to the tty.
7
8Example usage:
9  sp = SpawnedProc() # this launches fish
10  sp.expect_prompt() # wait for a prompt
11  sp.sendline("echo hello world")
12  sp.expect_prompt("hello world")
13
14"""
15
16from __future__ import print_function
17
18import inspect
19import os
20import os.path
21import re
22import sys
23import time
24import pexpect
25from signal import Signals
26
27# Default timeout for failing to match.
28TIMEOUT_SECS = 5
29
30UNEXPECTED_SUCCESS = object()
31
32# When rendering fish's output, remove the control sequences that modify terminal state,
33# to avoid confusing the calling terminal.
34SANITIZE_FOR_PRINTING_RE = re.compile(
35    r"""
36        # Filter CSI commands except for colors and cursor movement.
37        (?!\x1b\[\d*m)
38        (?!\x1b\[K)
39        (?!\x1b\[\d*[ABCD])
40        \x1b\[[\x30-\x3f]*[\x20-\x2f]*[\x40-\x7e]
41        # OSC
42        | \x1b\].*?\x07
43        # DCS
44        | \x1bP.*?\x1b\\
45        # application keypad mode
46        | \x1b=
47        | \x1b>
48    """,
49    re.VERBOSE,
50)
51
52assert SANITIZE_FOR_PRINTING_RE.sub("", "\x1b[>4;1m") == ""
53assert SANITIZE_FOR_PRINTING_RE.sub("", "\x1b[31m") == "\x1b[31m"
54
55
56def get_prompt_re(counter):
57    """Return a regular expression for matching a with a given prompt counter."""
58    return re.compile("prompt %d>" % counter)
59
60
61def get_callsite():
62    """Return a triple (filename, line_number, line_text) of the call site location."""
63    callstack = inspect.getouterframes(inspect.currentframe())
64    for f in callstack:
65        # Skip call sites from this file.
66        if inspect.getmodule(f.frame) is Message.MODULE:
67            continue
68        # Skip functions which have a truthy callsite_skip attribute.
69        if getattr(f.function, "callsite_skip", False):
70            continue
71        return (os.path.basename(f.filename), f.lineno, f.code_context)
72    return ("Unknown", -1, "")
73
74
75def escape(s):
76    """Escape the string 's' to make it human-understandable."""
77    res = []
78    for c in s:
79        if c == "\n":
80            res.append("\\n")
81        elif c == "\r":
82            res.append("\\r")
83        elif c == "\t":
84            res.append("\\t")
85        elif c.isprintable():
86            res.append(c)
87        else:
88            res.append("\\x{:02x}".format(ord(c)))
89    return "".join(res)
90
91
92def pexpect_error_type(err):
93    """Return a human-readable description of a pexpect error type."""
94    if isinstance(err, pexpect.EOF):
95        return "EOF"
96    elif isinstance(err, pexpect.TIMEOUT):
97        return "timeout"
98    elif err is UNEXPECTED_SUCCESS:
99        return "unexpected success"
100    else:
101        return "unknown error"
102
103
104class Message(object):
105    """Some text either sent-to or received-from the spawned proc.
106
107    Attributes:
108        dir: the message direction, either DIR_INPUT or DIR_OUTPUT
109        filename: the name of the file from which the message was sent
110        text: the text of the messages
111        when: a timestamp of when the message was sent
112    """
113
114    # Input is input into fish shell ("sent data").
115    DIR_INPUT = " INPUT"
116
117    # Output means output from fish shell ("received data").
118    DIR_OUTPUT = "OUTPUT"
119
120    MODULE = sys.modules[__name__]
121
122    def __init__(self, dir, text, when):
123        """Construct from a direction, message text and timestamp."""
124        self.dir = dir
125        self.filename, self.lineno, _ = get_callsite()
126        self.text = text
127        self.when = when
128
129    @staticmethod
130    def sent_input(text, when):
131        """Return an input message with the given text."""
132        return Message(Message.DIR_INPUT, text, when)
133
134    @staticmethod
135    def received_output(text, when):
136        """Return a output message with the given text."""
137        return Message(Message.DIR_OUTPUT, text, when)
138
139
140class SpawnedProc(object):
141    """A process, talking to our ptty. This wraps pexpect.spawn.
142
143    Attributes:
144        colorize: whether error messages should have ANSI color escapes
145        messages: list of Message sent and received, in-order
146        start_time: the timestamp of the first message, or None if none yet
147        spawn: the pexpect.spawn value
148        prompt_counter: the index of the prompt. This cooperates with the fish_prompt
149            function to ensure that each printed prompt is distinct.
150    """
151
152    def __init__(
153        self,
154        name="fish",
155        timeout=TIMEOUT_SECS,
156        env=os.environ.copy(),
157        scroll_content_up_supported: bool = False,
158        **kwargs,
159    ):
160        """Construct from a name, timeout, and environment.
161
162        Args:
163            name: the name of the executable to launch, as a key into the
164                  environment dictionary. By default this is 'fish' but may be
165                  other executables.
166            timeout: A timeout to pass to pexpect. This indicates how long to wait
167                     before giving up on some expected output.
168            env: a string->string dictionary, describing the environment variables.
169        """
170        import shlex
171
172        if name not in env:
173            raise ValueError("'%s' variable not found in environment" % name)
174        exe_path = env.get(name)
175        # HACK: If there are no args, pexpect will fail if exe_path contains any shell metachars.
176        # But not if there are args, in which case it probably switches spawning method?
177        if "args" not in kwargs:
178            exe_path = shlex.quote(exe_path)
179        self.colorize = sys.stdout.isatty() or env.get("FISH_FORCE_COLOR", "0") == "1"
180        self.messages = []
181        self.start_time = None
182        if "FISH_TEST_NO_RECURRENT_QUERIES" not in env:
183            env["FISH_TEST_NO_RECURRENT_QUERIES"] = "1"
184        self.spawn = pexpect.spawn(
185            exe_path, env=env, encoding="utf-8", timeout=timeout, **kwargs
186        )
187        self.spawn.delaybeforesend = None
188        self.prompt_counter = 0
189        if scroll_content_up_supported:
190            # XTGETTCAP
191            key = bytes.hex(b"indn")
192            value = bytes.hex(b"dont-care")
193            self.spawn.send(f"\x1bP1+r{key}={value}\x1b\\")
194        if env.get("TERM") != "dumb":
195            self.send_primary_device_attribute()
196
197    def send_cursor_position_report(self, *, y: int, x: int):
198        assert x != 0
199        assert y != 0
200        self.spawn.send(f"\x1b[{y};{x}R")
201
202    def send_primary_device_attribute(self):
203        self.spawn.send("\x1b[?123c")  # Primary Device Attribute
204
205    def time_since_first_message(self):
206        """Return a delta in seconds since the first message, or 0 if this is the first."""
207        now = time.monotonic()
208        if not self.start_time:
209            self.start_time = now
210        return now - self.start_time
211
212    def send(self, s):
213        """Cover over pexpect.spawn.send().
214        Send the given string to the tty, returning the number of bytes written.
215        """
216        res = self.spawn.send(s)
217        when = self.time_since_first_message()
218        self.messages.append(Message.sent_input(s, when))
219        return res
220
221    def sendline(self, s):
222        """Cover over pexpect.spawn.sendline().
223        Send the given string + linesep to the tty, returning the number of bytes written.
224        """
225        return self.send(s + os.linesep)
226
227    def expect_re(self, pat, pat_desc=None, unmatched=None, shouldfail=False, **kwargs):
228        """Cover over pexpect.spawn.expect().
229        Consume all "new" output of self.spawn until the given pattern is matched, or
230        the timeout is reached.
231        Note that output between the current position and the location of the match is
232        consumed as well.
233        The pattern is typically a regular expression in string form, but may also be
234        any of the types accepted by pexpect.spawn.expect().
235        If the 'unmatched' parameter is given, it is printed as part of the error message
236        of any failure.
237        On failure, this prints an error and exits.
238        """
239        try:
240            self.spawn.expect(pat, **kwargs)
241            when = self.time_since_first_message()
242            self.messages.append(
243                Message.received_output(self.spawn.match.group(), when)
244            )
245            # When a match is found,
246            # spawn.match is the MatchObject that produced it.
247            # This can be used to check what exactly was matched.
248            if shouldfail:
249                err = UNEXPECTED_SUCCESS
250                if not pat_desc:
251                    pat_desc = str(pat)
252                self.report_exception_and_exit(pat_desc, unmatched, err)
253            return self.spawn.match
254        except pexpect.ExceptionPexpect as err:
255            if shouldfail:
256                return True
257            if not pat_desc:
258                pat_desc = str(pat)
259            self.report_exception_and_exit(pat_desc, unmatched, err)
260
261    def expect_str(self, s, **kwargs):
262        """Cover over expect_re() which accepts a literal string."""
263        return self.expect_re(re.escape(s), **kwargs)
264
265    def expect_prompt(self, *args, increment=True, **kwargs):
266        """Convenience function which matches some text and then a prompt.
267        Match the given positional arguments as expect_re, and then look
268        for a prompt.
269        If increment is set, then this should be a new prompt and the prompt counter
270        should be bumped; otherwise this is not a new prompt.
271        Returns None on success, and exits on failure.
272        Example:
273           sp.sendline("echo hello world")
274           sp.expect_prompt("hello world")
275        """
276        if args:
277            self.expect_re(*args, **kwargs)
278        if increment:
279            self.prompt_counter += 1
280        self.expect_re(
281            get_prompt_re(self.prompt_counter),
282            pat_desc="prompt %d" % self.prompt_counter,
283        )
284
285    def report_exception_and_exit(self, pat, unmatched, err):
286        """Things have gone badly.
287        We have an exception 'err', some pexpect.ExceptionPexpect.
288        Report it to stdout, along with the offending call site.
289        If 'unmatched' is set, print it to stdout.
290        """
291        # Close the process so we can get the status
292        self.spawn.close()
293        colors = self.colors()
294        failtype = pexpect_error_type(err)
295        # If we get an EOF, we check if the process exited with a signal.
296        # This shows us e.g. if it crashed
297        if (
298            failtype == "EOF"
299            and self.spawn.signalstatus is not None
300            and self.spawn.signalstatus != 0
301        ):
302            failtype = "SIGNAL " + Signals(self.spawn.signalstatus).name
303
304        fmtkeys = {"failtype": failtype, "pat": escape(pat)}
305        fmtkeys.update(**colors)
306
307        filename, lineno, code_context = get_callsite()
308        fmtkeys["filename"] = filename
309        fmtkeys["lineno"] = lineno
310        fmtkeys["code"] = "\n".join([n.strip() for n in code_context if n])
311
312        if unmatched:
313            print(
314                "{RED}Error: {NORMAL}{BOLD}{unmatched}{RESET}".format(
315                    unmatched=unmatched, **fmtkeys
316                )
317            )
318        print(
319            "{RED}Failed to match pattern:{NORMAL} {BOLD}{pat}{RESET}".format(**fmtkeys)
320        )
321        print(
322            "{filename}:{lineno}: {BOLD}{failtype}{RESET} from {code}".format(**fmtkeys)
323        )
324
325        print("")
326        print("{CYAN}Escaped buffer:{RESET}".format(**colors))
327        print(escape(self.spawn.before))
328        print("")
329        print("{CYAN}When written to the tty, this looks like:{RESET}".format(**colors))
330        print("{CYAN}<-------{RESET}".format(**colors))
331        sys.stdout.write(SANITIZE_FOR_PRINTING_RE.sub("", self.spawn.before))
332        sys.stdout.flush()
333        maybe_nl = ""
334        if not self.spawn.before.endswith("\n"):
335            maybe_nl = "\n{CYAN}(no trailing newline)".format(**colors)
336        print(
337            "{RESET}{maybe_nl}{CYAN}------->{RESET}".format(maybe_nl=maybe_nl, **colors)
338        )
339
340        print("")
341
342        # Show the last 10 messages.
343        print("Last 10 messages:")
344        delta = None
345        for m in self.messages[-10:]:
346            etext = escape(m.text)
347            timestamp = m.when * 1000.0
348            # Use relative timestamps and add a sign.
349            # This assumes a max length of 10^10 milliseconds (115 days) for the initial timestamp,
350            # and 11.5 days for the delta.
351            if delta:
352                timestamp -= delta
353                timestampstr = "{timestamp:+10.2f} ms".format(timestamp=timestamp)
354            else:
355                timestampstr = "{timestamp:10.2f} ms".format(timestamp=timestamp)
356            delta = m.when * 1000.0
357            print(
358                "{dir} {timestampstr} (Line {lineno}): {BOLD}{etext}{RESET}".format(
359                    dir=m.dir,
360                    timestampstr=timestampstr,
361                    filename=m.filename,
362                    lineno=m.lineno,
363                    etext=etext,
364                    **colors,
365                )
366            )
367        print("")
368        sys.exit(1)
369
370    def sleep(self, secs):
371        """Cover over time.sleep()."""
372        time.sleep(secs)
373
374    def colors(self):
375        """Return a dictionary mapping color names to ANSI escapes"""
376
377        def ansic(n):
378            """Return either an ANSI escape sequence for a color, or empty string."""
379            return "\033[%dm" % n if self.colorize else ""
380
381        return {
382            "RESET": ansic(0),
383            "BOLD": ansic(1),
384            "NORMAL": ansic(39),
385            "BLACK": ansic(30),
386            "RED": ansic(31),
387            "GREEN": ansic(32),
388            "YELLOW": ansic(33),
389            "BLUE": ansic(34),
390            "MAGENTA": ansic(35),
391            "CYAN": ansic(36),
392            "LIGHTGRAY": ansic(37),
393            "DARKGRAY": ansic(90),
394            "LIGHTRED": ansic(91),
395            "LIGHTGREEN": ansic(92),
396            "LIGHTYELLOW": ansic(93),
397            "LIGHTBLUE": ansic(94),
398            "LIGHTMAGENTA": ansic(95),
399            "LIGHTCYAN": ansic(96),
400            "WHITE": ansic(97),
401        }
402
403
404def control(char: str) -> str:
405    """Returns the char sent when control is pressed along the given key."""
406    assert len(char) == 1
407    char = char.lower()
408    if ord("a") <= ord(char) <= ord("z"):
409        return chr(ord(char) - ord("a") + 1)
410    return chr(
411        {
412            "@": 0,
413            "`": 0,
414            "[": 27,
415            "{": 27,
416            "\\": 28,
417            "|": 28,
418            "]": 29,
419            "}": 29,
420            "^": 30,
421            "~": 30,
422            "_": 31,
423            "?": 127,
424        }[char]
425    )