MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / fixed_width_file_name

Function fixed_width_file_name

src/commoncode/cliutils.py:334–370  ·  view source on GitHub ↗

Return a fixed width file name of at most `max_length` characters computed from the `path` string and usable for fixed width display. If the `path` file name is longer than `max_length`, the file name is truncated in the middle using three dots "..." as an ellipsis and the ext is ke

(path, max_length=25)

Source from the content-addressed store, hash-verified

332
333
334def fixed_width_file_name(path, max_length=25):
335 """
336 Return a fixed width file name of at most `max_length` characters computed
337 from the `path` string and usable for fixed width display. If the `path`
338 file name is longer than `max_length`, the file name is truncated in the
339 middle using three dots "..." as an ellipsis and the ext is kept.
340
341 For example:
342 >>> fwfn = fixed_width_file_name('0123456789012345678901234.c')
343 >>> assert fwfn == '0123456789...5678901234.c'
344 >>> fwfn = fixed_width_file_name('some/path/0123456789012345678901234.c')
345 >>> assert fwfn == '0123456789...5678901234.c'
346 >>> fwfn = fixed_width_file_name('some/sort.c')
347 >>> assert fwfn == 'sort.c'
348 >>> fwfn = fixed_width_file_name('some/123456', max_length=5)
349 >>> assert fwfn == ''
350 """
351 if not path:
352 return ""
353
354 # get the path as unicode for display!
355 filename = file_name(path)
356 if len(filename) <= max_length:
357 return filename
358 base_name, ext = splitext(filename)
359 dots = 3
360 len_ext = len(ext)
361 remaining_length = max_length - len_ext - dots
362
363 if remaining_length < 5 or remaining_length < (len_ext + dots):
364 return ""
365
366 prefix_and_suffix_length = abs(remaining_length // 2)
367 prefix = base_name[:prefix_and_suffix_length]
368 ellipsis = dots * "."
369 suffix = base_name[-prefix_and_suffix_length:]
370 return "{prefix}{ellipsis}{suffix}{ext}".format(**locals())
371
372
373def file_name_max_len(used_width=BAR_WIDTH + 1 + 7 + 1 + 8 + 1):

Calls 3

file_nameFunction · 0.90
splitextFunction · 0.90
formatMethod · 0.45