-
-
Notifications
You must be signed in to change notification settings - Fork 207
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
Arbitrary Handlers (Feature) #90
Comments
If i understand correctly, you want an option like If so I agree fully! That would be a very useful feature! |
Hey @jacobian91, can you write a minimal example to see how this is working today? |
They're meaning, sending the progress bar to something instead of stdout. |
Yes, I understand. But I do not send to |
While, admittedly, not the most minimal of solutions this gives a good idea of what I'm trying to work with.
import asyncio
import sys
from contextlib import redirect_stdout, redirect_stderr
from prompt_toolkit import Application
from prompt_toolkit.application import get_app
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.key_binding.key_processor import KeyPressEvent
from prompt_toolkit.layout.containers import HSplit, Window
from prompt_toolkit.layout.controls import FormattedTextControl
from prompt_toolkit.layout.layout import Layout
# Notes
# A1: STDOUT is redirected to prevent UI corruption, but must be temporarily redirected
# back to the normal stdout for the command prompt to show the UI at all. Only the
# instantiation of the Application() objects need to be within the redirect context
# manager, the run() is not required.
# App Related Variables
loop = asyncio.get_event_loop()
progress_bar = FormattedTextControl()
kb = KeyBindings()
tui_layout = Layout(
HSplit(
[
Window(
height=2,
content=progress_bar,
style="bg:ansigray fg:ansiblack",
),
Window(), # Empty Space
Window(
height=1,
content=FormattedTextControl(text="ESC to Stop, Enter to add pipe."),
),
]
)
)
@kb.add("escape")
def exit_(event: KeyPressEvent):
event.app.exit()
@kb.add("enter")
def exit_scan_(event: KeyPressEvent):
progress_bar.text += "|"
async def progress_add():
while True:
progress_bar.text += "."
get_app().invalidate() # Redraw
await asyncio.sleep(0.5)
def draw_app():
with redirect_stdout(sys.__stdout__): # See Note A1
app = Application(key_bindings=kb, layout=tui_layout, full_screen=True)
_, f_pend = loop.run_until_complete(
asyncio.wait(
[
app.run_async(),
progress_add(),
],
return_when=asyncio.FIRST_COMPLETED,
)
)
f_pend.pop().cancel()
def main():
# Prevent UI Corruption, writes to file instead of terminal
with redirect_stderr(open("stderr.log", "a", encoding="utf-8")), redirect_stdout(
open("stdout.log", "a", encoding="utf-8")
):
draw_app()
if __name__ == "__main__":
main() |
Wow, very cool! I've never seen anything like it before. |
The |
Hello @jacobian91, |
Hi @rsalmei, I was trying to test this today but I don't see the branch where this code is implemented, could you point me in the right direction? |
Ohh, it isn't committed just yet... I got blocked by some other tasks and couldn't find the time to. |
@jacobian91 Until it's committed/released, I have a branch that implements it at https://github.com/aerickson/alive-progress/tree/file_as_argument. |
Hy @jacobian91, I'm committing the code! |
Tag me when it is committed or post here again and I'll take a look for sure! |
@rsalmei could you provide an example of how the new implementation works with a different type of text io? This is what I tried and got. The first section where I use alive_progress I try to print the value of the string object each loop and it shows empty during the loops, but after leaving the ap context manager, the stringio text is not empty. In the second section I just used a for loop as a proof to myself that StringIO can be updated during a for-loop. import io
import time
import alive_progress
ap_string = io.StringIO()
with alive_progress.alive_bar(10, file=ap_string) as bar:
for i in range(10):
bar()
time.sleep(0.1)
print(bar.current, "=", ap_string.getvalue())
print(bar.current, "=", ap_string.getvalue())
stringio = io.StringIO()
for i in range(10):
stringio.write(f" {i}")
time.sleep(0.1)
print(i, "=", stringio.getvalue())
print(i, "=", stringio.getvalue())
|
Hey, try with |
We are using prompt_toolkit and using layout
prompt_toolkit.layout.containers
that holdprompt_toolkit.layout.controls.FormattedTextControl
. It would be great to send the progress bar to one of these controls instead of going only to stdout. Right now, sending it to stdout corrupts the prompt-toolkit layout.The text was updated successfully, but these errors were encountered: