question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Codes printed to terminal when using typeahead with FinalTerm sequences

See original GitHub issue

Consider the finalterm-shell-integration.py example. Make the following change to it

--- a/examples/finalterm-shell-integration.py
+++ b/examples/finalterm-shell-integration.py
@@ -8,7 +8,7 @@ from __future__ import unicode_literals
 from prompt_toolkit import prompt
 from prompt_toolkit.token import Token
 import sys
-
+import time

 BEFORE_PROMPT = '\033]133;A\a'
 AFTER_PROMPT = '\033]133;B\a'
@@ -29,8 +29,9 @@ def get_prompt_tokens(cli):


 if __name__ == '__main__':
-    answer = prompt(get_prompt_tokens=get_prompt_tokens)
-
-    sys.stdout.write(BEFORE_OUTPUT)
-    print('You said: %s' % answer)
-    sys.stdout.write(AFTER_OUTPUT.format(command_status=0))
+    while True:
+        answer = prompt(get_prompt_tokens=get_prompt_tokens)
+        time.sleep(2)
+        sys.stdout.write(BEFORE_OUTPUT)
+        print('You said: %s' % answer)
+        sys.stdout.write(AFTER_OUTPUT.format(command_status=0))

Then run the script and type

hello
hello
hello
hello

several times without waiting for the prompt. Eventually, you’ll see something like this:

Say something: # hello
^[[45;1Rello
You said: hello

The ^[[45;1R is printed before the output. I don’t understand what that escape code is exactly. It doesn’t look like one of the FinalTerm codes. I believe the final term codes themselves are being printed correctly, as the “select output” feature in iTerm2 works correctly (it selects everything, including the ^[[45;1R).

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
asmeurercommented, Apr 30, 2018

As far as I can tell this is fixed in 2.0. I think somewhere between the new typeahead code and the better CPR handling it got fixed (I can bisect if you are interested).

I think I figured out the issue with PipeInput specifically (see https://github.com/jonathanslenders/python-prompt-toolkit/issues/610#issuecomment-385522974). Basically the stdout Output class assumes the input can respond to a CPR request, and has no way to know if it is PipeInput. Perhaps PipeInput should get a faked CPR request responder to deal with this? The real problem seems to be the disconnect between Input and Output classes in the code, even though CPR requests and responses inherently link the two (and require they both be a real TTY). At any rate, for now, I’ve come the conclusion that PipeInput should never be used for interactive purposes (only for things like unit tests). I’m still working on translating my code to 2.0 so I can’t say yet if my issues fully exist there.

One thing I couldn’t figure out though is what exactly the CPR responses even do. I commented out the CPR request code, and everything seemed to still act normal in my application. I also noticed that 2.0 has some code to automatically stop sending CPR codes if it notices they are not being responded to. So I’m curious exactly what I should expect to stop working if CPR codes are disabled. And if it’s nothing I care about if I could just disable them myself.

0reactions
max-sixtycommented, Oct 3, 2018

For more info, my ipython_config.py consists of:

import sys
from prompt_toolkit.key_binding.vi_state import InputMode, ViState


def get_input_mode(self):
    return self._input_mode


def set_input_mode(self, mode):
    shape = {InputMode.NAVIGATION: 2, InputMode.REPLACE: 4}.get(mode, 6)
    raw = u'\x1b[{} q'.format(shape)
    if hasattr(sys.stdout, '_cli'):
        out = sys.stdout._cli.output.write_raw
    else:
        out = sys.stdout.write
    out(raw)
    sys.stdout.flush()
    self._input_mode = mode


ViState._input_mode = InputMode.INSERT
ViState.input_mode = property(get_input_mode, set_input_mode)
c.TerminalInteractiveShell.editing_mode = 'vi'

Partly from https://github.com/jonathanslenders/python-prompt-toolkit/issues/192#issuecomment-380654372

Read more comments on GitHub >

github_iconTop Results From Across the Web

cursor shape (e.g. for vim mode) · Issue #192 · prompt-toolkit ...
I know the character sequences that change cursor shape for my terminal, ... Codes printed to terminal when using typeahead with FinalTerm sequences...
Read more >
The magical command line - Stubbornella
The command line makes poor use of a visual memory (though setting ... So, when I type git m , the magical command...
Read more >
Documentation - iTerm2 - macOS Terminal Replacement
To use autocomplete, type the beginning of a word and then press cmd-;. ... If enabled, escape codes that initiate printing will be...
Read more >
2_Linux Enviroment and command line
Linux is a Unix-like computer operating system assembled under the model of free and open source software development and distribution. The defining component ......
Read more >
Reference — prompt_toolkit 3.0.36 documentation
This is important on terminals that use vt100 input. We can't distinguish the ... This can be used for instance, for inserting Final...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found