11#!/usr/bin/env python3
22
3+ import argparse
34import itertools
45import os
56import stat
@@ -9,6 +10,11 @@ import gitignorefile
910
1011
1112paths = sys .argv [1 :] or ["." ]
13+ parser = argparse .ArgumentParser ()
14+ parser .add_argument ("directory" , nargs = "*" , default = ["." ], help = "Directory." )
15+ parser .add_argument ("-a" , action = "store_true" , help = "All files are printed." )
16+ parser .add_argument ("-s" , action = "store_true" , help = "Print the size of each file in bytes along with the name." )
17+ args = parser .parse_args ()
1218
1319
1420def color (name , mode , is_link ):
@@ -69,9 +75,10 @@ def pairwise(collection):
6975 return zip (a , b )
7076
7177
72- def get_mode (path ):
78+ def get_mode_and_size (path ):
7379 try :
74- return os .lstat (path ).st_mode
80+ stat = os .lstat (path )
81+ return stat .st_mode , stat .st_size
7582
7683 except (FileNotFoundError , PermissionError ):
7784 """"""
@@ -81,12 +88,14 @@ def filter_path(path, prefix):
8188 children = sorted (os .listdir (path ), key = lambda name : (name .lower (), name ))
8289
8390 for name in children :
91+ if not args .a and name .startswith ("." ):
92+ continue
8493 sub_path = os .path .join (path , name )
85- mode = get_mode (sub_path )
94+ mode , size = get_mode_and_size (sub_path )
8695 is_link = mode and stat .S_IFMT (mode ) == stat .S_IFLNK
8796 if is_link :
8897 link = os .readlink (sub_path )
89- link_mode = get_mode (os .path .join (path , link ))
98+ link_mode , _ = get_mode_and_size (os .path .join (path , link ))
9099 is_dir = (link_mode or False ) and stat .S_IFMT (link_mode ) == stat .S_IFDIR
91100 link_suffix = f" -> { apply_color (link , link_mode , False )} "
92101 if link_mode is None :
@@ -97,15 +106,18 @@ def filter_path(path, prefix):
97106 is_dir = (mode or False ) and stat .S_IFMT (mode ) == stat .S_IFDIR
98107
99108 if name != ".git" and not gitignores (sub_path , is_dir = is_dir ):
100- yield sub_path , is_link , is_dir , name , link_suffix , mode
109+ yield sub_path , is_link , is_dir , name , link_suffix , mode , size
101110
102111
103112def process_path (path , prefix = "" ):
104- for (sub_path , is_link , is_dir , name , link_suffix , mode ), next in pairwise (
113+ for (sub_path , is_link , is_dir , name , link_suffix , mode , size ), next in pairwise (
105114 itertools .chain (filter_path (path , prefix ), [None ])
106115 ):
107116 sticks = "├──" if next else "└──"
108- print (f"{ prefix } { sticks } { apply_color (name , mode , is_link )} { link_suffix } " )
117+ colored_name = apply_color (name , mode , is_link )
118+ if args .s :
119+ colored_name = f"[{ str (size ).rjust (11 )} ] { colored_name } "
120+ print (f"{ prefix } { sticks } { colored_name } { link_suffix } " )
109121
110122 if not is_link and is_dir :
111123 sticks = "│ " if next else " "
@@ -139,9 +151,11 @@ colors = dict(
139151
140152gitignores = gitignorefile .Cache ()
141153statistics = {"directories" : 0 , "files" : 0 }
142- for path in paths :
143- mode = get_mode (path )
154+ for path in args . directory :
155+ mode , size = get_mode_and_size (path )
144156 colored_name = apply_color (path , mode , False )
157+ if args .s :
158+ colored_name = f"[{ str (size ).rjust (11 )} ] { colored_name } "
145159 if mode is not None and stat .S_IFMT (mode ) == stat .S_IFDIR :
146160 print (colored_name )
147161 process_path (path )
0 commit comments