Skip to content

Instantly share code, notes, and snippets.

@pry0cc
Last active August 29, 2015 14:13
Show Gist options
  • Save pry0cc/7d41dc3e3091525aa6e1 to your computer and use it in GitHub Desktop.
Save pry0cc/7d41dc3e3091525aa6e1 to your computer and use it in GitHub Desktop.
The Dysfunctional Progress Bar - A progress bar, by me. xD
#!/usr/bin/env python
import time
import sys
import os
#################### By Pry0cc - The Dysfunctional Progress bar ####################
### Credit from stack overflow - A function to get height and width of terminal window
def getTerminalSize():
env = os.environ
def ioctl_GWINSZ(fd):
try:
import fcntl, termios, struct, os
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ,
'1234'))
except:
return
return cr
cr = ioctl_GWINSZ(0) or ioctl_GWINSZ(1) or ioctl_GWINSZ(2)
if not cr:
try:
fd = os.open(os.ctermid(), os.O_RDONLY)
cr = ioctl_GWINSZ(fd)
os.close(fd)
except:
pass
if not cr:
cr = (env.get('LINES', 25), env.get('COLUMNS', 80))
return int(cr[1]), int(cr[0])
### Set the variables accordingly
(term_width, height) = getTerminalSize()
### Main function to draw and print bar
def run_bar(message, prog_time, progress_pointer, width):
if (width < 26):
print("26 is the minimum size...")
print("Continuing with %s instead of %s" % (term_width, width))
width = term_width
## Initializations
width += -1; background_bar = ""; bar=""; incomplete_char = "-"; i = 0; b = 0; percentage = " " * 5
## Configures the time taken for each section movement.
bar_interval = prog_time / width
# Background Bar
## Draw background bar ("scaffolding")
while b < width:
b+=1
background_bar += incomplete_char
if (b == width ):
background_bar += "]"
## Define moving progress bar length (what it moves to)
if ( len(progress_pointer) == 1):
progress_bar_len = width - len(message) - len(percentage)
else:
progress_bar_len = (width - len(message) - len(percentage)) / len(progress_pointer)
# Foreground bar
## Draw and print progress bar
while i < progress_bar_len:
## Increment i by 1
i+=1
## Construct percentage of progress bar completed
percentage = str(round((i / progress_bar_len) * 100)) + "% "
## Create text to go before progress bar
text = percentage + message + " ["
## Create actual bar
bar = text + bar[len(text):] + progress_pointer
## Print it!
sys.stdout.write("%s\r%s\r" % (background_bar, bar))
sys.stdout.flush()
time.sleep(bar_interval)
print("")
## Uncomment for bar demonstration
run_bar("Client: Hacking US Government", 5, "#", term_width)
time.sleep(4)
print("Client: COMPLETE")
print("Client: READY FOR VIRUS UPLOAD")
time.sleep(1)
run_bar("Server: Uploading Virus", 10, "*", term_width)
time.sleep(4)
run_bar("Server: Configuring backdoor", 4, "*", term_width)
run_bar("Server: Clearing event logs", 2, "*", term_width)
run_bar("Server: Killing antivirus", 3, "*", term_width)
print("")
time.sleep(1)
run_bar("Client: Scanning for files...", 8, ">", term_width)
time.sleep(3)
print("Client: SECRET FILES FOUND")
time.sleep(1)
print("Client: INITIATING DOWNLOADS")
time.sleep(2)
run_bar("Server: Downloading Files", 18, "#", term_width)
time.sleep(4)
print("Client: FILES ENCRYPTED. ATTEMPT TO DECRYPT?")
input(">> ")
time.sleep(2)
run_bar("Client: SCANNING FILES", 5, ">", term_width)
print("FATAL ERROR")
time.sleep(2)
a = 0
while a < 1000:
a+=1
sys.stdout.write("FATAL ERROR *(& *& ")
sys.stdout.write(" *(&(*&^%*()) *ahs d jdsal &*( ()*&^%$£ jalskd &*^%^^*& adls& ")
sys.stdout.write("109381830810983098498210938 10928 \r 109283 012830 ")
sys.stdout.flush()
time.sleep(0.002)
print("\n")
time.sleep(1)
print("ERROR: ")
time.sleep(3)
print("SYSTEM BREACH")
ghf = "Message from system: Moahahaha I see you've been a naughty boy. Don't try and hack me again."
words = ghf.split()
for x in words:
sys.stdout.write(x + " ")
sys.stdout.flush()
time.sleep(0.3)
input("...")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment