From 88c20dbce0d5b931e9c8609b30cd808a849bb952 Mon Sep 17 00:00:00 2001 From: spy Date: Wed, 20 Apr 2022 17:03:28 +0100 Subject: [PATCH] Automatic resizing of flag & data + modularisation & quality of life improvements --- README.md | 2 +- packages.py | 28 +++++++++++++++ pridefetch | 101 +++++++++++++++++++++++----------------------------- 3 files changed, 73 insertions(+), 58 deletions(-) create mode 100644 packages.py diff --git a/README.md b/README.md index 4c2fd8f..7ec51ae 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Forked from [megabytesofrem/pridefetch](https://github.com/megabytesofrem/pridef `pridefetch -f trans` #### Display either a trans or lesbian flag, with a 50/50 chance -`pridefetch -c trans,lesbian` +`pridefetch -r trans,lesbian` #### List all the available flags `pridefetch -l` diff --git a/packages.py b/packages.py new file mode 100644 index 0000000..a0cf4b4 --- /dev/null +++ b/packages.py @@ -0,0 +1,28 @@ +from subprocess import check_output + + +def get_num_packages() -> (int, bool): + try: + return len(check_output(["pacman", "-Qq"]).decode("utf-8").split("\n")) - 1 + except FileNotFoundError: + pass + try: + return len(check_output(["apt", "list", "--installed"]).decode("utf-8").split("\n")) - 1 + except FileNotFoundError: + pass + try: + return len(check_output(["yum", "list", "installed"]).decode("utf-8").split("\n")) - 1 + except FileNotFoundError: + pass + try: + return len(check_output(["dnf", "list", "installed"]).decode("utf-8").split("\n")) - 1 + except FileNotFoundError: + pass + try: + return len(check_output(["qlist", "-I"]).decode("utf-8").split("\n")) - 1 + except FileNotFoundError: + pass + try: + return len(check_output(["rpm", "-qa"]).decode("utf-8").split("\n")) - 1 + except FileNotFoundError: + return False \ No newline at end of file diff --git a/pridefetch b/pridefetch index 5c350c0..8b24e8d 100755 --- a/pridefetch +++ b/pridefetch @@ -1,36 +1,35 @@ -#!/usr/bin/env python3.10 +#!/usr/bin/env python3 from argparse import ArgumentParser -from distro import name as distro_name -from platform import machine, release -from random import choice as random_choice -from time import clock_gettime, CLOCK_BOOTTIME -from getpass import getuser -from socket import gethostname from datetime import timedelta +from packages import get_num_packages as packages +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 +from distro import name as distribution # Define a dictionary of all the flags and their colors # Each color is the color for an individual row in the flag flags = { - "straight": [0, 255, 0, 255, 0], "gay": [196, 208, 226, 28, 20, 90], "bisexual": [198, 198, 97, 25, 25], "lesbian": [202, 209, 255, 255, 168, 161], - "pansexual": [198, 198, 220, 220, 39, 39], + "pansexual": [198, 220, 39], "trans": [81, 211, 255, 211, 81], - "nonbinary": [226, 226, 255, 255, 98, 98, 237, 237], + "nonbinary": [226, 255, 98, 237,], "demiboy": [244, 249, 117, 255, 117, 249, 244], "demigirl": [244, 249, 218, 255, 218, 249, 244], "genderfluid": [211, 255, 128, 0, 63], "aromantic": [71, 149, 255, 249, 0], "agender": [0, 251, 255, 149, 255, 251, 0], - "asexual": [0, 0, 242, 242, 255, 255, 54, 54], + "asexual": [0, 242, 255, 54], "graysexual": [54, 242, 255, 242, 54], } -# Each box for each part of the flag -box = " " - # When printed, reset will end the color of the row reset = "\033[0m\033[39m" @@ -41,54 +40,39 @@ def color256(col: int, bg_fg: str) -> str: def draw_fetch(flag_name: str, width: int = None): - # Load configuration variables & flag data + # Load the flag from the dictionary of flags flag = flags[flag_name] - width = width or 20 # 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") + black = '\x1b[38;5;242m' + + row_data = [ + f"{color256(flag[0], 'fg') if row_color != black else color256(242, 'fg')}" + f"\033[1m{getuser()}@{gethostname()}{reset}", + f"{row_color}os {reset}{distribution() or system() or 'N/A'}", + # f"{row_color}arch {reset}{architecture() or 'N/A'}", + f"{row_color}pkgs {reset}{packages() or 'N/A'}", + f"{row_color}kernel {reset}{kernel() or system() or 'N/A'}", + f"{row_color}uptime {reset}{str(timedelta(seconds=clock_gettime(CLOCK_BOOTTIME))).split('.', 1)[0]}" + ] + + while len(flag) < len(row_data): + # If the data is greater than the flag length then duplicate the length of the flag until it is longer + flag = [element for element in flag for _ in (0, 1)] + + # Set the width of the flag relative to it's height + width = width or round(len(flag) * 1.5 * 3) + + # Print nothing for empty lines + row_data.append("") # Print a blank line to separate the flag from the terminal prompt print() - for row_number, row in enumerate(flag): - # Alternate displaying the information based on the current row - match row_number: - case 0: - # Get the username and hostname - user = getuser() - host = gethostname() - - # Username and hostname - color = color256(flag[row_number], "fg") - row_info = f"{color}\033[1m{user}@{host}{reset}" - - case 1: - # Distribution - distribution = distro_name() - row_info = f"{row_color}os {reset}{distribution or 'N/A'}" - - case 2: - # Architecture - arch = machine() - row_info = f"{row_color}arch {reset}{arch}" - - case 3: - # Kernel version - kernel = release() - row_info = f"{row_color}kern {reset}{kernel}" - - case 4: - # Uptime - time = str(timedelta(seconds=clock_gettime(CLOCK_BOOTTIME))).split(".", 1)[0] - row_info = f"{row_color}uptime {reset}{time}" - - case _: - # Empty row - row_info = "" - - # Print the row next to the flag - print(f" {color256(row, 'bg')}{box * width}\033[49m {row_info}") + for index, row in enumerate(flag): + # Print out each row of the fetch + print(f" {color256(row, 'bg')}{' ' * width}\033[49m{reset} {row_data[min(index, len(row_data) - 1)]}{reset}") # Print a blank line to separate the flag from the terminal prompt print() @@ -98,7 +82,7 @@ def main(): # Argument configuration parser = ArgumentParser() parser.add_argument("-f", "--flag", help="displays the chosen flag") - parser.add_argument("-c", "--choose", help="choose a flag at random from a list seperated by commas") + 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") @@ -106,10 +90,13 @@ def main(): args = parser.parse_args() if args.flag: - # Draw the flag + # Check if the flag exists in the dictionary of flags + assert args.flag in flags.keys(), f"flag '{args.flag}' is not a valid flag" + + # Draw the chosen flag and system information draw_fetch(args.flag, args.width) - if args.choose: + if args.random: # Choose a flag at random from a list of comma-seperated flags flag_choices = args.choose.split(",") draw_fetch(random_choice(flag_choices))