summaryrefslogtreecommitdiff
path: root/src/verbs/search.py
blob: 498a88e172e8949a991043d17c22a947264668fe (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
import os
import sys
import colors
import util
import shutil

from verbs.install import find_package, retrieve_package_info
from verbs.sync import sync

def list_repos(repos, packages_dir, sources):
    return [
            f"{repo}/{file}" for repo in repos for file in os.listdir(os.path.join(packages_dir, repo)) 
            ]

def search(args, options, config):
    if not options["l"]:
        sync(args, options, config)

    if len(args) > 0:
        packages = list_repos(config["repos"], config["dir"]["packages"], config["sources"])
        for package in args:

            # TODO fuzzy searching here
            results = [p for p in packages if package.lower() in p.lower()]
    
            if len(results) > 0:
                print(colors.GREEN + f"Search results for {package}:")
                for r in results:
                    print(colors.LIGHT_GREEN + f"\t{r}")

                print(colors.RESET, end="")
                sys.exit(0)
            else:
                print(colors.RED + f"Package {package} could not be found")
                print(colors.RESET, end="")
                sys.exit(1)
    else:
        print(colors.LIGHT_RED + "Nothing to do")