if termscreen is smaller than output, dont show full path

This commit is contained in:
stephentoth 2024-04-25 18:45:47 -05:00
parent 8d0c42dfe2
commit 795bc462bf
1 changed files with 21 additions and 13 deletions

30
ls.py
View File

@ -50,6 +50,11 @@ FileString = namedtuple("FileString", ["path", "file", "file_color",
def colorwrap(text, color): def colorwrap(text, color):
return f"{COLORS[color]}{text}{COLORS['RESET']}" return f"{COLORS[color]}{text}{COLORS['RESET']}"
def escape(str):
if ' ' in str:
return '\'' + str + '\''
return str
def sizeof_fmt(num): def sizeof_fmt(num):
for unit in ("", "K", "M", "G", "T", "P", "E", "Z"): for unit in ("", "K", "M", "G", "T", "P", "E", "Z"):
if abs(num) < 1024.0: if abs(num) < 1024.0:
@ -170,23 +175,25 @@ def short_listing(files_meta):
# rel is broken # rel is broken
#print(colored_file) #print(colored_file)
def long_listing(files_meta): def long_listing(files_meta):
use_full_path = True use_full_path = True
print(f"ls.py {' '.join(sys.argv[1:])}") print(f"ls.py {' '.join(sys.argv[1:])}")
print(f"files: {len(files_meta)}") 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)) 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: if longest_filename_string_len + longest_size_string_len + 60 > term_width:
# 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) use_full_path = False
# 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)
#
for file in files_meta: for file in files_meta:
@ -209,14 +216,15 @@ def long_listing(files_meta):
size=sizeof_fmt(size) size=sizeof_fmt(size)
if args.full_time: 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 #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_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 use_full_path:
if ' ' in file.abs_file_path: file_str = ' ' + escape(file_strings.colored_filepath) + file_strings.link_sep + file_strings.colored_link_dest
file_str = '\'' + 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)) #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 '' 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 ''