-
Notifications
You must be signed in to change notification settings - Fork 732
How to pass formatted text to a buffer ? #711
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
Comments
Hi @superlevure,
It is very tricky indeed right now to display formatted text into a I know it's a workaround for now. I'm thinking about unifying this Thanks for asking the question. It's very valuable to know in what ways people are using prompt_toolkit. [1] https://github.com/jonathanslenders/pypager/blob/master/pypager/layout.py#L22 |
Thank you for your quick answer, import threading
import time
from prompt_toolkit import Application
from prompt_toolkit.layout.layout import Layout
from prompt_toolkit.layout.containers import Window
from prompt_toolkit.layout.controls import FormattedTextControl
from prompt_toolkit.key_binding import KeyBindings
bindings = KeyBindings()
@bindings.add("q", eager=True)
def _(event):
event.app.exit()
root_container = Window(content=FormattedTextControl(text="<b>Hello</b> world"))
app = Application(
layout=Layout(root_container), full_screen=True, key_bindings=bindings
)
def update_text():
while True:
root_container.content.text += " test"
time.sleep(1)
if __name__ == "__main__":
thread1 = threading.Thread(target=update_text)
thread1.daemon = True
thread1.start()
app.run() When you run the code, the content is not being updated unless you hit any key on the keyboard. I think there might be a bug here ? Anyway back to my problem, I managed to pass formatted text to a buffer thank to all the info you gave me, again thank you for that. Here is the processor I used in case someone is interested: class FormatText(Processor):
def apply_transformation(self, ti):
fragments = to_formatted_text(HTML(fragment_list_to_text(ti.fragments)))
return Transformation(fragments) I think it would be a good move indeed to unify |
Hi @superlevure, I guess you have to tell the application to redraw itself: from prompt_toolkit.application import get_app
get_app().invalidate() Place that in the |
It works indeed, thanks for the tip I was looking for this method for another issue for quite some time now |
Any updates on this? I'll be using the workaround for now, but having BufferControl and FormattedTextControl merged would be a nice addition. |
Just ran into a situation where having this more easily accessible would have been very beneficial. |
Hi everyone, I did not forget about this, but I've been really busy. I hope to get some time for this in the coming months. |
I am using the workaround which works ok. But the workaround messes up selecting text inside the buffer: You don't get any visual feedback of the actual text being selected. I am trying to figure out why exactly, but any help is appreciated ! |
I've also been struggling to get formatted text - this is such a great resource @jonathanslenders it would be awesome to have formatted text on top of it all. I've successfully integrated @superlevure 's modifications without error but the text is still being displayed raw instead of with the colours etc. Is there any workaround recommended to try? I have this:
Where FormatText is copied from @superlevure 's comment above. |
@mashaklzo I've used this method in my custom client for Intercept: martmists/intercept_python_client
|
@Martmists thanks for your input. I was able to get this working, appended to my code snippet above:
|
Hi, we ended up using this solution for Lira. |
@stsewd I ran into this and tried to use your solution. However it doesn't seem to support insert_text calls with formatted text? How do you append new lines of formatted text to the end of the buffer as they happen? |
@jonathanslenders As 6 years have passed, I'm guessing you didn't find time for this? |
Hi,
After hours of reading the doc/code I still cannot find a way to use a buffer object with formatted text, let me explain:
In a minimalistic full screen application, I have a Window which consist in a scrollable text. Its content is being updated by another thread using buffer.insert_text() method.
I've tried a lot of solutions, even using processor, but cannot achieve to print coloured text (using HTML syntax for example)
I also tried to use a FormattedTextControl instead of a BufferControl, HTML works that way but then after the content is set at declaration, I cannot find a way to update it later.
I think I'm missing something, any clues ?
Thank you for your help
The text was updated successfully, but these errors were encountered: