tree refactoring

This commit is contained in:
Amber 2024-01-24 14:46:00 +01:00
parent 838ac16bc6
commit c2c0f3ec3c
2 changed files with 43 additions and 14 deletions

View File

@ -2,16 +2,24 @@ from colors import RED, YELLOW, GREEN, CYAN , BLUE, PURPLE
from tree import NerdTree
class NerdTreeFind(NerdTree):
'''
@param startpath string: the startpath from which to generate
the json_tree member (the tree representation)
@param opts dict: the opts for the tree representation
'''
def __init__(self, startpath, item_name, opts={}):
self.item_name = item_name
def __init__(self, startpath, opts={}):
self.item_name = ''
self.results = []
## computed tree for find
self.json_tree_find = {}
NerdTree.__init__(self, startpath, opts=opts)
def find_node_recursively(self, node):
if node['name'] == self.item_name:
node.update({'color_formatter' : YELLOW})
self.results.append(node)
node_copy = node.copy()
node_copy.update({'color_formatter' : YELLOW})
self.results.append(node_copy)
children = node.get('children') or []
@ -20,8 +28,9 @@ class NerdTreeFind(NerdTree):
self.find_node_recursively(item_child)
else:
if item_child['name'] == self.item_name:
item_child.update({'color_formatter' : YELLOW})
self.results.append(item_child)
item_child_copy = item_child.copy()
item_child_copy.update({'color_formatter' : YELLOW})
self.results.append(item_child_copy)
def find_node(self):
self.find_node_recursively(self.json_tree)
@ -49,12 +58,12 @@ class NerdTreeFind(NerdTree):
return (False, [])
def find_tree_struct(self, item_name):
def find_tree_struct(self):
'''
'''
results = self.find_node()
dont_show_children_nodes = self.opts['dont_show_children_nodes'] if self.opts.get('dont_show_children_nodes') else False
dont_show_children_nodes = self.find_opts['dont_show_children_nodes'] if self.find_opts.get('dont_show_children_nodes') else False
if not results:
return {}
@ -94,11 +103,31 @@ class NerdTreeFind(NerdTree):
return tree_struct
def print(self):
self.tree_struct = tree_struct = self.find_tree_struct(self.item_name)
if not tree_struct:
print('No results')
return
def reset(self, new_item_name, new_opts):
self.find_opts = new_opts
self.results = []
self.item_name = new_item_name
self.json_tree_find = {}
print(self.tree_from_struct(self.tree_struct))
def print(self, item_name='', opts={}):
if not item_name and not self.item_name:
print('Please use a valid search string')
return
if item_name != self.item_name or self.find_opts != opts:
self.reset(item_name, opts)
self.json_tree_find = self.find_tree_struct()
if not self.json_tree_find:
print('No results')
return
print(self.tree_from_struct(self.json_tree_find))
def find(self, item_name, opts={}):
'''
@param item_name the string to search in the tree
@param opts dict for the find option
'''
return self.print(item_name, opts=opts)