prompt-toolkit / ptpython

A better Python REPL
BSD 3-Clause "New" or "Revised" License
5.23k stars 281 forks source link

[bug] ptpython doesn't respect sys.displayhook #524

Closed Freed-Wu closed 1 year ago

Freed-Wu commented 1 year ago

This is rich.print:

screen-2023-01-09-20-11-51

This is ptpython's output (Colors -> Syntax highlighting = on)

screen-2023-01-09-20-12-36

Can ptpython use rich.print to highlight output? I think rich.print is better for some object, such as resnet18().

Related: https://github.com/Textualize/rich/issues/2749

TIA!

Freed-Wu commented 1 year ago

https://github.com/prompt-toolkit/python-prompt-toolkit/blob/master/src/prompt_toolkit/formatted_text/base.py#L75 should let to_formatted_text convert a string to correct formatted text from rich.

Freed-Wu commented 1 year ago

Reduce one level of indent of https://github.com/prompt-toolkit/ptpython/blob/master/ptpython/repl.py#L360-L362 can get: screen-2023-01-14-02-34-47 Although I think rich.print is more colorful.

Freed-Wu commented 1 year ago

If use sys.displayhook(result) to replace https://github.com/prompt-toolkit/ptpython/blob/master/ptpython/repl.py#L360-L362, it can work. Why not provide an expression to allow user to do it?

Freed-Wu commented 1 year ago

This can fix the problem in ~/.config/ptpython/config.py:

repl.show_result = sys.displayhook  # type: ignore