Following #128 talk about how output should be made for search. Just changed the way on how the search function output the result, no more ala grep output.

Cheatsheets are now parsed [by CheatSheets.__parse_cheat_command_block(self, cheat_fp)] into block (separated by newline), i have seen that all block in cheatsheets are delimited by a blank line, so instead of parsing from first consecutive # to last consecutive command, an output that is not used by all sheets (reference to "7z" cheatfile).
And so the block are parsed by begin of the document to blanck line to end of the document.
Finally the output is made by indenting the block content by 4 spaces + the title of the sheet on the top.
This is a way to handle subcommands in my mind (search "git commit" and you now have all what you want)
This commit is contained in:
0rax 2013-11-07 00:23:54 +01:00
parent b1212052f7
commit d85fab763b
1 changed files with 19 additions and 3 deletions

22
cheat
View File

@ -179,13 +179,29 @@ class CheatSheets(object):
return ('\n'.join(sorted(['%s [%s]' % (key.ljust(max_command), value)
for key, value in self.sheets.items()])))
def __parse_cheat_command_block(self, cheat_fp):
block = ""
for line in cheat_fp.readlines():
if line == '\n' or line == '':
yield block
block = ""
else:
block += line
def search(self, term):
for sheet, sheet_dir in self.sheets.iteritems():
path = os.path.join(sheet_dir, sheet)
with open(path) as f:
for line in f.readlines():
if term in line:
print sheet + ':', line,
output = ''
for block in self.__parse_cheat_command_block(f):
if term in block:
if not output:
output = sheet + ":\n"
output += ''.join([" " + line + '\n' for line
in block.split('\n')])
if output:
print output,
# Custom action for argparse
class ListDirectories(argparse.Action):