Skip to content

Semantic prompt #58

@KoviRobi

Description

@KoviRobi

Hi,

I enjoy PyREPL in Python 3.13, thank you it's great! Especially that now it means python -m asyncio has a proper REPL. I was thinking it could do with semantic prompt integration (e.g. I have C-b C-p set up in Tmux to select the previous output).

To achieve this, I put the following into my PYTHONSTARTUP

import sys

def semantic_displayhook(obj, displayhook=sys.displayhook):
    print("\x1b]133;C\x07", end="", flush=True)
    displayhook(obj)
    print("\x1b]133;D;0\x07\x1b]N;aid=python;cl=v\x07", end="", flush=True)

def semantic_excepthook(exc, val, tb, excepthook=sys.excepthook):
    print("\x1b]133;C\x07", end="", flush=True, file=sys.stderr)
    excepthook(exc, val, tb)
    print("\x1b]133;D;1\x07\x1b]N;aid=python;cl=v\x07", end="", flush=True, file=sys.stderr)

sys.displayhook = semantic_displayhook
sys.excepthook = semantic_excepthook

sys.ps1 = "\1\x1b]133;A;aid=python;cl=v\x07\2>>> \1\x1b]133;I\x07\2"
sys.ps2 = "\1\x1b]133;P;k=c\x07\2... \1\x1b]133;I\x07\2"

I thought I would put this here in case it can be added to defaults in case it's useful to others? I could open a PR if you prefer, looks like the hooks might fit into runcode and maybe get_prompt for the ps1/ps2?

Thank you,
Robert

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions