support for shell expansions in nerd_tree command
This commit is contained in:
parent
96986ee543
commit
d892c03bfa
42
src/tree.py
42
src/tree.py
@ -1,5 +1,7 @@
|
|||||||
import os
|
import os
|
||||||
import stat as _stat
|
import stat as _stat
|
||||||
|
import fnmatch, re
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from src.colors import RED, YELLOW, GREEN, CYAN , BLUE, PURPLE
|
from src.colors import RED, YELLOW, GREEN, CYAN , BLUE, PURPLE
|
||||||
@ -16,11 +18,33 @@ class NerdTree():
|
|||||||
def __init__(self, startpath='', opts={}):
|
def __init__(self, startpath='', opts={}):
|
||||||
self.startpath = startpath
|
self.startpath = startpath
|
||||||
self.opts = opts
|
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:
|
if startpath:
|
||||||
self.json_tree = self.tree_struct(startpath)
|
self.json_tree = self.tree_struct(startpath)
|
||||||
else:
|
else:
|
||||||
self.json_tree = {}
|
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):
|
def get_path_obj(self, abs_path_item):
|
||||||
## using lstat not produce error in case of symbolic link
|
## using lstat not produce error in case of symbolic link
|
||||||
path_object = Path(abs_path_item)
|
path_object = Path(abs_path_item)
|
||||||
@ -65,6 +89,19 @@ class NerdTree():
|
|||||||
|
|
||||||
return os.listdir(startpath)
|
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):
|
def colorize(self, d):
|
||||||
'''
|
'''
|
||||||
use the mandatory key `type` to format item
|
use the mandatory key `type` to format item
|
||||||
@ -138,7 +175,10 @@ class NerdTree():
|
|||||||
d.setdefault('not_readable', 1)
|
d.setdefault('not_readable', 1)
|
||||||
|
|
||||||
for item in items:
|
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
|
continue
|
||||||
|
|
||||||
abs_path_item = startpath+item
|
abs_path_item = startpath+item
|
||||||
|
Loading…
x
Reference in New Issue
Block a user