summaryrefslogtreecommitdiff
path: root/src/xi.py
blob: f0c473c36341ee2e979f3aac978d6bfbedea341f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import options
import config
import util
import os
import colors

from verbs.sync import sync
from verbs.file import file
from verbs.files import files
from verbs.search import search
from verbs.info import info, get_installed_info
from verbs.remove import remove
from verbs.install import install
from verbs.update import update
from verbs.sync import keyimport

verbs = { v: globals()[v] for v in [
                "search",
                "keyimport",
                "file",
                "files",
                "info",
                "update",
                "install",
                "remove",
                "sync"
            ]
        }

def print_stats(conf, opts):
    pkg_count = {}
    installed_dir = util.add_path(opts["r"], conf["dir"]["installed"])

    for package in os.listdir(installed_dir):
        installed_info = get_installed_info(package, conf, opts)
        repo = installed_info["REPO"]
        
        if repo not in pkg_count: pkg_count[repo] = 0
        pkg_count[repo] += 1

    key_count = len(os.listdir(util.add_path(opts["r"], conf["dir"]["keychain"])))

    total = sum(pkg_count.values())

    distro = util.get_distro()["NAME"]

    print(colors.LIGHT_CYAN + ("~"*w) + colors.RESET)
    print(colors.LIGHT_CYAN + "xipkg", end="")
    print(colors.CYAN + " on ", end="")
    print(colors.LIGHT_CYAN + distro) 
        
    print(colors.BLUE + f"Total key count: {colors.LIGHT_BLUE}{key_count}")
    print(colors.BLUE + f"Total package count: {colors.LIGHT_BLUE}{total}")

    for repo,count in pkg_count.items():
        print(f"\t{colors.BLUE}{repo}: {colors.LIGHT_BLUE}{count}")

    w, h = util.get_area()
    print(colors.LIGHT_CYAN + ("~"*w) + colors.RESET)


def main():
    opts = options.parse_args()
    args = opts["args"]
    
    if opts["h"]:
        options.print_usage()
        return
    
    conf = config.parse_file(opts["c"])
    if len(args) > 0:
        verb = args[0].lower()

        try: 
            (
                verbs[verb] if verb in verbs else search
            )(
                args[1:] if len(args) > 1 else [], opts, conf
            )
        except KeyboardInterrupt:
            print(colors.RESET + colors.CLEAR_LINE + colors.RED + "Action cancelled by user")
    else:
        print_stats(conf, opts)
        return

    print(colors.RESET + colors.CLEAR_LINE, end="")