Skip to content

Commit

Permalink
first version, copied from pallets/click#649
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoav Ram committed Sep 4, 2016
0 parents commit 019cf1e
Show file tree
Hide file tree
Showing 10 changed files with 2,416 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
click_spinner/_version.py export-subst
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.idea
.DS_Store
*.pyc
*.pyo
*.egg-ignore
*.egg-info
dist
build
docs/_build
click.egg-info
.tox
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include versioneer.py
include click_spinner/_version.py
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Click Spinner

Sometimes you would just like to show the user some progress,
but a progress bar is not suitable because you don’t know how much longer it would take.
In these cases you might want to display a simple spinner using the `spinner()` function.

Example usage:

```py
with click_spinner.spinner():
do_something()
do_something_else()
```

It looks like this:

![spinner](https://cloud.githubusercontent.com/assets/1288133/18229827/29629cd4-728f-11e6-8007-6c85ac50565c.gif)

Spinner class based on on a [gist by @cevaris](https://gist.github.com/cevaris/79700649f0543584009e).

Introduced in [PR #649](https://github.com/pallets/click/pull/649).

## Install

```
pip install click-spinner
```

## Authors

- Yoav Ram (@yoavram)
53 changes: 53 additions & 0 deletions click_spinner/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from builtins import next
from builtins import object
import sys
import threading
import time
import itertools


class Spinner(object):
spinner_cycle = itertools.cycle(['-', '/', '|', '\\'])

def __init__(self):
self.stop_running = threading.Event()
self.spin_thread = threading.Thread(target=self.init_spin)

def start(self):
self.spin_thread.start()

def stop(self):
self.stop_running.set()
self.spin_thread.join()

def init_spin(self):
while not self.stop_running.is_set():
sys.stdout.write(next(self.spinner_cycle))
sys.stdout.flush()
time.sleep(0.25)
sys.stdout.write('\b')

def __enter__(self):
self.start()

def __exit__(self, exc_type, exc_val, exc_tb):
self.stop()


def spinner():
"""This function creates a context manager that is used to display a
spinner as long as the context has not exited.
Example usage::
with spinner():
do_something()
do_something_else()
"""
return Spinner()


from ._version import get_versions
__version__ = get_versions()['version']
del get_versions
Loading

0 comments on commit 019cf1e

Please sign in to comment.