|
| 1 | +#!/usr/bin/env python |
| 2 | +""" |
| 3 | +Example of printing colored text to the output. |
| 4 | +""" |
| 5 | +import time |
| 6 | +import asyncio |
| 7 | +import concurrent.futures |
| 8 | +import multiprocessing |
| 9 | +from quo import print |
| 10 | +from quo.text import FormattedText |
| 11 | +from quo.style import Style |
| 12 | + |
| 13 | +async def main(): |
| 14 | + style = Style.add( |
| 15 | + { |
| 16 | + "hello": "#ff0066", |
| 17 | + "world": "#44ff44 italic", |
| 18 | + } |
| 19 | + ) |
| 20 | + |
| 21 | + # Print using a a list of text fragments. |
| 22 | + text_fragments = FormattedText( |
| 23 | + [ |
| 24 | + ("class:hello", "Hello "), |
| 25 | + ("class:world", "World"), |
| 26 | + ("", "\n"), |
| 27 | + ] |
| 28 | + ) |
| 29 | + async def ok(text_fragments, style): |
| 30 | + async with text_framents as txt: |
| 31 | + print(txt, style=style) |
| 32 | + |
| 33 | + async def oks(dits): |
| 34 | + tasks = [] |
| 35 | + task = asyncio.ensure_future(ok(text_fragments,style)) |
| 36 | + tasks.append(task) |
| 37 | + await asyncio.gather(*tasks, return_exceptions=True) |
| 38 | + |
| 39 | +def asyc_tasks(dits): |
| 40 | + asyncio.get_event_loop().run_until_complete(ok(dits)) |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | + # with multiprocessing.Pool() as pool: |
| 45 | + # pool.map(ok, dits) |
| 46 | + #print(text_fragments, style=style) |
| 47 | + |
| 48 | + # Print using an HTML object. |
| 49 | + print("<hello>hello</hello> <world>world</world>\n", style=style) |
| 50 | + |
| 51 | + # Print using an HTML object with inline styling. |
| 52 | + print('<style fg="#ff0066">hello</style> ' |
| 53 | + '<style fg="#44ff44"><i>world</i></style>\n' |
| 54 | + ) |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + start = time.time() |
| 58 | + with concurrent.futures.ProcessPoolExecutor(max_workers=5) as ex: |
| 59 | + futures = [ex.submit(asyc_tasks)] |
| 60 | + for future in concurrent.futures.as_completed(futures): |
| 61 | + pass |
| 62 | + duration = time.time() - start |
| 63 | + print(f"Duration {duration}") |
0 commit comments