From d85fab763bc3bb7867db436eacf229d5b9894c18 Mon Sep 17 00:00:00 2001 From: 0rax Date: Thu, 7 Nov 2013 00:23:54 +0100 Subject: [PATCH] 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) --- cheat | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/cheat b/cheat index 02fd7e0..fa19657 100755 --- a/cheat +++ b/cheat @@ -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):