From 77b06090f69264bcc93911664bea2810cd4571ba Mon Sep 17 00:00:00 2001
From: davidovski <david@davidovski.xyz>
Date: Wed, 24 Nov 2021 22:22:30 +0000
Subject: cleaned a few things up

---
 src/util.py          |  7 +++++-
 src/verbs/install.py | 60 +++++++++++++++++++++++++++++++++-------------------
 src/verbs/sync.py    | 17 ++++++++++++++-
 src/xi.py            | 19 ++++++++++++-----
 4 files changed, 74 insertions(+), 29 deletions(-)

diff --git a/src/util.py b/src/util.py
index 50efa7b..50b8ff8 100644
--- a/src/util.py
+++ b/src/util.py
@@ -33,6 +33,11 @@ def loading_bar(completed, total, text,
 
     print(color + info, end="\r")
 
+def fill_line(text, color, end="\n"):
+    columns, rows = shutil.get_terminal_size((80, 20))
+    spaces = columns - (len(text))
+    print(color + text +  "".join([" " for i in range(spaces)]), end=end)
+
 def print_reset(text):
     print(colors.RESET + text)
 
@@ -65,7 +70,7 @@ def curl_to_file(url, path, text=""):
             for chunk in ic:
                 if text:
                     divisor, unit = get_unit(length)
-                    loading_bar(int(done/divisor), int(length/divisor), "Downloading " + text, unit=unit)
+                    loading_bar(round(done/divisor, 2), round(length/divisor, 2), "Downloading " + text, unit=unit)
 
                 f.write(chunk)
                 done += c_size
diff --git a/src/verbs/install.py b/src/verbs/install.py
index 0531110..6c8c9fd 100644
--- a/src/verbs/install.py
+++ b/src/verbs/install.py
@@ -174,28 +174,38 @@ def find_all_dependencies(package_names, options, config):
     while len(to_check) > 0:
         util.loading_bar(len(all_deps), len(all_deps) + len(to_check), "Resolving dependencies...")
         dep = to_check.pop()
-
-        dep_checksum, dep_sources, dep_repo = find_package(dep, config["repos"], config["dir"]["packages"], config["sources"])
-        if dep_checksum is not None:
-            info = retrieve_package_info(
-                        dep_sources, dep_checksum, dep, config,
-                        verbose=options["v"], skip_verification=options["u"]
-                    )
-
-            if len(info) > 0:
-                if not dep in all_deps:
-                    all_deps.append(dep)
-                    deps = resolve_dependencies(info)
-                    for dep in deps:
-                        if not dep in all_deps:
-                            if is_installed(dep, config, options["r"]):
-                                if options["v"]: print(colors.YELLOW + f"Package {dep} has already been installed")
-                            else:
-                                to_check.append(dep)
-            elif options["v"]:
-                    util.print_reset(colors.CLEAR_LINE + colors.RED + f"Failed to retrieve info for {dep}")
+        
+        # probably better way to implement this obligatory wildcard
+        # 100% sure there is a better way of doing this than installing all packages from a repo
+        # maybe some sort of package grouping (or empty package with deps on all needed)
+        if dep[-2:] == "/*":
+            repo = dep[:-2]
+            repo_dir = os.path.join(config["dir"]["packages"], repo)
+            files = os.listdir(repo_dir)
+            return files
         else:
-            if options["v"]: util.print_reset(colors.CLEAR_LINE + colors.RED + f"Failed to find package {dep}")
+            dep_checksum, dep_sources, dep_repo = find_package(dep, config["repos"], config["dir"]["packages"], config["sources"])
+
+            if dep_checksum is not None:
+                info = retrieve_package_info(
+                            dep_sources, dep_checksum, dep, config,
+                            verbose=options["v"], skip_verification=options["u"]
+                        )
+
+                if len(info) > 0:
+                    if not dep in all_deps:
+                        all_deps.append(dep)
+                        deps = resolve_dependencies(info)
+                        for dep in deps:
+                            if not dep in all_deps:
+                                if is_installed(dep, config, options["r"]):
+                                    if options["v"]: print(colors.YELLOW + f"Package {dep} has already been installed")
+                                else:
+                                    to_check.append(dep)
+                elif options["v"]:
+                        util.print_reset(colors.CLEAR_LINE + colors.RED + f"Failed to retrieve info for {dep}")
+            else:
+                if options["v"]: util.print_reset(colors.CLEAR_LINE + colors.RED + f"Failed to find package {dep}")
 
     if len(all_deps) > 0:
         util.loading_bar(len(all_deps), len(all_deps) + len(to_check), "Resolved dependencies")
@@ -270,6 +280,7 @@ def install(args, options, config):
     if len(to_install) > 0:
 
 
+        print(colors.CLEAR_LINE + colors.RESET, end="")
         print(colors.BLUE + "The following packages will be installed:")
         print(end="\t")
         for d in to_install:
@@ -278,6 +289,7 @@ def install(args, options, config):
 
         if util.ask_confirmation(colors.BLUE + "Continue?", no_confirm=options["y"]):
 
+            downloaded = []
             for package in to_install:
                 checksum, sources, repo = find_package(package, config["repos"],
                         config["dir"]["packages"], config["sources"])
@@ -291,10 +303,14 @@ def install(args, options, config):
                         info, package, config, 
                         verbose=v, skip_verification=unsafe)
 
+                downloaded.append(package, package_path, info, 
+                                repo, sources[source], key
+                        )
                 install_package(package, package_path, info, 
                         repo, sources[source], key,
                         config, root=options["r"])
-                print(colors.BG_CYAN + colors.LIGHT_BLACK + f"Installed {package}" + colors.RESET)
+                util.fill_line(f"Installed {package}", colors.BG_CYAN + colors.LIGHT_BLACK, end="\n")
+
         else:
             print(colors.RED + "Action cancelled by user")
     else:
diff --git a/src/verbs/sync.py b/src/verbs/sync.py
index e71109d..85e3623 100644
--- a/src/verbs/sync.py
+++ b/src/verbs/sync.py
@@ -59,11 +59,19 @@ def save_package(package, info, location):
     util.mkdir(location)
     package_file = os.path.join(location, package)
     
+    exists = False
+    if os.path.exists(package_file):
+        with open(package_file, "r") as file:
+            text = file.read()
+            exists = info["checksum"] in text
+
     content = ""
     with open(package_file, "w") as file:
         file.write("checksum=" + info["checksum"] + "\n")
         file.write("sources=" + " ".join([source for source in info["sources"]]))
 
+    return exists
+
 
 ###### !!! #######
 # THIS SHOULD BE A USER ACTION 
@@ -125,6 +133,8 @@ def sync(args, options, config):
 
     v = options["v"]
 
+    new = 0
+    
     for repo in repos:
         if v: print(colors.LIGHT_BLACK + f"downloading package lists for {repo}...")
 
@@ -141,12 +151,17 @@ def sync(args, options, config):
         total = len(packages.items())
         for package,versions in packages.items():
             info = validate_package(package, versions, repo, verbose=v)
-            save_package(package, info, os.path.join(config["dir"]["packages"], repo))
+            if not save_package(package, info, os.path.join(config["dir"]["packages"], repo)):
+                new += 1
             done += 1
             util.loading_bar(done, total, f"Syncing {repo}")
 
         util.loading_bar(total, total, f"Synced {repo}")
         print(colors.RESET)
+
+    if new > 0:
+        util.fill_line(f"There are {new} new updates", colors.LIGHT_GREEN)
+
     
     if "key_authority" in config:
         imported = 0
diff --git a/src/xi.py b/src/xi.py
index f6089ec..aa819b4 100644
--- a/src/xi.py
+++ b/src/xi.py
@@ -1,5 +1,7 @@
 import options
 import config
+import util
+import colors
 
 from verbs.sync import sync
 from verbs.install import install
@@ -25,14 +27,21 @@ def main():
         options.print_usage()
         return
 
+    
     conf = config.parse_file(opts["c"])
     if len(args) > 0:
         verb = args[0].lower()
-        (
-            verbs[verb] if verb in verbs else search
-        )(
-            args[1:] if len(args) > 1 else [], opts, conf
-        )
+
+        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:
         options.print_usage()
         return
+
+    print(colors.RESET + colors.CLEAR_LINE, end="")
-- 
cgit v1.2.1