Reorganise directory, improve comments and add more package managers

This commit is contained in:
Maddie H 2022-05-24 14:35:41 +01:00
parent 3a7d071529
commit 172ccb907e
No known key found for this signature in database
GPG Key ID: 63900F0992F8C7B3
3 changed files with 50 additions and 33 deletions

View File

@ -1,19 +0,0 @@
from subprocess import check_output
commands = [
"pacman -Qq",
"apt list --installed",
"yum list installed",
"dnf list installed",
"qlist -I",
"rpm -qa",
"nix-store -qR /run/current-system/sw"
]
def get_num_packages() -> (int, bool):
for command in commands:
try:
return len(check_output(command.split(" ")).decode("utf-8").split("\n")) - 1
except FileNotFoundError:
pass
return False

View File

@ -1,20 +1,23 @@
#!/usr/bin/env python3
# General imports
from argparse import ArgumentParser
from datetime import timedelta
from getpass import getuser
# from platform import machine as architecture
from platform import platform as system
from platform import release as kernel
from random import choice as random_choice
from socket import gethostname
from time import clock_gettime, CLOCK_BOOTTIME
# Title - user@hostname
from getpass import getuser
from socket import gethostname
# System info modules
from platform import platform as system
from platform import release as kernel
# from platform import machine as architecture
from distro import name as distribution
from modules.packages import get_num_packages as packages
from packages import get_num_packages as packages
# Define a dictionary of all the flags and their colors
# A dictionary of all the flags and their colors
# Each color is the color for an individual row in the flag
flags = {
"classic": [196, 208, 226, 28, 20, 90],
@ -38,18 +41,18 @@ reset = "\033[0m\033[39m"
def color256(col: int, bg_fg: str) -> str:
# Hacky alias around manually typing out escape codes every time
# Alias to avoid manually typing out escape codes every time
return f"\033[{48 if bg_fg == 'bg' else 38};5;{col}m"
def draw_fetch(flag_name: str, width: int = None):
# Load the flag from the dictionary of flags
# Load the chosen flag from the dictionary of flags
flag = flags[flag_name]
# Make sure that the row color is different to the color of the hostname
row_color = color256(flag[1] if flag[0] != flag[1] else flag[2], "fg")
# The fetch data to be displayed
# The fetch data (system info) to be displayed
row_data = [
f"{color256(flag[0], 'fg') if flag[0] != 0 else color256(242, 'fg')}"
f"\033[1m{getuser()}@{gethostname()}{reset}",
@ -65,7 +68,7 @@ def draw_fetch(flag_name: str, width: int = None):
# If the data is greater than the flag length then duplicate the length of the flag
flag = [element for element in flag for _ in (0, 1)]
# Set the width of the flag relative to its height (keep it in a nice ratio)
# Set the width of the flag relative to its height (keep it in a ratio)
width = width or round(len(flag) * 1.5 * 3)
# Ensures nothing is printed for empty lines
@ -83,14 +86,14 @@ def draw_fetch(flag_name: str, width: int = None):
def main():
# Argument configuration
# Argument configuration - options
parser = ArgumentParser()
parser.add_argument("-f", "--flag", help="displays the chosen flag")
parser.add_argument("-r", "--random", help="randomly choose a flag from a list seperated by commas")
parser.add_argument("-w", "--width", help="choose a custom width for the flag", type=int)
parser.add_argument("-l", "--list", help="lists all the flags that can be displayed", action="store_true")
# Parse the arguments
# Parse (collect) any arguments
args = parser.parse_args()
if args.flag:
@ -110,6 +113,7 @@ def main():
print(f"Available flags:\n{', '.join(flags)}")
else:
# By default, draw the classic flag
draw_fetch("classic", args.width)

32
src/modules/packages.py Normal file
View File

@ -0,0 +1,32 @@
from subprocess import check_output
commands = [
"pacman -Qq --color never", # Arch Linux
"xbps-query -l", # Void Linux
"kiss l", # KISS Linux
"apt list --installed", # Debian, Ubuntu, Mint
"dpkg -l", # Debian, Ubuntu, Mint
"dnf list installed", # Fedora
"zypper search -i", # openSUSE
"rpm -qa", # RHEL, Fedora Core, CentOS
"yum list installed", # RHEL, Fedora Core, CentOS
"nix-store -qR /run/current-system/sw", # NixOS
"equery list '*'", # Gentoo
"qlist -I", # Gentoo
"pkg info -a", # BSDs
"pkg_info", # BSDs
"apk info", # Alpine
]
def get_num_packages() -> (int, bool):
for command in commands:
try:
# Get the length of the output of the command - the number of packages
return len(check_output(command.split(" ")).decode("utf-8").split("\n")) - 1
except FileNotFoundError:
# If the command doesn't exist, skip it
pass
# If we get here, we didn't find any of the commands
return False