Skip to content

Commit

Permalink
No submodule anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Brunel committed May 2, 2020
1 parent da7f57e commit 7e40168
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ProgressBar.info
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This is tag 1.0 of git project https://github.com/oldabl/python-progress-bar.git

106 changes: 106 additions & 0 deletions ProgressBar/ProgressBar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import time, sys, os

# TO USE IN YOUR CODE
#from folder.ProgressBar import ProgressBar

class ProgressBar:
def __init__(self,
pretext=r"", # Text to print before the bar
progresschar=r"█", # Character to show progress
loadingchars=r"█▓▒░▒▓", # Last character of bar moving as bar loads (moves even if no progress)
startendchar=r"||", # Characters going around the bar
barwidth=int(os.get_terminal_size().columns/2), # Length of the bar in characters (does not include what's around the bar)
displaypercentage=False, # Show percentage as well or not
displaycount=False # Show count as well or not
):
self.pretext = str(pretext)
self.progresschar = str(progresschar)
self.loadingchars = loadingchars
self.startendchar = str(startendchar)
self.barwidth = int(barwidth)
self.displaypercentage = displaypercentage
self.displaycount = displaycount

# loadingchars ideas
# characters = "-/|\"
# characters = ["-_="]

# Private
self.loadingcharsindex = 0
self.firstprint = True


# Role : print the progress bar as an independent thread
# Arguments:
# number: progress value (type multiprocessing.Value of int)
# max: value to reach (int)
# updateperiod: refresh period of progress bar in seconds
# Example:
# number = multiprocessing.Value("i", 0)
# max = 30
# progressbar = ProgressBar()
#-> multiprocessing.Process(target=progressbar.inThread, args=(number,max,0.1))
# for i in range(0,max)
# number.value = i
# time.sleep(1)
def inThread(self, number, max, updateperiod=0.1):
while(number.value != max):
self.print(number.value,max)
time.sleep(float(updateperiod))
self.print(max,max)


# Role : print the progress bar
# Arguments:
# number: progress value (int)
# max: maximum value (int)
# Example:
# max = 30
# progressbar = ProgressBar()
# for i in range(0,max):
#-> progressbar.print(i, max)
# time.sleep(1)
def print(self,number,max):
barstring = ""

# No carriage return on first print
if not self.firstprint:
barstring += "\r"
self.firstprint = False

# Pre progress bar
if self.pretext:
barstring += self.pretext

# Progress bar
#Start char
if self.startendchar:
barstring += self.startendchar[0]
#Current state of affairs
sofarbar = int( (number/max)*self.barwidth )
remainingbar = self.barwidth - sofarbar
#Add progress chars
barstring += sofarbar*self.progresschar
#If loading chars, print loading chars and go to next one (unless 100%)
if self.loadingchars != "" and number != max:
barstring += self.loadingchars[self.loadingcharsindex]
self.loadingcharsindex = (self.loadingcharsindex+1) % len(self.loadingchars)
remainingbar -= 1
#Add remaining gap
barstring += remainingbar*" "
#End char
if self.startendchar:
if len(self.startendchar) >= 2:
barstring += self.startendchar[1]
else:
barstring += self.startendchar[0]

# Post progress bar
if self.displaypercentage:
barstring += " %d%%" % int(number*100/max)
if self.displaycount:
barstring += " (%d/%d)" % (number,max)

# Print the bar out
sys.stdout.write(barstring)
sys.stdout.flush()
51 changes: 51 additions & 0 deletions ProgressBar/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Python Progress Bar
Prints an all customisable **easy-to-use** progress bar. It can be the thread target or a normal function call.


## Import in your code
```python
from *folderclone*.ProgressBar import ProgressBar
```
Now use as class ProgressBar.
/!\ folderclone should not contain character "-". Make sure not to clone in default git clone dir for this repo.


## Use
### As a simple function call
```python
max = 30
progressbar = ProgressBar()
for i in range(0,max):
progressbar.print(i,max)
time.sleep(1) # To see progress
```

### As a separate thread target
```python
max = 30
progressbar = ProgressBar()
number = multiprocessing.Value("i", 0)
multiprocessing.Process(target=progressbar.inThread, args=(number,max))
for i in range(0,max)
number.value = i
time.sleep(1) # To see progress
```


## Go further: customise your bar
### Style parameters
You can give the following optional arguments to the ProgressBar constructor:
- pretext: Text to print before the bar (default "")
- progresschar: Character to show progress (default '█')
- loadingchars: Last character of bar moving as bar loads (moves even if no progress) (default "█▓▒░▒▓")
- startendchar: Characters going around the bar (default "||")
- barwidth: Length of the bar in characters (does not include optionally printed pretext, progresschar, percentage and count) (default terminal width/2)
- displaypercentage: Show percentage as well or not (default False)
- displaycount: Show count as well or not (default False)

### Thread parameters
If you use the thread version, you can customise the refresh time of your progress bar.
To do this, give an extra argument (type float) representing the refreshing period of the bar.
```python
multiprocessing.Process(target=progressbar.inThread, args=(number,max,0.2))
```
Empty file added ProgressBar/__init__.py
Empty file.

0 comments on commit 7e40168

Please sign in to comment.