joouha / euporie

Jupyter notebooks in the terminal
https://euporie.readthedocs.io
MIT License
1.54k stars 36 forks source link

Help in integrating with editors (newlines are handled weirdly) #85

Closed IndianBoy42 closed 11 months ago

IndianBoy42 commented 11 months ago

I am trying to use euporie-console to have a nice graphical interactive python repl next to my editor (neovim).

I tried to use the Kitty terminals kitty @ send_text to send selected code to the terminal that euporie-console is running in, but I am having some problem with newlines. sending \r acts like hitting enter which starts running the code but the next line is already sent and simply gets appended to the current line. And somehow euporie ends up trying to run this concatenated line, resulting in a syntax error

import numpy as np
from matplotlib import pyplot as plt

ends up being sent as

import numpy as npfrom matplotlib import pyplot as plt
                  ^ lost newline

Is it possible to have newlines in a long sequence of text be handled properly? Text after a \r but before the current line has actually started finished executing should go into a new line, ideally in the same chunk

joouha commented 11 months ago

Hello,

That sounds like a pretty cool workflow!

You'll need to use bracketed paste to get this working:

  1. Send \cc to clear euporie's input box before sending anything
  2. Send \x1b[200~ before your code;
  3. Send your code;
  4. Send \x1b[201~ after your code;
  5. Send \x1b[13;5u to run the code (Ctrl-Enter).

\x1b[13;5u will always run whatever is entered, whereas \r will only run what is entered if it passes the kernel's code-completeness check.

For example:

kitty @ send-text --all --exclude-active '\cc\x1b[200~print(123)\nprint(456)\x1b[201~\x1b[13;5u'

image

I hope that you get it working!

IndianBoy42 commented 11 months ago

Thanks for the tip about bracketed paste! that works great.

You seem pretty knowledgeable so can I ask, is there a good utility/library for converting keypresses into those csi/escape codes? or somewhere to learn all the details necessary to make one myself?

joouha commented 11 months ago

The VT100 user guide always makes a nice bit of light reading: https://vt100.net/docs/vt100-ug/chapter3.html#S3.3

There's also a wikipedia page on control-codes which might be a bit more accessible: https://en.wikipedia.org/wiki/C0_and_C1_control_codes

You might want to read the wikipedia page on ansi escape sequences first for a bit of background: https://en.wikipedia.org/wiki/ANSI_escape_code#Fe_Escape_sequences

Here's a couple of pages on modern terminal keyboard input: http://www.leonerd.org.uk/hacks/fixterms/ https://sw.kovidgoyal.net/kitty/keyboard-protocol/

If you run stty raw -echo; cat -v, your terminal will print escaped key-press escape sequences.