diff --git a/ls.py b/ls.py index 07857ce..063bf28 100755 --- a/ls.py +++ b/ls.py @@ -50,6 +50,11 @@ FileString = namedtuple("FileString", ["path", "file", "file_color", def colorwrap(text, color): return f"{COLORS[color]}{text}{COLORS['RESET']}" +def escape(str): + if ' ' in str: + return '\'' + str + '\'' + return str + def sizeof_fmt(num): for unit in ("", "K", "M", "G", "T", "P", "E", "Z"): if abs(num) < 1024.0: @@ -170,23 +175,25 @@ def short_listing(files_meta): # rel is broken #print(colored_file) + def long_listing(files_meta): use_full_path = True print(f"ls.py {' '.join(sys.argv[1:])}") print(f"files: {len(files_meta)}") - term_width = os.get_terminal_size() + term_width = os.get_terminal_size().columns longest_size_string_len = len(str(functools.reduce(lambda a, b: a if len(str(a.stats.st_size)) > len(str(b.stats.st_size)) else b, files_meta).stats.st_size)) - - # if use_full_path: - # longest_filename_string_len = len(functools.reduce(lambda a, b: a if len(a.abs_file_path) > len(b.abs_file_path) else b, files_meta).abs_file_path) - # else: - # longest_filename_string_len = len(functools.reduce(lambda a, b: a if len(a.rel_file_path) > len(b.rel_file_path) else b, files_meta).rel_file_path) - # + if use_full_path: + longest_filename_string_len = len(functools.reduce(lambda a, b: a if len(a.abs_file_path) > len(b.abs_file_path) else b, files_meta).abs_file_path) + else: + longest_filename_string_len = len(functools.reduce(lambda a, b: a if len(a.rel_file_path) > len(b.rel_file_path) else b, files_meta).rel_file_path) + + if longest_filename_string_len + longest_size_string_len + 60 > term_width: + use_full_path = False for file in files_meta: @@ -209,15 +216,16 @@ def long_listing(files_meta): size=sizeof_fmt(size) if args.full_time: - timestring = time.strftime("%b %d %Y %X %z", time.localtime(file["stats"].st_mtime)) # -0400 + timestring = time.strftime("%b %d %Y %X %z", time.localtime(file.stats.st_mtime)) # -0400 #mtime = time.strftime("%b %d %X %Z", time.localtime(stats.st_mtime)) # CST file_strings = file_string_gen(file.abs_file_path, file.stats.st_mode) - file_str = ' ' + file_strings.colored_filepath + file_strings.link_sep + file_strings.colored_link_dest - #so hacky - if ' ' in file.abs_file_path: - file_str = '\'' + file_strings.colored_filepath + '\' ' + file_strings.link_sep + file_strings.colored_link_dest - + + if use_full_path: + file_str = ' ' + escape(file_strings.colored_filepath) + file_strings.link_sep + file_strings.colored_link_dest + else: + file_str = ' ' + escape(file_strings.colored_file) + file_strings.link_sep + file_strings.colored_link_dest + #print(file_string_gen(file["abs_file_path"], file["stats"].st_mode)) gitstat = git_gud(file.abs_file_path) if (args.extend and is_path_git_repo_dir(file.abs_file_path, file.stats.st_mode)) else ''