dnf-search-installed: modernize, update for dnf5
This commit is contained in:
parent
34e2ca053e
commit
85a34dc79a
2 changed files with 24 additions and 20 deletions
|
|
@ -1,47 +1,51 @@
|
|||
#!/usr/bin/env python
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import dnf
|
||||
import subprocess
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
from typing import Set
|
||||
import dnf
|
||||
|
||||
DNF = 'dnf'
|
||||
DNF = '/bin/dnf5'
|
||||
|
||||
def get_installed() -> Set[str]:
|
||||
|
||||
def get_installed() -> set[str]:
|
||||
base = dnf.Base()
|
||||
base.fill_sack()
|
||||
q = base.sack.query()
|
||||
return {f'{p.name}.{p.arch}' for p in q.installed()}
|
||||
|
||||
def colorize_g_fg(s: str) -> str:
|
||||
|
||||
def colorize_green_fg(s: str) -> str:
|
||||
return f'\x1b[32m{s}\033[0m'
|
||||
|
||||
|
||||
if sys.argv[1] != 'search':
|
||||
os.execvp(DNF, sys.argv[1:])
|
||||
sys.exit(0)
|
||||
|
||||
# call CLI to get all the formatting and highlighting for free
|
||||
res = subprocess.run(
|
||||
[DNF, '--color=always'] + sys.argv[1:],
|
||||
# todo: add --color=always https://github.com/rpm-software-management/dnf5/issues/839
|
||||
[DNF, *sys.argv[1:]],
|
||||
capture_output=True,
|
||||
encoding='utf-8',
|
||||
)
|
||||
check=False,
|
||||
)
|
||||
lines = res.stdout.split('\n')
|
||||
installed = get_installed()
|
||||
printlines = ''
|
||||
printlines = []
|
||||
# matching bold, magenta and normal codes from
|
||||
# https://github.com/rpm-software-management/dnf/blob/master/dnf/cli/term.py
|
||||
dnf_color = re.compile(r'\x1b\[35m|\x1b\[1m|\x1b\(B\x1b\[m')
|
||||
i = colorize_g_fg('*')
|
||||
i = colorize_green_fg('*')
|
||||
for line in lines:
|
||||
if not line or line.startswith('='):
|
||||
printlines += (line + '\n')
|
||||
if not line or not line.startswith(' '):
|
||||
printlines.append(line)
|
||||
continue
|
||||
(package, _) = line.split(' : ', maxsplit=1)
|
||||
package = dnf_color.sub('', package.rstrip())
|
||||
(package, _) = line.split(':', maxsplit=1)
|
||||
package = dnf_color.sub('', package.lstrip())
|
||||
indicator = i if package in installed else ' '
|
||||
printlines += f'{indicator} {line}\n'
|
||||
print(printlines, end='')
|
||||
printlines.append(f'{indicator} {line[1:]}') # [:1] to strip ' '
|
||||
print('\n'.join(printlines))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue