nerd_tree follow_symbolic_link

This commit is contained in:
Amber 2024-04-17 16:54:54 +02:00
parent 207cc4934b
commit 198b4cdfd3
5 changed files with 28 additions and 6 deletions

View File

@ -10,12 +10,14 @@ if __name__ == '__main__':
path = cmd_args.path path = cmd_args.path
list_dir_first = cmd_args.list_directories_first list_dir_first = cmd_args.list_directories_first
colorize = cmd_args.colorize colorize = cmd_args.colorize
follow_symbolic_link = cmd_args.follow_symbolic_link
# opts for generatign the tree # opts for generatign the tree
opts = { opts = {
'items_to_exclude' : cmd_args.exclude or [], 'items_to_exclude' : cmd_args.exclude or [],
'list_dir_first' : list_dir_first, 'list_dir_first' : list_dir_first,
'colorize' : colorize, 'colorize' : colorize,
'follow_symbolic_link': follow_symbolic_link,
} }
ntree = NerdTree(path, opts=opts) ntree = NerdTree(path, opts=opts)

View File

@ -12,6 +12,8 @@ cmd_parser.add_argument("path")
cmd_parser.add_argument("-e", "--exclude", action="append") cmd_parser.add_argument("-e", "--exclude", action="append")
cmd_parser.add_argument("-l", "--list-directories-first", action="store_true") cmd_parser.add_argument("-l", "--list-directories-first", action="store_true")
cmd_parser.add_argument("-L", "--follow-symbolic-link", action="store_true")
# colorize # colorize
cmd_parser.add_argument("-c", "--colorize", action="store_true") cmd_parser.add_argument("-c", "--colorize", action="store_true")

View File

@ -109,8 +109,14 @@ class NerdTree():
colorize = self.opts['colorize'] if self.opts.get('colorize') is not None else False colorize = self.opts['colorize'] if self.opts.get('colorize') is not None else False
if not colorize: if not colorize:
return d return d
t = d['type']
d['color_formatter'] = _tree_repr.get_color_formatter(t) if d.get('is_symlink'):
d['color_formatter'] = _tree_repr.get_color_formatter('l')
d['target_color_formatter'] = _tree_repr.get_color_formatter(d['type'])
else:
d['color_formatter'] = _tree_repr.get_color_formatter(d['type'])
d['target_color_formatter'] = ''
return d return d
def tree_struct(self, startpath, path=[]): def tree_struct(self, startpath, path=[]):
@ -166,6 +172,10 @@ class NerdTree():
'is_symlink' : start_dir_is_symlink, 'is_symlink' : start_dir_is_symlink,
}) })
follow_symbolic_link = self.opts['follow_symbolic_link'] if self.opts.get('follow_symbolic_link') is not None else False
if start_dir_is_symlink and not follow_symbolic_link:
return d
# using listdir # using listdir
try: try:
items = self.sort_items(startpath) items = self.sort_items(startpath)
@ -181,15 +191,14 @@ class NerdTree():
if self.test_exclude_true(item): if self.test_exclude_true(item):
continue continue
abs_path_item = startpath+item abs_path_item = startpath+item
item_type, path_object = self.get_path_obj(abs_path_item) item_type, path_object = self.get_path_obj(abs_path_item)
if path_object.is_dir(): if path_object.is_dir():
d['children'].append(self.tree_struct(abs_path_item, path=path[:])) d['children'].append(self.tree_struct(abs_path_item, path=path[:]))
else: else:
is_symlink = path_object.is_symlink() is_symlink = path_object.is_symlink()
stat_item = path_object.lstat() stat_item = path_object.lstat()
stat_item_mode = stat_item.st_mode stat_item_mode = stat_item.st_mode
@ -265,6 +274,9 @@ class NerdTree():
nerd_tree_txt = rootnode_name nerd_tree_txt = rootnode_name
if (rootnode['is_symlink'] and rootnode.get('target', '')): if (rootnode['is_symlink'] and rootnode.get('target', '')):
if rootnode.get('target_color_formatter'):
nerd_tree_txt += ' -> %s' % (rootnode['target_color_formatter'](rootnode['target']),)
else:
nerd_tree_txt += ' -> %s' % (rootnode['target'],) nerd_tree_txt += ' -> %s' % (rootnode['target'],)
items = rootnode.get('children') or [] items = rootnode.get('children') or []

View File

@ -44,6 +44,9 @@ def produce_treeline(prec_seps, lsep):
tomount = sep + item_name tomount = sep + item_name
if item.get('is_symlink') and item.get('target', ''): if item.get('is_symlink') and item.get('target', ''):
if item.get('target_color_formatter'):
tomount += ' -> %s' % ((item['target_color_formatter'](item['target']),))
else:
tomount += ' -> %s' % ((item['target'],)) tomount += ' -> %s' % ((item['target'],))
# if item.get('type') == 'd': # if item.get('type') == 'd':
@ -81,5 +84,7 @@ def get_color_formatter(t):
return BLUE return BLUE
if t == 'x': if t == 'x':
return GREEN return GREEN
if t == 'l':
return CYAN
return None return None

View File

@ -16,6 +16,7 @@ if __name__ == '__main__':
# opts for generatign the tree # opts for generatign the tree
opts = { opts = {
'items_to_exclude' : cmd_args.exclude or [], 'items_to_exclude' : cmd_args.exclude or [],
'follow_symbolic_link': True,
} }
if show_subtree_info: if show_subtree_info: