From b1212052f7c6ebef10c1773035f6adb8aa06b1aa Mon Sep 17 00:00:00 2001 From: 0rax Date: Wed, 6 Nov 2013 00:36:49 +0100 Subject: [PATCH] Added search function into cheat, used a grep like output, if needed it could be changed, discussion is open inside #128 issue --- cheat | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/cheat b/cheat index 120b764..02fd7e0 100755 --- a/cheat +++ b/cheat @@ -65,6 +65,7 @@ class CheatSheets(object): dirs = None sheets = None + def __init__(self): self.dirs = self.__cheat_directories() # verify that we have at least one cheat directory @@ -178,6 +179,13 @@ class CheatSheets(object): return ('\n'.join(sorted(['%s [%s]' % (key.ljust(max_command), value) for key, value in self.sheets.items()]))) + 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, # Custom action for argparse class ListDirectories(argparse.Action): @@ -201,6 +209,13 @@ class EditSheet(argparse.Action): parser.exit() +class SearchSheet(argparse.Action): + """If the user wants to search a term inside all cheatsheets""" + def __call__(self, parser, namespace, values, option_string=None): + sheets.search(values[0]) + parser.exit() + + def main(): global sheets @@ -239,6 +254,9 @@ def main(): parser_group.add_argument('-e', '--edit', metavar='cheatsheet', action=EditSheet, type=str, nargs=1, help='Edit ') + parser_group.add_argument('-s', '--search', metavar='term', + action=SearchSheet, type=str, nargs=1, + help='Search inside all cheatsheets') parser_group.add_argument('-l', '--list', action=ListCheatsheets, nargs=0, help='List all available cheatsheets')