From feca9e08fbcd31cfcd360413baf563cee546204b Mon Sep 17 00:00:00 2001 From: Yinan <95043151+yinan-c@users.noreply.github.com> Date: Thu, 1 Jun 2023 17:26:20 +0100 Subject: [PATCH] description in commands submenu --- brewinfo.py | 19 ++++------- icons/{brew.png => formula.png} | Bin info.plist | 2 +- runcommands.py | 56 +++++++++++++++++++++----------- 4 files changed, 45 insertions(+), 32 deletions(-) rename icons/{brew.png => formula.png} (100%) diff --git a/brewinfo.py b/brewinfo.py index 6717e24..464e85c 100644 --- a/brewinfo.py +++ b/brewinfo.py @@ -27,7 +27,7 @@ def get_outdated_list(brewtype='all'): }, "arg": 'brew upgrade ' + name, "autocomplete": name, - "quicklookurl": f'https://formulae.brew.sh/formula/{name}#default' + "quicklookurl": f'https://formulae.brew.sh/formula/{name}' }) if 'casks' in outdated_list: @@ -44,7 +44,7 @@ def get_outdated_list(brewtype='all'): }, "arg": 'brew upgrade --cask ' + name, "autocomplete": name, - "quicklookurl": f'https://formulae.brew.sh/cask/{name}#default' + "quicklookurl": f'https://formulae.brew.sh/cask/{name}' }) if not result["items"]: result["items"].append({ @@ -80,7 +80,7 @@ def get_brew_leaves(): }, "arg": line, "autocomplete": line, - "quicklookurl": f'https://formulae.brew.sh/formula/{line}#default' + "quicklookurl": f'https://formulae.brew.sh/formula/{line}' }) return result @@ -109,14 +109,9 @@ def get_brew_list(brewtype='all'): return result def get_all_formula_names(brewtype): - if brewtype == 'cask': - response = requests.get('https://formulae.brew.sh/api/cask.json') - icon_path = {"path": "icons/cask.png"} - elif brewtype == 'formula': - response = requests.get('https://formulae.brew.sh/api/formula.json') - icon_path = {"path": "icons/brew.png"} + response = requests.get('https://formulae.brew.sh/api/'+brewtype+'.json') + icon_path = {"path": f"icons/{brewtype}.png"} data = response.json() - items = [] for item in data: if brewtype == 'cask': @@ -136,7 +131,7 @@ def get_all_formula_names(brewtype): "arg": token, "icon": icon_path, "autocomplete": token, - "quicklookurl": f'https://formulae.brew.sh/{brewtype}/{token}#default', + "quicklookurl": f'https://formulae.brew.sh/{brewtype}/{token}', "match": brewtype + ' ' + token } items.append(formula) @@ -147,7 +142,7 @@ def get_info(brewtype,formula_name): output_data = {"items": []} token = formula_name.lower() response = requests.get(f'https://formulae.brew.sh/api/{brewtype}/{token}.json') - info_page = f'https://formulae.brew.sh/{brewtype}/{token}#default' + info_page = f'https://formulae.brew.sh/{brewtype}/{token}' data = response.json() if brewtype == 'cask': version = data['version'] diff --git a/icons/brew.png b/icons/formula.png similarity index 100% rename from icons/brew.png rename to icons/formula.png diff --git a/info.plist b/info.plist index 11b0d48..f45aca9 100644 --- a/info.plist +++ b/info.plist @@ -1108,7 +1108,7 @@ More detailed README with figures can be found on [GitHub](https://github.com/yi version - 1.1.1 + 1.2 webaddress github.com/yinan-c/alfred-homebrew diff --git a/runcommands.py b/runcommands.py index 6df8264..9115167 100644 --- a/runcommands.py +++ b/runcommands.py @@ -3,9 +3,8 @@ import json from brewinfo import get_brew_list from brewinfo import get_info -from brewinfo import get_brew_leaves -def check_formula_and_compare_versions(formula_name, version): +def compare_versions(formula_name, version): brew_list = get_brew_list() for item in brew_list["items"]: if item["arg"] == formula_name: @@ -14,41 +13,60 @@ def check_formula_and_compare_versions(formula_name, version): return "Installed and version matches.", installed_version else: return "Installed but version does not match.", installed_version - return "Formula not installed.", None + return "Not installed.", None def get_commands(brewtype,formula_name): token = formula_name.lower() info_command = 'brew info '+ token + response = requests.get('https://formulae.brew.sh/api/'+brewtype+'/'+token+'.json') + data = response.json() + icon_path = {"path": f"icons/{brewtype}.png"} + if brewtype == 'cask': - response = requests.get('https://formulae.brew.sh/api/cask/'+token+'.json') install_command = 'brew install --cask '+ token uninstall_command = 'brew uninstall --cask '+ token force_uninstall_command = 'brew uninstall --force --zap --cask '+ token upgrade_command = 'brew upgrade --cask '+ token + name = data['name'][0] + version = data['version'] + try: + subtitle = name + ' ℹ️ '+ data['desc'] + except: + subtitle = name + elif brewtype == 'formula': - response = requests.get('https://formulae.brew.sh/api/formula/'+token+'.json') install_command = 'brew install '+ token uninstall_command = 'brew uninstall '+ token upgrade_command = 'brew upgrade '+ token - data = response.json() - if brewtype == 'cask': - name = data['name'][0] - version = data['version'] - elif brewtype == 'formula': name = data['name'] + subtitle = data['desc'] version = data['versions']['stable'] + + information = { + "valid": True, + "title": token, + "subtitle": subtitle, + "arg": "brew info "+ token, + "icon": icon_path, + "autocomplete": token, + "quicklookurl": f'https://formulae.brew.sh/{brewtype}/{token}', + "match": brewtype + ' ' + token + } + back_button = { + "title": "Back to list", + "arg": 1, + "icon": {"path": "icons/back.png"}, + "valid": True + } + output_data = { "items": [ - { - "title": "Back to search", - "arg": 1, - "icon": {"path": "icons/back.png"}, - "valid": True - } + back_button, + information, ] } - status, installed_version = check_formula_and_compare_versions(token, version) - if status == "Formula not installed.": + status, installed_version = compare_versions(token, version) + if status == "Not installed.": output_data["items"].extend([ { "valid": True, @@ -153,7 +171,7 @@ def get_commands(brewtype,formula_name): output_data = { "items": [ { - "title": "No cask/formula found, back to search", + "title": "No cask/formula found yet, ⏎ back to list", "arg": 1, "icon": {"path": "icons/back.png"}, "valid": True