almost working version
This commit is contained in:
parent
6ca12cb426
commit
31f176e2aa
1 changed files with 45 additions and 12 deletions
57
checkpkg.py
57
checkpkg.py
|
@ -4,6 +4,7 @@ import urllib.request
|
|||
import json
|
||||
import signal
|
||||
import re
|
||||
import shutil
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
from urllib.error import HTTPError, URLError
|
||||
|
||||
|
@ -151,34 +152,66 @@ def main():
|
|||
output[pkg][pm] = 'Not found'
|
||||
|
||||
headers = ['Package'] + selected_pms
|
||||
terminal_width = get_terminal_width()
|
||||
|
||||
# Calculate initial column widths
|
||||
col_widths = [len(h) for h in headers]
|
||||
content_widths = {0: max(len(pkg) for pkg in args.packages)}
|
||||
|
||||
for pkg in args.packages:
|
||||
col_widths[0] = max(col_widths[0], len(pkg))
|
||||
versions = output.get(pkg, {})
|
||||
for i, pm in enumerate(selected_pms, 1):
|
||||
col_widths[i] = max(col_widths[i], len(output.get(pkg, {}).get(pm, '')))
|
||||
content_widths[i] = max(content_widths.get(i, 0), len(versions.get(pm, '')))
|
||||
|
||||
header_row = [colorize(h.ljust(w), 'header') for h, w in zip(headers, col_widths)]
|
||||
header_line = ' | '.join(header_row)
|
||||
# Set initial widths
|
||||
for i in range(len(headers)):
|
||||
col_widths[i] = max(len(headers[i]), content_widths.get(i, 0))
|
||||
|
||||
# Adjust for terminal width
|
||||
total_width = sum(col_widths) + 3 * (len(headers) - 1) # 3 chars per separator
|
||||
max_reductions = 5 # Max characters to reduce per column
|
||||
|
||||
if total_width > terminal_width:
|
||||
excess = total_width - terminal_width
|
||||
reducible = [i for i in range(1, len(headers)) if col_widths[i] > 10]
|
||||
|
||||
while excess > 0 and reducible:
|
||||
per_col = max(1, min(max_reductions, excess // len(reducible)))
|
||||
for i in reducible:
|
||||
reduction = min(per_col, col_widths[i] - 10, excess)
|
||||
col_widths[i] -= reduction
|
||||
excess -= reduction
|
||||
if excess <= 0:
|
||||
break
|
||||
|
||||
# Truncate text with ellipsis if needed
|
||||
def truncate(text, width):
|
||||
if len(text) > width and width > 3:
|
||||
return text[:width-3] + '...'
|
||||
return text.ljust(width)
|
||||
|
||||
# Print header
|
||||
header_line = ' | '.join(
|
||||
truncate(colorize(h, 'header'), w)
|
||||
for h, w in zip(headers, col_widths)
|
||||
)
|
||||
print(header_line)
|
||||
|
||||
# Simplified single-line separator without color
|
||||
if colorize.is_tty:
|
||||
print('-' * (sum(col_widths) + 3 * (len(headers) - 1)))
|
||||
else:
|
||||
print('-' * (sum(col_widths) + 3 * (len(headers) - 1)))
|
||||
# Separator line
|
||||
print('-' * min(sum(col_widths) + 3*(len(headers)-1), terminal_width))
|
||||
|
||||
# Print rows
|
||||
for pkg in args.packages:
|
||||
row = [colorize(pkg.ljust(col_widths[0]), 'bold')]
|
||||
row = [truncate(colorize(pkg, 'bold'), col_widths[0])]
|
||||
versions = output.get(pkg, {pm: 'Not found' for pm in selected_pms})
|
||||
|
||||
for pm in selected_pms:
|
||||
for i, pm in enumerate(selected_pms, 1):
|
||||
version = versions.get(pm, 'Not found')
|
||||
color = 'green' if version != 'Not found' else 'red'
|
||||
if 'AUR' in version:
|
||||
color = 'yellow'
|
||||
row.append(colorize(version.ljust(col_widths[headers.index(pm)]), color))
|
||||
# Fixed line with proper parenthesis closure
|
||||
row.append(truncate(colorize(version, color), col_widths[i]))
|
||||
|
||||
print(' | '.join(row))
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue