From d892c03bfaa25d70e535b5f3df1a600a6cb7e639 Mon Sep 17 00:00:00 2001 From: Amber Date: Fri, 5 Apr 2024 11:39:18 +0200 Subject: [PATCH] support for shell expansions in nerd_tree command --- src/tree.py | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/tree.py b/src/tree.py index 2b70087..8bff2da 100644 --- a/src/tree.py +++ b/src/tree.py @@ -1,5 +1,7 @@ import os import stat as _stat +import fnmatch, re + from pathlib import Path from src.colors import RED, YELLOW, GREEN, CYAN , BLUE, PURPLE @@ -16,11 +18,33 @@ class NerdTree(): def __init__(self, startpath='', opts={}): self.startpath = startpath self.opts = opts + ''' + the below member contains regexp compiled for the items to exclude + the match in any of these regexp must cause the skip + ''' + self.reobj_excludes = [] + self.init_reobj_excludes() if startpath: self.json_tree = self.tree_struct(startpath) else: self.json_tree = {} + def init_reobj_excludes(self): + ''' + it use bash expansion pattern to generate + regexp to check for exclude items + ''' + exclusions = self.opts.get('items_to_exclude') or [] + if not exclusions: + return + + for e in exclusions: + # for example e -> '*.txt' + regex = fnmatch.translate(e) + reobj = re.compile(regex) + self.reobj_excludes.append(reobj) + + def get_path_obj(self, abs_path_item): ## using lstat not produce error in case of symbolic link path_object = Path(abs_path_item) @@ -65,6 +89,19 @@ class NerdTree(): return os.listdir(startpath) + def test_exclude_true(self, item_name): + ''' + @param item_name string - the name of file or folder + ''' + if not self.reobj_excludes: + return False + + reobj_tests = list(map(lambda reobj: reobj.match(item_name) ,self.reobj_excludes)) + if any(reobj_tests): + return True + + return False + def colorize(self, d): ''' use the mandatory key `type` to format item @@ -138,7 +175,10 @@ class NerdTree(): d.setdefault('not_readable', 1) for item in items: - if item in (self.opts.get('items_to_exclude') or []): + # if item in (self.opts.get('items_to_exclude') or []): + # continue + + if self.test_exclude_true(item): continue abs_path_item = startpath+item