From 198b4cdfd37b1c289b1607ed1fa121c01847aa91 Mon Sep 17 00:00:00 2001 From: Amber Date: Wed, 17 Apr 2024 16:54:54 +0200 Subject: [PATCH] nerd_tree follow_symbolic_link --- ntree.py | 2 ++ src/ntree_cmdline_parser.py | 2 ++ src/tree.py | 22 +++++++++++++++++----- src/tree_repr.py | 7 ++++++- vfind.py | 1 + 5 files changed, 28 insertions(+), 6 deletions(-) diff --git a/ntree.py b/ntree.py index 203a987..9aab7d8 100755 --- a/ntree.py +++ b/ntree.py @@ -10,12 +10,14 @@ if __name__ == '__main__': path = cmd_args.path list_dir_first = cmd_args.list_directories_first colorize = cmd_args.colorize + follow_symbolic_link = cmd_args.follow_symbolic_link # opts for generatign the tree opts = { 'items_to_exclude' : cmd_args.exclude or [], 'list_dir_first' : list_dir_first, 'colorize' : colorize, + 'follow_symbolic_link': follow_symbolic_link, } ntree = NerdTree(path, opts=opts) diff --git a/src/ntree_cmdline_parser.py b/src/ntree_cmdline_parser.py index cf6a14a..41a4255 100644 --- a/src/ntree_cmdline_parser.py +++ b/src/ntree_cmdline_parser.py @@ -12,6 +12,8 @@ cmd_parser.add_argument("path") cmd_parser.add_argument("-e", "--exclude", action="append") cmd_parser.add_argument("-l", "--list-directories-first", action="store_true") +cmd_parser.add_argument("-L", "--follow-symbolic-link", action="store_true") + # colorize cmd_parser.add_argument("-c", "--colorize", action="store_true") diff --git a/src/tree.py b/src/tree.py index 8bff2da..d4ae9f2 100644 --- a/src/tree.py +++ b/src/tree.py @@ -109,8 +109,14 @@ class NerdTree(): colorize = self.opts['colorize'] if self.opts.get('colorize') is not None else False if not colorize: 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 def tree_struct(self, startpath, path=[]): @@ -166,6 +172,10 @@ class NerdTree(): '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 try: items = self.sort_items(startpath) @@ -181,15 +191,14 @@ class NerdTree(): if self.test_exclude_true(item): continue + abs_path_item = startpath+item item_type, path_object = self.get_path_obj(abs_path_item) - if path_object.is_dir(): d['children'].append(self.tree_struct(abs_path_item, path=path[:])) else: is_symlink = path_object.is_symlink() - stat_item = path_object.lstat() stat_item_mode = stat_item.st_mode @@ -265,7 +274,10 @@ class NerdTree(): nerd_tree_txt = rootnode_name if (rootnode['is_symlink'] and rootnode.get('target', '')): - nerd_tree_txt += ' -> %s' % (rootnode['target'],) + if rootnode.get('target_color_formatter'): + nerd_tree_txt += ' -> %s' % (rootnode['target_color_formatter'](rootnode['target']),) + else: + nerd_tree_txt += ' -> %s' % (rootnode['target'],) items = rootnode.get('children') or [] diff --git a/src/tree_repr.py b/src/tree_repr.py index 1522c34..3d4eea3 100644 --- a/src/tree_repr.py +++ b/src/tree_repr.py @@ -44,7 +44,10 @@ def produce_treeline(prec_seps, lsep): tomount = sep + item_name if item.get('is_symlink') and item.get('target', ''): - tomount += ' -> %s' % ((item['target'],)) + if item.get('target_color_formatter'): + tomount += ' -> %s' % ((item['target_color_formatter'](item['target']),)) + else: + tomount += ' -> %s' % ((item['target'],)) # if item.get('type') == 'd': # if item.get('nchildren') is not None: @@ -81,5 +84,7 @@ def get_color_formatter(t): return BLUE if t == 'x': return GREEN + if t == 'l': + return CYAN return None diff --git a/vfind.py b/vfind.py index 4d8c9d0..e6dc3fa 100755 --- a/vfind.py +++ b/vfind.py @@ -16,6 +16,7 @@ if __name__ == '__main__': # opts for generatign the tree opts = { 'items_to_exclude' : cmd_args.exclude or [], + 'follow_symbolic_link': True, } if show_subtree_info: