Skip to content

Commit

Permalink
description in commands submenu
Browse files Browse the repository at this point in the history
  • Loading branch information
yinan-c committed Jun 1, 2023
1 parent a15458f commit feca9e0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 32 deletions.
19 changes: 7 additions & 12 deletions brewinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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({
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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':
Expand All @@ -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)
Expand All @@ -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']
Expand Down
File renamed without changes
2 changes: 1 addition & 1 deletion info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,7 @@ More detailed README with figures can be found on [GitHub](https://github.com/yi
</dict>
</array>
<key>version</key>
<string>1.1.1</string>
<string>1.2</string>
<key>webaddress</key>
<string>github.com/yinan-c/alfred-homebrew</string>
</dict>
Expand Down
56 changes: 37 additions & 19 deletions runcommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit feca9e0

Please sign in to comment.