From de88767812d2ecb3df552dd37af2648ee0a74e66 Mon Sep 17 00:00:00 2001 From: Amber Date: Tue, 30 Jan 2024 14:24:21 +0100 Subject: [PATCH] dont_show_children, aggregate data --- src/__pycache__/tree.cpython-39.pyc | Bin 4260 -> 4706 bytes src/__pycache__/tree_find.cpython-39.pyc | Bin 3836 -> 3831 bytes src/tree.py | 44 ++++++++++++++++++----- src/tree_find.py | 20 ++++++----- 4 files changed, 47 insertions(+), 17 deletions(-) diff --git a/src/__pycache__/tree.cpython-39.pyc b/src/__pycache__/tree.cpython-39.pyc index 4571cd0d573b22f9466d5904a0950808519ffb44..cc2e19bd1d99628452fcac86f8fabf0cc6dace04 100644 GIT binary patch delta 2100 zcmZ8iOKcoP5bf^y&(6%QojCS7zjeM|u$?GEBtT+<5-BJ_R+Qv4fEL?5Ymc+P^o)rd zdSodHHxPsg2#FkQDGEYDLLx}ygvbfb95~F4aN-aNapXi*Z$8GByQZqTy1G8EYwwOd z+850FzDwZm%f}C*Uq@~QJDL|j;)K%{3GvHXw9P_Bk=I&UTMzZN5gKhXG}~5awe8Su zJE5cYY0GVwLXVO&gzMb6Nw{%?g+8YjN!|PlubHXSLO(y7F5Z%-Y4g^@$!YFz_a+Ig z1=6Iv#J!s|wE0#Z@G@|Y(pLBY@GfUDX)->@w}4dQJ~$n^zmJYl-viIW2l5RO1NjAZ zh?eu~?8vGPDhBHmtmGt+lt_AmW=yg*x=zG4PBX1D3K9cpA7>zWAZZ{S=31snEjf0U zT&Gf#tjXX)eWswipv{7|>bgMRg&8Le9*e?)Z!>O1g$qow(r(48oy%ZUK_h>5E@|{2 z??#+P?V_aE?%Yx|pB4_P8tF16jfq7w7D>7}P&es{RHc?2$-mc+1Z6NsT&c>p@_+Q-Pu7?#b>3t#|UHMH? z=?s(1m1ND6?mB&kWu?rM-kQHoG9SLdI{B1}32Ab#O4f8qmdd$ijvWnT0Fon1(&fe- zZJko`%tmQtApIqS2X_qIgPTlfTseQ+j193BV#<&38b*+7vmNeQA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rkG$QA~85tGr65h`2Lq2Bg0-vxZ?kz=fhtFxlQKmn<XN~0w=YjGS(eU7!@pnlC3XUbUpe$&Pn8Ug{hc=9x$F67N^V!g-iLHYe%{toF19>CWvI=}E8wIy;&m%WQHrI)?qP}k4A3)`MNVt{?djH5}djW9~RFzRiFvWDwX zGRU++ysMAuVLNWNB6%3&xuP5~SYXUClVHoR+uMkTmE>X>hAZ7z_Uf^mFgdd(2ci_( zRoBG)VYx1Nq8Cmx8S$!)w K0SkI3==}zu6YG%x diff --git a/src/tree.py b/src/tree.py index aa02caa..da97e72 100644 --- a/src/tree.py +++ b/src/tree.py @@ -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: diff --git a/src/tree_find.py b/src/tree_find.py index 3e31048..dded9d2 100644 --- a/src/tree_find.py +++ b/src/tree_find.py @@ -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):