support for shell expansions in nerd_tree command

This commit is contained in:
Amber 2024-04-05 11:39:18 +02:00
parent 96986ee543
commit d892c03bfa

View File

@ -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