2016-05-10 16:14:53 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2016-05-22 02:18:25 +00:00
|
|
|
'''
|
|
|
|
util.py: frequently used terminal and text processing utilities
|
|
|
|
copyright (c) 2016 ~endorphant (endorphant@tilde.town)
|
|
|
|
|
|
|
|
GNU GPL BOILERPLATE:
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
'''
|
|
|
|
|
2016-05-10 16:14:53 +00:00
|
|
|
import inflect
|
|
|
|
import time
|
2016-05-15 03:44:03 +00:00
|
|
|
import random
|
2016-05-15 04:19:05 +00:00
|
|
|
import colorama
|
|
|
|
|
|
|
|
colorama.init()
|
|
|
|
|
|
|
|
textcolors = [ colorama.Fore.RED, colorama.Fore.GREEN, colorama.Fore.YELLOW, colorama.Fore.BLUE, colorama.Fore.MAGENTA, colorama.Fore.WHITE, colorama.Fore.CYAN]
|
|
|
|
|
|
|
|
lastcolor = colorama.Fore.RESET
|
2016-05-10 16:14:53 +00:00
|
|
|
|
|
|
|
p = inflect.engine()
|
|
|
|
|
2016-05-15 04:19:05 +00:00
|
|
|
def set_rainbow():
|
2016-05-22 03:36:21 +00:00
|
|
|
'''
|
|
|
|
prints a random terminal color code
|
|
|
|
'''
|
|
|
|
|
2016-05-15 04:19:05 +00:00
|
|
|
global lastcolor
|
|
|
|
|
|
|
|
color = lastcolor
|
|
|
|
while color == lastcolor:
|
|
|
|
color = random.choice(textcolors)
|
|
|
|
|
|
|
|
lastcolor = color
|
|
|
|
|
|
|
|
print(color)
|
|
|
|
|
|
|
|
def reset_color():
|
2016-05-22 03:36:21 +00:00
|
|
|
'''
|
|
|
|
prints terminal color code reset
|
|
|
|
'''
|
|
|
|
|
2016-05-15 04:19:05 +00:00
|
|
|
print(colorama.Fore.RESET)
|
|
|
|
|
|
|
|
def attach_rainbow():
|
2016-05-22 03:36:21 +00:00
|
|
|
'''
|
|
|
|
returns a random terminal color code, presumably to be 'attached' to a string
|
|
|
|
'''
|
|
|
|
|
2016-05-15 04:19:05 +00:00
|
|
|
global lastcolor
|
|
|
|
|
|
|
|
color = lastcolor
|
|
|
|
while color == lastcolor:
|
|
|
|
color = random.choice(textcolors)
|
|
|
|
|
|
|
|
lastcolor = color
|
|
|
|
return color
|
|
|
|
|
|
|
|
def attach_reset():
|
2016-05-22 03:36:21 +00:00
|
|
|
'''
|
|
|
|
returns terminal color code reset, presumably to be 'attached' to a string
|
|
|
|
'''
|
|
|
|
|
2016-05-15 04:19:05 +00:00
|
|
|
return colorama.Style.RESET_ALL
|
|
|
|
|
|
|
|
def hilight(text):
|
2016-05-22 03:36:21 +00:00
|
|
|
'''
|
|
|
|
takes a string and highlights it on return
|
|
|
|
'''
|
|
|
|
|
2016-05-15 04:19:05 +00:00
|
|
|
return colorama.Style.BRIGHT+text+colorama.Style.NORMAL
|
|
|
|
|
2016-05-10 16:14:53 +00:00
|
|
|
def pretty_time(time):
|
2016-05-22 03:36:21 +00:00
|
|
|
'''
|
|
|
|
human-friendly time formatter
|
|
|
|
|
|
|
|
takes an integer number of seconds and returns a phrase that describes it,
|
|
|
|
using the largest possible figure, rounded down (ie, time=604 returns '10
|
|
|
|
minutes', not '10 minutes, 4 seconds' or '604 seconds')
|
|
|
|
'''
|
|
|
|
|
2016-05-10 16:14:53 +00:00
|
|
|
m, s = divmod(time, 60)
|
|
|
|
if m > 0:
|
|
|
|
h, m = divmod(m, 60)
|
|
|
|
if h > 0:
|
|
|
|
d, h = divmod(h, 24)
|
|
|
|
if d > 0:
|
|
|
|
w, d = divmod(d, 7)
|
|
|
|
if w > 0:
|
|
|
|
mo, w = divmod(w, 4)
|
|
|
|
if mo > 0:
|
|
|
|
return p.no("month", mo)
|
|
|
|
else:
|
|
|
|
return p.no("week", w)
|
|
|
|
else:
|
|
|
|
return p.no("day", d)
|
|
|
|
else:
|
|
|
|
return p.no("hour", h)
|
|
|
|
else:
|
|
|
|
return p.no("minute", m)
|
|
|
|
else:
|
|
|
|
return p.no("second", s)
|
2016-05-15 03:44:03 +00:00
|
|
|
|
|
|
|
def genID(digits=5):
|
2016-05-22 03:36:21 +00:00
|
|
|
'''
|
|
|
|
returns a string-friendly string of digits, which can start with 0
|
|
|
|
'''
|
2016-05-15 03:44:03 +00:00
|
|
|
|
|
|
|
id = ""
|
|
|
|
x = 0
|
|
|
|
while x < digits:
|
|
|
|
id += str(random.randint(0,9))
|
|
|
|
x += 1
|
|
|
|
|
|
|
|
return id
|