colorize find results and comments

This commit is contained in:
Amber 2024-01-18 21:41:19 +01:00
parent 1ecc40fddd
commit eea94fd4aa
2 changed files with 26 additions and 10 deletions

View File

@ -31,8 +31,12 @@ def produce_treeline(prec_seps, lsep):
for level_n, (sep, item) in enumerate(lseps):
tomount = sep
if item:
tomount = sep + item
if item['name']:
if item.get('color_formatter'):
item_name = item['color_formatter'](item['name'])
else:
item_name = item['name']
tomount = sep + item_name
tree_line = mount_str_at(tree_line, level2indent(level_n), tomount)
return tree_line.rstrip()
@ -72,7 +76,10 @@ def nerd_tree_from_struct(rootnode, prec_seps=[]):
## rootnode is always a dir -> colorize
rootnode_name = rootnode['name']
nerd_tree_txt = CYAN(rootnode_name)
if rootnode.get('color_formatter'):
nerd_tree_txt = rootnode['color_formatter'](rootnode_name)
else:
nerd_tree_txt = rootnode_name
items = rootnode.get('children') or []
@ -93,10 +100,10 @@ def nerd_tree_from_struct(rootnode, prec_seps=[]):
if item['type'] == 'd':
seps = prec_seps[:]
seps.append((psep_char, ''))
treeline = produce_treeline(prec_seps, (sep, ' ')) + ' ' + nerd_tree_from_struct(item, prec_seps=seps)
seps.append((psep_char, {'name' : ''}))
treeline = produce_treeline(prec_seps, (sep, {'name' : ' '})) + ' ' + nerd_tree_from_struct(item, prec_seps=seps)
else:
treeline = produce_treeline(prec_seps, (sep, item['name']))
treeline = produce_treeline(prec_seps, (sep, item))
nerd_tree_txt += '\n' + treeline
@ -171,8 +178,12 @@ def nerd_tree_struct(startpath, path=[]):
return d
def find_subtree(node, node_name, results=[]):
'''
Starting fron node, recursively, find node_name and its subtree
'''
if node['name'] == node_name:
node.update({'color_formatter' : YELLOW})
results.append(node)
children = node.get('children') or []
@ -182,11 +193,16 @@ def find_subtree(node, node_name, results=[]):
find_subtree(item_child, node_name, results)
else:
if item_child['name'] == node_name:
item_child.update({'color_formatter' : YELLOW})
results.append(item_child)
def find_node(startpath, item_name):
'''
search item_name in subtree starting from startpath
@param startpath string
@param item_name string
search the node with name==item_name (basically a subtree)
starting from startpath
Note: it finds all occurrencies
'''
rnode = nerd_tree_struct(startpath)
@ -198,8 +214,9 @@ def find_node(startpath, item_name):
def find_children(tree, item_abs_path):
'''
It returns a tuple, first value is if father of node with item_name is found,
second value represents the children of parent node
It returns a tuple:
first value True if father of node with item_abs_path is found,
second value represents the children of parent node with item_abs_path
'''
if tree['abs_path'] == item_abs_path:
if not tree.get('children'):
@ -219,7 +236,6 @@ def find_children(tree, item_abs_path):
def find_tree_struct(startpath, item_name):
'''
Bad!!!
'''
results = find_node(startpath, item_name)
if not results: