dont_show_children, aggregate data

This commit is contained in:
Amber 2024-01-30 14:24:21 +01:00
parent 60ccd874df
commit de88767812
4 changed files with 47 additions and 17 deletions

Binary file not shown.

View File

@ -1,6 +1,7 @@
import os
from src.colors import RED, YELLOW, GREEN, CYAN , BLUE, PURPLE
from pathlib import Path
from src.colors import RED, YELLOW, GREEN, CYAN , BLUE, PURPLE
import src.tree_repr as _tree_repr
class NerdTree():
@ -19,6 +20,21 @@ class NerdTree():
else:
self.json_tree = {}
def get_path_obj(self, abs_path_item):
## using lstat not produce error in case of symbolic link
path_object = Path(abs_path_item)
if path_object.is_dir():
item_type = 'd'
elif path_object.is_file():
item_type = 'f'
elif path_object.is_symlink():
item_type = 'l'
assert item_type
return (item_type, path_object)
def sort_directories_first(self, startpath):
files = []
dirs = []
@ -97,21 +113,23 @@ class NerdTree():
continue
abs_path_item = startpath+item
stat_item = os.stat(abs_path_item)
item_type, path_object = self.get_path_obj(abs_path_item)
if os.path.isdir(abs_path_item):
stat_item = path_object.lstat()
if path_object.is_dir():
d['children'].append(self.tree_struct(abs_path_item, path=path[:]))
else:
path_copy = path[:]
path_copy.append({
'name' : item,
'type' : 'f',
'type' : item_type,
'abs_path' : abs_path_item
})
d['children'].append({
'name' : item,
'type' : 'f',
'type' : item_type,
'abs_path' : abs_path_item,
'path' : path_copy,
'size' : stat_item.st_size,
@ -144,7 +162,7 @@ class NerdTree():
}
## if you want count the current directory as item you must sum 1
## if you want count the current directory size you must include item['size']
subtree['subtree_size'] += subtree_compute['subtree_size'] + item['size']
subtree['subtree_size'] += subtree_compute['subtree_size'] + item['subtree_size']
subtree['subtree_items'] += subtree_compute['subtree_items'] + 1
else:
subtree['subtree_size'] += item['size']
@ -167,10 +185,18 @@ class NerdTree():
else:
nerd_tree_txt = rootnode_name
if rootnode.get('nchildren') is not None:
nerd_tree_txt += ' (%s▾)' % rootnode['nchildren']
nodefound = rootnode.get('found')
if nodefound and self.opts['dont_show_children_nodes']:
if rootnode.get('subtree_items'):
nerd_tree_txt += ' (%s item(s)/%s %s ▾)' % (rootnode['subtree_items'], rootnode['subtree_size'], 'b')
# return nerd_tree_txt
# else:
# nerd_tree_txt += ' (0 - 0 ▾)'
items = rootnode.get('children') or []
items = []
else:
items = rootnode.get('children') or []
for n, item in enumerate(items):
if item['name'] in _tree_repr.ITEMS_TO_EXCLUDE:

View File

@ -19,7 +19,7 @@ class NerdTreeFind(NerdTree):
def find_node_recursively(self, node):
if node['name'] == self.item_name:
node_copy = node.copy()
node_copy.update({'color_formatter' : YELLOW})
node_copy.update({'color_formatter' : YELLOW, 'found' : 1})
self.results.append(node_copy)
children = node.get('children') or []
@ -30,7 +30,7 @@ class NerdTreeFind(NerdTree):
else:
if item_child['name'] == self.item_name:
item_child_copy = item_child.copy()
item_child_copy.update({'color_formatter' : YELLOW})
item_child_copy.update({'color_formatter' : YELLOW, 'found' : 1})
self.results.append(item_child_copy)
def find_node(self):
@ -64,16 +64,19 @@ class NerdTreeFind(NerdTree):
'''
results = self.find_node()
dont_show_children_nodes = self.find_opts['dont_show_children_nodes'] if self.find_opts.get('dont_show_children_nodes') is not None else False
## remove from this level the option
if not results:
return {}
if dont_show_children_nodes:
for result in results:
children = result.get('children') or []
result['nchildren'] = len(children)
result['children'] = []
# if dont_show_children_nodes:
# for result in results:
# children = result.get('children') or []
# result['nchildren'] = len(children)
# result['children'] = []
dont_show_children_nodes = self.find_opts['dont_show_children_nodes'] if self.find_opts.get('dont_show_children_nodes') is not None else False
self.opts['dont_show_children_nodes'] = dont_show_children_nodes
tree_struct = {}
@ -104,6 +107,7 @@ class NerdTreeFind(NerdTree):
if not islastpath: children.append(pcopy)
else : children.append(node)
self.compute_aggregate_recursively(tree_struct)
return tree_struct
def reset(self, new_item_name, new_opts):