https://github.com/soda480/single-line
A context manager to facilitate printing messages to the same line.
https://github.com/soda480/single-line
asyncio colorama context-manager docker pybuilder-example python terminal
Last synced: about 1 year ago
JSON representation
A context manager to facilitate printing messages to the same line.
- Host: GitHub
- URL: https://github.com/soda480/single-line
- Owner: soda480
- Created: 2024-07-27T15:54:55.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-07-28T03:10:15.000Z (over 1 year ago)
- Last Synced: 2025-03-22T19:05:54.678Z (about 1 year ago)
- Topics: asyncio, colorama, context-manager, docker, pybuilder-example, python, terminal
- Language: Python
- Homepage:
- Size: 288 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# single-line
[](https://github.com/soda480/single-line/actions/workflows/main.yml)
[](https://pybuilder.io/)
[](https://radon.readthedocs.io/en/latest/api.html#module-radon.complexity)
[](https://pypi.org/project/bandit/)
[](https://badge.fury.io/py/single-line)
[](https://www.python.org/downloads/)
A context manager to facilitate printing messages to the same line.
## Installation
```bash
pip install single-line
```
## Usage
Using 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.
```Python
from time import sleep
from faker import Faker
from single_line import SingleLine
with SingleLine() as line:
for _ in range(25):
line.write(Faker().sentence())
sleep(.15)
```

Setting 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.
```Python
from time import sleep
from faker import Faker
from colorama import Fore
from single_line import SingleLine
with SingleLine(exit_message='done') as line:
for _ in range(25):
line.write(Faker().sentence(), color={'fore': Fore.YELLOW})
sleep(.15)
```

By 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.
```Python
import sys
import random
from time import sleep
from faker import Faker
from single_line import SingleLine
from colorama import Fore, Back, Style
def get_random_fore():
return random.choice([Fore.BLACK, Fore.RED, Fore.GREEN, Fore.YELLOW, Fore.BLUE, Fore.MAGENTA, Fore.CYAN, Fore.WHITE])
def get_random_back():
return random.choice([Back.BLACK, Back.RED, Back.GREEN, Back.YELLOW, Back.BLUE, Back.MAGENTA, Back.CYAN, Back.WHITE])
def get_random_style():
return random.choice([Style.NORMAL, Style.DIM, Style.BRIGHT])
with SingleLine(stream=sys.stderr) as line:
for _ in range(25):
line.write(Faker().sentence(), color={'fore': get_random_fore(), 'back': get_random_back(), 'style': get_random_style()})
sleep(.15)
```

You can also use the `SingleLine` context manager to display messages when executing [asyncio](https://docs.python.org/3/library/asyncio.html) methods.
```Python
import asyncio
import random
from faker import Faker
from single_line import SingleLine
async def do_some_work(worker, fake, line):
for index in range(random.randint(10, 35)):
await asyncio.sleep(random.choice([.5, .1, .25]))
line.write(f'worker{worker} {fake.sentence()}')
async def run(line):
await asyncio.gather(*(do_some_work(worker, Faker(), line) for worker in range(5)))
with SingleLine(exit_message='done with asyncio') as line:
asyncio.run(run(line))
```

## Development
Clone the repository and ensure the latest version of Docker is installed on your development server.
Build the Docker image:
```sh
docker image build \
-t single-line:latest .
```
Run the Docker container:
```sh
docker container run \
--rm \
-it \
-v $PWD:/code \
single-line:latest \
bash
```
Execute the build:
```sh
pyb -X
```