Skip to content

Commit

Permalink
Merge pull request #12 from maltsev/global_search
Browse files Browse the repository at this point in the history
Global shortcuts search
  • Loading branch information
mutdmour authored Jul 21, 2018
2 parents ae43326 + 8913b3a commit 64e5f9d
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 48 deletions.
55 changes: 30 additions & 25 deletions src/cheatsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@

import sys
import os
from workflow import Workflow, ICON_INFO
from workflow import Workflow, ICON_INFO, ICON_WARNING
import cPickle

wf = None
log = None
apps = None
custom = None

nothing_found_error_text = 'Nothing found'

app_icons_dir = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'apps/icons'
Expand Down Expand Up @@ -91,7 +93,18 @@ def run(args):
u'Ctrl ⏎',
icon=ICON_INFO)
action = ""
addShortcuts(app, action)
if addShortcuts(app, action) == 0:
wf.add_item(nothing_found_error_text, icon=ICON_WARNING)
elif (u'--search-global' in args):
shortcuts_added = 0
for app in apps:
shortcuts_added += addShortcuts(app, command, True)

for app in custom:
shortcuts_added += addShortcuts(app, command, True)

if shortcuts_added == 0:
wf.add_item(nothing_found_error_text, icon=ICON_WARNING)
else:
filter(command, apps)

Expand All @@ -102,7 +115,7 @@ def filter(query, items):
items
)
if (len(matching) == 0):
wf.add_item('none found')
wf.add_item(nothing_found_error_text, icon=ICON_WARNING)
else:
addApps(matching)

Expand All @@ -119,15 +132,6 @@ def getApps():

return apps

def getShorcuts(app):
opts = []
if (app in shortcuts):
opts = shortcuts[app].keys()
if (app in custom):
custom_app_shortcuts = custom[app].keys()
opts = list(set(opts)|set(custom_app_shortcuts))
return opts

def getAppIconPath(app):
icon_path = os.path.join(app_icons_dir, app + '.png')
if not os.path.isfile(icon_path):
Expand All @@ -145,27 +149,28 @@ def addApps(items):
arg=item,
valid=True)

def addShortcuts(app, search):
def addShortcuts(app, search, include_app_in_search=False):
actions = {}
if (app in shortcuts):
actions = shortcuts[app]
if (app in custom):
actions.update(custom[app])

icon_path = getAppIconPath(app)

if (search):
opts = getShorcuts(app)
if include_app_in_search:
actions_pairs = [(action, shortcut, app + ' ' + action) for action, shortcut in actions.items()]
else:
actions_pairs = [(action, shortcut, action) for action, shortcut in actions.items()]

matching = wf.filter(search, opts)
if (len(matching) == 0):
wf.add_item('none found')
else:
for k in matching:
addShortcut(k, actions[k], app, icon_path)
if search:
actions_pairs_to_show = wf.filter(search, actions_pairs, key=lambda a: a[2])
else:
for k in actions:
addShortcut(k, actions[k], app, icon_path)
actions_pairs_to_show = actions_pairs

icon_path = getAppIconPath(app)
for action, shortcut, _ in actions_pairs_to_show:
addShortcut(action, shortcut, app, icon_path)

return len(actions_pairs_to_show)


def addShortcut(action, shortcut, app, icon_path):
Expand Down
113 changes: 90 additions & 23 deletions src/info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@
<false/>
</dict>
</array>
<key>2E590801-05A0-4265-8987-2E2ECFC949C8</key>
<array>
<dict>
<key>destinationuid</key>
<string>EDF72E90-47D6-4C41-AD90-B95E2FB7C1E5</string>
<key>modifiers</key>
<integer>262144</integer>
<key>modifiersubtext</key>
<string></string>
<key>vitoclose</key>
<false/>
</dict>
</array>
<key>4CD04C2A-4956-4EA4-9CEF-7F65287C49DD</key>
<array>
<dict>
Expand Down Expand Up @@ -486,6 +499,53 @@
<key>version</key>
<integer>1</integer>
</dict>
<dict>
<key>config</key>
<dict>
<key>alfredfiltersresults</key>
<false/>
<key>alfredfiltersresultsmatchmode</key>
<integer>0</integer>
<key>argumenttrimmode</key>
<integer>0</integer>
<key>argumenttype</key>
<integer>0</integer>
<key>escaping</key>
<integer>102</integer>
<key>keyword</key>
<string>cheatsheetg</string>
<key>queuedelaycustom</key>
<integer>3</integer>
<key>queuedelayimmediatelyinitially</key>
<true/>
<key>queuedelaymode</key>
<integer>0</integer>
<key>queuemode</key>
<integer>1</integer>
<key>runningsubtext</key>
<string>getting shortcuts</string>
<key>script</key>
<string>/usr/bin/env python -O cheatsheet.py "{query}" --search-global</string>
<key>scriptargtype</key>
<integer>0</integer>
<key>scriptfile</key>
<string></string>
<key>subtext</key>
<string>get shortcuts</string>
<key>title</key>
<string>cheatsheet global</string>
<key>type</key>
<integer>0</integer>
<key>withspace</key>
<true/>
</dict>
<key>type</key>
<string>alfred.workflow.input.scriptfilter</string>
<key>uid</key>
<string>2E590801-05A0-4265-8987-2E2ECFC949C8</string>
<key>version</key>
<integer>2</integer>
</dict>
</array>
<key>readme</key>
<string></string>
Expand All @@ -494,93 +554,100 @@
<key>0327F3D7-7D0F-44EA-B0FA-08FC32DBE1DA</key>
<dict>
<key>xpos</key>
<integer>260</integer>
<integer>230</integer>
<key>ypos</key>
<integer>170</integer>
</dict>
<key>1822C5D0-3B68-4ED0-807F-14DFE273F3F3</key>
<dict>
<key>xpos</key>
<integer>440</integer>
<integer>430</integer>
<key>ypos</key>
<integer>10</integer>
<integer>20</integer>
</dict>
<key>1A086E06-E707-4A15-80A2-702A9FEB49A3</key>
<dict>
<key>xpos</key>
<integer>180</integer>
<integer>170</integer>
<key>ypos</key>
<integer>40</integer>
<integer>50</integer>
</dict>
<key>2E590801-05A0-4265-8987-2E2ECFC949C8</key>
<dict>
<key>xpos</key>
<integer>10</integer>
<key>ypos</key>
<integer>310</integer>
</dict>
<key>42D16115-359D-4EA4-9AF8-0659BFF3F294</key>
<dict>
<key>xpos</key>
<integer>970</integer>
<integer>1180</integer>
<key>ypos</key>
<integer>50</integer>
<integer>310</integer>
</dict>
<key>4CD04C2A-4956-4EA4-9CEF-7F65287C49DD</key>
<dict>
<key>xpos</key>
<integer>600</integer>
<integer>820</integer>
<key>ypos</key>
<integer>50</integer>
<integer>310</integer>
</dict>
<key>61503630-0367-4047-90B3-D78A9C380E92</key>
<dict>
<key>xpos</key>
<integer>10</integer>
<key>ypos</key>
<integer>120</integer>
<integer>90</integer>
</dict>
<key>6F425287-3622-49F6-8570-B8FE647090F2</key>
<dict>
<key>xpos</key>
<integer>380</integer>
<integer>370</integer>
<key>ypos</key>
<integer>40</integer>
<integer>50</integer>
</dict>
<key>938A0BFA-E8CE-4604-BD90-DC46BB078F68</key>
<dict>
<key>xpos</key>
<integer>180</integer>
<integer>170</integer>
<key>ypos</key>
<integer>200</integer>
</dict>
<key>BEB063AE-6618-47F9-ADFF-219BC48F9EA2</key>
<dict>
<key>xpos</key>
<integer>600</integer>
<integer>760</integer>
<key>ypos</key>
<integer>210</integer>
<integer>340</integer>
</dict>
<key>D438B820-3D0E-42F5-8464-13224C161E31</key>
<dict>
<key>xpos</key>
<integer>240</integer>
<integer>230</integer>
<key>ypos</key>
<integer>10</integer>
<integer>20</integer>
</dict>
<key>EDF72E90-47D6-4C41-AD90-B95E2FB7C1E5</key>
<dict>
<key>xpos</key>
<integer>470</integer>
<integer>620</integer>
<key>ypos</key>
<integer>180</integer>
<integer>310</integer>
</dict>
<key>F38BF85B-1DA9-4112-8FB4-CE76572C3F3D</key>
<dict>
<key>xpos</key>
<integer>820</integer>
<integer>1030</integer>
<key>ypos</key>
<integer>50</integer>
<integer>310</integer>
</dict>
<key>FF5EF30B-2BA1-4798-8F07-0443E48CC4B6</key>
<dict>
<key>xpos</key>
<integer>760</integer>
<integer>970</integer>
<key>ypos</key>
<integer>80</integer>
<integer>340</integer>
</dict>
</dict>
<key>webaddress</key>
Expand Down

0 comments on commit 64e5f9d

Please sign in to comment.