diff --git a/orvani.py b/orvani.py index 4d14cd0..fbf4913 100644 --- a/orvani.py +++ b/orvani.py @@ -214,7 +214,7 @@ class OrvaniTag: rpath = os.path.join(root, dir) self.purge(rpath, False) - def list(self, search: str, path: str='./', filter=None, exact=True, recursive: bool=True, cache: bool=False): + def list(self, path: str='./', filter=None, exact=True, recursive: bool=True, cache: bool=False, *search: str): """ Returns an index object with query results and optionally saves a cached index @@ -255,20 +255,49 @@ class OrvaniTag: 'index' : self.INDEX, 'found' : [] } + query = self.__context(search) + ret = self.__context([' ']) + ret['note'] = [] for ctx in self.CTX.keys(): for context in self.INDEX[ctx]: if filter and not filter == ctx: continue + for q in query[ctx]: + if exact: + if q == context: + if self.INDEX[ctx][context] not in ret[ctx]: + ret[ctx] += self.INDEX[ctx][context] + else: + if q in context: + if self.INDEX[ctx][context] not in ret[ctx]: + ret[ctx] += self.INDEX[ctx][context] if exact: - if search == context: - if self.INDEX[ctx][context] not in result['found']: - result['found'] += self.INDEX[ctx][context] + if 'note' in query and query['note'] == context: + if self.INDEX[ctx][context] not in ret['note']: + ret['note'] += self.INDEX[ctx][context] else: - if search in context: - if self.INDEX[ctx][context] not in result['found']: - result['found'] += self.INDEX[ctx][context] - result['found'] = sorted(list(set(result['found'])), key=lambda x: x.count('/')) + if 'note' in query and query['note'] in context: + if self.INDEX[ctx][context] not in ret['note']: + ret['note'] += self.INDEX[ctx][context] + ret[ctx] = list(set(ret[ctx])) + ret['note'] = list(set(ret['note'])) + if len(search) > 1: + result['found'] = sorted(list(set(self.__intersect(ret))), key=lambda x: x.count('/')) + else: + min = [] + for sublist in ret.values(): + min += sublist + result['found'] = min return result + + def __intersect(self, result: dict): + mresult = {key: value for key, value in result.items() if value} + if len(mresult) < 2: + return [] + common_elements = set(mresult[next(iter(mresult))]) + for key in mresult.keys(): + common_elements = common_elements.intersection(mresult[key]) + return list(common_elements) def __indexfolder(self, sub: str): """ @@ -292,27 +321,30 @@ class OrvaniTag: self.INDEX[ctx][item] = filelist def mktable(query: str, index: dict): - table = Table(title=f"Results for `{query}`") - table.add_column("Path", justify="right", style="white", no_wrap=True) - table.add_column("Note", style="#cccccc") - table.add_column("Projects", justify="center", style="cyan") - table.add_column("Contexts", justify="center", style="magenta") - table.add_column("Tags", justify="center", style="green") - note = tags = projects = contexts = tags = '' - for file in index['found']: - path = file - fc = index['index']['files'][file] - if 'note' in fc: - note = fc['note'] - else: - note = '' - tags = ' '.join(['#' + tag for tag in fc['tags']]) - projects = ' '.join(['+' + p for p in fc['projects']]) - contexts = ' '.join(['@' + c for c in fc['contexts']]) - table.add_row(path, note, projects, contexts, tags) - console = Console() - console.print(table) + if len(index['found']) > 0: + table = Table(title=f"Results for `{query}`") + table.add_column("Path", justify="right", style="white", no_wrap=True) + table.add_column("Note", style="#cccccc") + table.add_column("Projects", justify="center", style="cyan") + table.add_column("Contexts", justify="center", style="magenta") + table.add_column("Tags", justify="center", style="green") + note = tags = projects = contexts = tags = '' + for file in index['found']: + path = file + fc = index['index']['files'][file] + if 'note' in fc: + note = fc['note'] + else: + note = '' + tags = ' '.join(['#' + tag for tag in fc['tags']]) + projects = ' '.join(['+' + p for p in fc['projects']]) + contexts = ' '.join(['@' + c for c in fc['contexts']]) + table.add_row(path, note, projects, contexts, tags) + console.print(table) + else: + console.print(f"No results found for `{query}`", style="red") + def main(): @@ -370,7 +402,7 @@ def main(): orvani.remove(args.file, *args.context) elif args.command in ['list', 'ls', 'dir', 'l', 'listproj', 'lsprj', 'lp', 'listcon', 'lsc', 'lc', 'listtag', 'lst', 'lt']: - query = args.file + query = [args.file] + args.context if args.command in ['listproj', 'lsprj', 'lp'] or args.context_filter == 'project': args.context_filter = 'projects' if args.command in ['listcon', 'lsc', 'lc'] or args.context_filter == 'context': @@ -378,7 +410,7 @@ def main(): if args.command in ['listtag', 'lst', 'lt'] or args.context_filter == 'tag': args.context_filter = 'tags' if query: - r = orvani.list(query, args.path, args.context_filter, args.exact, args.not_recursive, args.cache) + r = orvani.list(args.path, args.context_filter, args.exact, args.not_recursive, args.cache, *query) mktable(' '.join(sys.argv[1:]), r) elif args.command in ['copy', 'cp']: