Skip to content

Add options for prompt (fixes #283) #284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion jupyter_console/ptshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from zmq import ZMQError
from IPython.core import page
from IPython.core.interactiveshell import SeparateUnicode
from traitlets import (
Bool,
Integer,
Expand Down Expand Up @@ -325,6 +326,10 @@ class ZMQTerminalInteractiveShell(SingletonConfigurable):
config=True
)

separate_in = SeparateUnicode('\n').tag(config=True)
separate_out = SeparateUnicode('\n').tag(config=True)
separate_out2 = SeparateUnicode('\n').tag(config=True)

manager = Instance("jupyter_client.KernelManager", allow_none=True)
client = Instance("jupyter_client.KernelClient", allow_none=True)

Expand Down Expand Up @@ -398,6 +403,7 @@ def get_out_prompt_tokens(self):
]

def print_out_prompt(self):
sys.stdout.write(self.separate_out)
tokens = self.get_out_prompt_tokens()
print_formatted_text(PygmentsTokens(tokens), end='',
style = self.pt_cli.app.style)
Expand Down Expand Up @@ -636,7 +642,7 @@ def set_doc():

async def interact(self, loop=None, display_banner=None):
while self.keep_running:
print('\n', end='')
print(self.separate_in, end='')

try:
code = await self.prompt_for_code()
Expand Down Expand Up @@ -900,6 +906,7 @@ def handle_iopub(self, msg_id=''):
# For multi-line results, start a new line after prompt
print()
print(text_repr)
sys.stdout.write(self.separate_out2)

# Remote: add new prompt
if not self.from_here(sub_msg):
Expand Down
25 changes: 22 additions & 3 deletions jupyter_console/tests/test_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,26 @@ def test_console_starts():
p, pexpect, t = start_console()
p.sendline("5")
p.expect([r"Out\[\d+\]: 5", pexpect.EOF], timeout=t)
p.expect([r"In \[\d+\]", pexpect.EOF], timeout=t)
p.expect(["\n", "\n", r"In \[\d+\]", pexpect.EOF], timeout=t)
stop_console(p, pexpect, t)

@flaky
@pytest.mark.skipif(should_skip, reason="not supported")
def test_console_starts_nonl():
"""test that `jupyter console` starts a terminal"""
p, pexpect, t = start_console(['--ZMQTerminalInteractiveShell.separate_in=""'])
p.sendline("5")
p.expect([r"Out\[\d+\]: 5", pexpect.EOF], timeout=t)
p.expect([r"(?<!\n)\n", r"In \[\d+\]", pexpect.EOF], timeout=t)
stop_console(p, pexpect, t)

@pytest.mark.xfail(reason='testing the regex to p.expect',strict=True)
def test_console_starts_nonl_correct_matcher():
"""test that `jupyter console` starts a terminal"""
p, pexpect, t = start_console()
p.sendline("5")
p.expect([r"Out\[\d+\]: 5", pexpect.EOF], timeout=t)
p.expect([r"(?<!\n)\n", r"In \[\d+\]", pexpect.EOF], timeout=2)
stop_console(p, pexpect, t)

def test_help_output():
Expand Down Expand Up @@ -59,11 +78,11 @@ def stop_console(p, pexpect, t):
p.terminate()


def start_console():
def start_console(args=()):
"Start `jupyter console` using pexpect"
import pexpect

args = ['-m', 'jupyter_console', '--colors=NoColor']
args = ['-m', 'jupyter_console', '--colors=NoColor'] + list(args)
cmd = sys.executable
env = os.environ.copy()
env["JUPYTER_CONSOLE_TEST"] = "1"
Expand Down