mirror of https://github.com/cheat/cheat.git
add columnized output
This commit is contained in:
parent
824e3dd99c
commit
eed51ae5de
54
cheat
54
cheat
|
@ -1,32 +1,46 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
from itertools import izip_longest, islice
|
||||||
|
|
||||||
# assemble a keyphrase out of all params passed to the script
|
def columnize(sequence, columns=2):
|
||||||
keyphrase = ' '.join(sys.argv[1:])
|
size, remainder = divmod(len(sequence), columns)
|
||||||
cheat_dir = os.path.expanduser('~') + '/.cheat/'
|
if remainder:
|
||||||
|
size += 1
|
||||||
|
slices = [islice(sequence, pos, pos + size)
|
||||||
|
for pos in xrange(0, len(sequence), size)]
|
||||||
|
return izip_longest(fillvalue='', *slices)
|
||||||
|
|
||||||
# verify that the ~/.cheat directory exists
|
def main():
|
||||||
if not os.path.isdir(cheat_dir):
|
# assemble a keyphrase out of all params passed to the script
|
||||||
print >> sys.stderr, 'The ~/.cheat directory does not exist.'
|
keyphrase = ' '.join(sys.argv[1:])
|
||||||
|
cheat_dir = os.path.expanduser('~') + '/.cheat/'
|
||||||
|
|
||||||
|
# verify that the ~/.cheat directory exists
|
||||||
|
if not os.path.isdir(cheat_dir):
|
||||||
|
sys.stderr.write('The ~/.cheat directory does not exist.')
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
# list the files in the ~/.cheat directory
|
# list the files in the ~/.cheat directory
|
||||||
cheatsheets = os.listdir(cheat_dir)
|
cheatsheets = os.listdir(cheat_dir)
|
||||||
cheatsheets.sort()
|
cheatsheets.sort()
|
||||||
|
|
||||||
# print help if requested
|
# print help if requested
|
||||||
if keyphrase in ['', 'help', '--help', '-h']:
|
if keyphrase in ['', 'help', '--help', '-h']:
|
||||||
print "Usage: cheat [keyphrase]\n"
|
print("Usage: cheat [keyphrase]\n")
|
||||||
print "Available keyphrases:"
|
print("Available keyphrases:")
|
||||||
print "\n".join(cheatsheets)
|
for values in columnize(cheatsheets, 4):
|
||||||
|
print(" %s" % ' '.join(value.ljust(20) for value in values))
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
# print the cheatsheet if it exists
|
# print the cheatsheet if it exists
|
||||||
if keyphrase in cheatsheets:
|
if keyphrase in cheatsheets:
|
||||||
with open (cheat_dir + keyphrase, 'r') as cheatsheet:
|
with open (cheat_dir + keyphrase, 'r') as cheatsheet:
|
||||||
print cheatsheet.read()
|
print(cheatsheet.read())
|
||||||
|
|
||||||
# if it does not, say so
|
# if it does not, say so
|
||||||
else:
|
else:
|
||||||
print 'No cheatsheet found.'
|
print('No cheatsheet found.')
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in New Issue