{"id":17151228,"url":"https://github.com/soda480/single-line","last_synced_at":"2025-03-30T15:45:25.886Z","repository":{"id":250496331,"uuid":"834549539","full_name":"soda480/single-line","owner":"soda480","description":"A context manager to facilitate printing messages to the same line.","archived":false,"fork":false,"pushed_at":"2024-07-28T03:10:15.000Z","size":295,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-22T19:05:54.678Z","etag":null,"topics":["asyncio","colorama","context-manager","docker","pybuilder-example","python","terminal"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/soda480.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-27T15:54:55.000Z","updated_at":"2024-07-28T03:09:04.000Z","dependencies_parsed_at":"2024-07-27T22:02:20.769Z","dependency_job_id":"23b5b577-5afd-47cb-acfc-808f70b1d447","html_url":"https://github.com/soda480/single-line","commit_stats":null,"previous_names":["soda480/single-line"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soda480%2Fsingle-line","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soda480%2Fsingle-line/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soda480%2Fsingle-line/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soda480%2Fsingle-line/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/soda480","download_url":"https://codeload.github.com/soda480/single-line/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246342817,"owners_count":20761939,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asyncio","colorama","context-manager","docker","pybuilder-example","python","terminal"],"created_at":"2024-10-14T21:37:32.890Z","updated_at":"2025-03-30T15:45:25.867Z","avatar_url":"https://github.com/soda480.png","language":"Python","readme":"# single-line\n[![build](https://github.com/soda480/single-line/actions/workflows/main.yml/badge.svg)](https://github.com/soda480/single-line/actions/workflows/main.yml)\n[![coverage](https://img.shields.io/badge/coverage-100%25-brightgreen)](https://pybuilder.io/)\n[![complexity](https://img.shields.io/badge/complexity-A-brightgreen)](https://radon.readthedocs.io/en/latest/api.html#module-radon.complexity)\n[![vulnerabilities](https://img.shields.io/badge/vulnerabilities-None-brightgreen)](https://pypi.org/project/bandit/)\n[![PyPI version](https://badge.fury.io/py/single-line.svg)](https://badge.fury.io/py/single-line)\n[![python](https://img.shields.io/badge/python-3.8%20%7C%203.9%20%7C%203.10%20%7C%203.11%20%7C%203.12-teal)](https://www.python.org/downloads/)\n\nA context manager to facilitate printing messages to the same line.\n\n## Installation\n```bash\npip install single-line\n```\n\n## Usage\n\nUsing the `SingleLine` context manager all calls to its `write` method will print the message to the same line. A common use case is to use it in conjuction with a for loop.\n\n```Python\nfrom time import sleep\nfrom faker import Faker\nfrom single_line import SingleLine\n\nwith SingleLine() as line:\n    for _ in range(25):\n        line.write(Faker().sentence())\n        sleep(.15)\n```\n\n![example1](https://raw.githubusercontent.com/soda480/single-line/main/docs/images/example1.gif)\n\nSetting the `exit_message` parameter will print the designated message when the context exits. The `write` method also supports colored messages via the [colorama](https://pypi.org/project/colorama/) module (so long as the stream is interactive); pass an optional `color` parameter with a dictionary containing the `fore`, `back` and `style` values.\n\n```Python\nfrom time import sleep\nfrom faker import Faker\nfrom colorama import Fore\nfrom single_line import SingleLine\n\nwith SingleLine(exit_message='done') as line:\n    for _ in range(25):\n        line.write(Faker().sentence(), color={'fore': Fore.YELLOW})\n        sleep(.15)\n\n```\n\n![example2](https://raw.githubusercontent.com/soda480/single-line/main/docs/images/example2.gif)\n\nBy default messages will be printed out to the sys.stdout stream but you can designate sys.stderr by setting the `stream` parameter. Note if stream is not connected to an interactive terminal device 'SingleLine` will simply print the message, color and cursor directives will be ignored. This example also shows the extent of using colors when printing messages.\n\n```Python\nimport sys\nimport random\nfrom time import sleep\nfrom faker import Faker\nfrom single_line import SingleLine\nfrom colorama import Fore, Back, Style\n\ndef get_random_fore():\n    return random.choice([Fore.BLACK, Fore.RED, Fore.GREEN, Fore.YELLOW, Fore.BLUE, Fore.MAGENTA, Fore.CYAN, Fore.WHITE])\n\ndef get_random_back():\n    return random.choice([Back.BLACK, Back.RED, Back.GREEN, Back.YELLOW, Back.BLUE, Back.MAGENTA, Back.CYAN, Back.WHITE])\n\ndef get_random_style():\n    return random.choice([Style.NORMAL, Style.DIM, Style.BRIGHT])\n\nwith SingleLine(stream=sys.stderr) as line:\n    for _ in range(25):\n        line.write(Faker().sentence(), color={'fore': get_random_fore(), 'back': get_random_back(), 'style': get_random_style()})\n        sleep(.15)\n```\n\n![example3](https://raw.githubusercontent.com/soda480/single-line/main/docs/images/example3.gif)\n\nYou can also use the `SingleLine` context manager to display messages when executing [asyncio](https://docs.python.org/3/library/asyncio.html) methods.\n\n```Python\nimport asyncio\nimport random\nfrom faker import Faker\nfrom single_line import SingleLine\n\nasync def do_some_work(worker, fake, line):\n    for index in range(random.randint(10, 35)):\n        await asyncio.sleep(random.choice([.5, .1, .25]))\n        line.write(f'worker{worker} {fake.sentence()}')\n\nasync def run(line):\n    await asyncio.gather(*(do_some_work(worker, Faker(), line) for worker in range(5)))\n\nwith SingleLine(exit_message='done with asyncio') as line:\n    asyncio.run(run(line))\n```\n\n![example4](https://raw.githubusercontent.com/soda480/single-line/main/docs/images/example4.gif)\n\n## Development\n\nClone the repository and ensure the latest version of Docker is installed on your development server.\n\nBuild the Docker image:\n```sh\ndocker image build \\\n-t single-line:latest .\n```\n\nRun the Docker container:\n```sh\ndocker container run \\\n--rm \\\n-it \\\n-v $PWD:/code \\\nsingle-line:latest \\\nbash\n```\n\nExecute the build:\n```sh\npyb -X\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoda480%2Fsingle-line","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoda480%2Fsingle-line","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoda480%2Fsingle-line/lists"}