From f16897721fc7afe198429dfea8a9f745696eb373 Mon Sep 17 00:00:00 2001 From: Amber Date: Mon, 5 Feb 2024 10:10:00 +0100 Subject: [PATCH] adding type search --- src/cmdline_parser.py | 1 + src/tree_find.py | 15 +++++++++++++-- vfind.py | 3 ++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/cmdline_parser.py b/src/cmdline_parser.py index 4d6578e..7c5bc1d 100644 --- a/src/cmdline_parser.py +++ b/src/cmdline_parser.py @@ -9,6 +9,7 @@ cmd_parser = argparse.ArgumentParser( cmd_parser.add_argument("path") cmd_parser.add_argument("pattern") +cmd_parser.add_argument("-t", "--type") # if -s is found in the command line cmd_args.show_children_nodes is True cmd_parser.add_argument("-s", "--show-children-nodes", action="store_true") ## more than one occurrencies of -e option are appended in a list diff --git a/src/tree_find.py b/src/tree_find.py index cbcdda5..165189b 100644 --- a/src/tree_find.py +++ b/src/tree_find.py @@ -19,9 +19,19 @@ class NerdTreeFind(NerdTree): self.find_opts = find_opts NerdTree.__init__(self, startpath, opts=opts) + def found_true_cond(self, test_node): + search_type = self.find_opts['type'] if self.find_opts.get('type') else None + + if not search_type: test_type = True + else: test_type = (test_node['type'] == search_type) + + test_match = True if self.re_item_name.match(test_node['name']) else False + return all([test_match, test_type]) + def find_node_recursively(self, node): # if node['name'] == self.item_name: - if self.re_item_name.match(node['name']): + # if self.re_item_name.match(node['name']): + if self.found_true_cond(node): node_copy = node.copy() node_copy.update({'color_formatter' : YELLOW, 'found' : 1}) self.results.append(node_copy) @@ -33,7 +43,8 @@ class NerdTreeFind(NerdTree): self.find_node_recursively(item_child) else: # if item_child['name'] == self.item_name: - if self.re_item_name.match(item_child['name']): + # if self.re_item_name.match(item_child['name']): + if self.found_true_cond(item_child): item_child_copy = item_child.copy() item_child_copy.update({'color_formatter' : YELLOW, 'found' : 1}) self.results.append(item_child_copy) diff --git a/vfind.py b/vfind.py index 9b4aaa5..5baa88b 100755 --- a/vfind.py +++ b/vfind.py @@ -17,7 +17,8 @@ if __name__ == '__main__': # opts for find find_opts = { - 'dont_show_children_nodes': not show_children_nodes + 'dont_show_children_nodes': not show_children_nodes, + 'type' : cmd_args.type, } ntree = NerdTreeFind(path, opts=opts)