Skip to content

Commit

Permalink
Make items valid & add Snippet Trigger. Closes #2
Browse files Browse the repository at this point in the history
- Update Alfred-Workflow
- Add notification if update is available
- Reload results if thumbnails are being generated
- Add Snippet Trigger to search and paste in active app
- Smarted thumbnail queue
  • Loading branch information
deanishe committed Aug 24, 2018
1 parent edd6d3b commit b8d5cac
Show file tree
Hide file tree
Showing 16 changed files with 1,804 additions and 682 deletions.
179 changes: 179 additions & 0 deletions .gitignore
@@ -0,0 +1,179 @@

*.dist-info

# Created by https://www.gitignore.io/api/python,sublimetext,vim

### Python ###
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/

### Python Patch ###
.venv/

### Python.VirtualEnv Stack ###
# Virtualenv
# http://iamzed.com/2009/05/07/a-primer-on-virtualenv/
[Bb]in
[Ii]nclude
[Ll]ib
[Ll]ib64
[Ll]ocal
[Ss]cripts
pyvenv.cfg
pip-selfcheck.json

### SublimeText ###
# Cache files for Sublime Text
*.tmlanguage.cache
*.tmPreferences.cache
*.stTheme.cache

# Workspace files are user-specific
*.sublime-workspace

# Project files should be checked into the repository, unless a significant
# proportion of contributors will probably not be using Sublime Text
# *.sublime-project

# SFTP configuration file
sftp-config.json

# Package control specific files
Package Control.last-run
Package Control.ca-list
Package Control.ca-bundle
Package Control.system-ca-bundle
Package Control.cache/
Package Control.ca-certs/
Package Control.merged-ca-bundle
Package Control.user-ca-bundle
oscrypto-ca-bundle.crt
bh_unicode_properties.cache

# Sublime-github package stores a github token in this file
# https://packagecontrol.io/packages/sublime-github
GitHub.sublime-settings

### Vim ###
# Swap
[._]*.s[a-v][a-z]
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]

# Session
Session.vim

# Temporary
.netrwhist
*~
# Auto-generated tag files
tags
# Persistent undo
[._]*.un~


# End of https://www.gitignore.io/api/python,sublimetext,vim
Binary file removed Say it with GIFs-1.0.alfredworkflow
Binary file not shown.
Binary file added Say-it-with-GIFs-1.1.alfredworkflow
Binary file not shown.
68 changes: 47 additions & 21 deletions src/gifs.py
Expand Up @@ -8,7 +8,7 @@
# Created on 2016-10-19
#

"""Search GIFs"""
"""Search GIFs."""

from __future__ import print_function, unicode_literals, absolute_import

Expand All @@ -19,19 +19,25 @@
from urllib import quote

from thumbnails import Thumbs
from workflow import Workflow3, ICON_WARNING
from workflow import Workflow3
from workflow.background import is_running, run_in_background

log = None

HELP_URL = 'https://github.com/deanishe/alfred-gifs'
GITHUB = 'deanishe/alfred-gifs'
ICON_UPDATE = 'update-available.png'

# Local GIF directory
GIF_DIR = os.path.expanduser(os.getenv('GIF_DIR'))
GIF_DIR = os.path.expanduser(os.getenv('GIF_DIR')).decode('utf-8')
# Remote URL that corresponds to above
GIF_URL = os.getenv('GIF_URL')
GIF_URL = os.getenv('GIF_URL').decode('utf-8')

# Whether scripts was called via Snippet Trigger
IS_SNIPPET = os.getenv('focusedapp') is not None

# Whether to Quicklook local files or URLs
PREVIEW_LOCAL = os.getenv('PREVIEW_LOCAL', '') not in ('', '0')

Gif = namedtuple('Gif', 'name url path icon')

Expand All @@ -41,12 +47,15 @@
def load_gifs():
"""Return list of `Gif` objects."""
gifs = []

for fn in os.listdir(GIF_DIR):

if fn.lower().endswith('.gif'):
name = os.path.splitext(fn)[0]
path = os.path.join(GIF_DIR, fn)
icon = thumbs.thumbnail(path)
url = os.path.join(GIF_URL, quote(fn))

gif = Gif(name, url, path, icon)
gifs.append(gif)

Expand All @@ -70,51 +79,68 @@ def main(wf):
# Initialise thumbnail generator
thumbs = Thumbs(wf.cachefile('thumbs'))

show_uids = True
query = None
if len(wf.args):
query = wf.args[0].strip()

if wf.update_available and not query:
show_uids = False # force item to top
wf.add_item('An Update is Available',
'↩ or ⇥ to install update',
valid=False,
autocomplete='workflow:update',
icon=ICON_UPDATE)

gifs = load_gifs()

if query:
gifs = wf.filter(query, gifs, key=attrgetter('name'), min_score=30)

if not gifs:
wf.add_item('No matching GIFs',
'Try a different query',
icon=ICON_WARNING)
wf.send_feedback()
return

# Display results
for gif in gifs:
action = 'copy'
subtitle = 'Copy URL to clipboard'
if IS_SNIPPET:
action = 'paste'
subtitle = 'Paste URL to active app'

it = wf.add_item(gif.name,
'Copy URL to clipboard',
subtitle,
arg=gif.url,
quicklookurl=gif.url,
icon=gif.icon)
quicklookurl=gif.path if PREVIEW_LOCAL else gif.url,
uid=gif.url if show_uids else None,
icon=gif.icon,
valid=True)

it.setvar('action', action)

# Alternate actions
mod = it.add_modifier('cmd', 'Open in Browser', arg=gif.url)
mod.setvar('action', 'browse')

mod = it.add_modifier('alt', 'Copy as BB Code',
mod = it.add_modifier('alt', action.title() + ' as BB Code',
arg=bb_image(gif.url))
mod.setvar('action', 'bbcode')

mod = it.add_modifier('ctrl', 'Copy as Markdown',
mod = it.add_modifier('ctrl', action.title() + ' as Markdown',
arg=markdown_image(gif.url))
mod.setvar('action', 'markdown')

wf.send_feedback()
mod = it.add_modifier('shift', 'Paste URL in active app', arg=gif.url)
mod.setvar('action', 'paste')

# Generate thumbnails if necessary
thumbs.save_queue()
if thumbs.has_queue and not is_running('generate_thumbnails'):
running = is_running('generate_thumbnails')
if not running and thumbs.queue_size:
run_in_background('generate_thumbnails', ['/usr/bin/python',
wf.workflowfile('thumbnails.py')])
running = True

return 0
if running:
wf.rerun = 1

wf.warn_empty('No matching GIFs', 'Try a different query?')
wf.send_feedback()


if __name__ == '__main__':
Expand Down

0 comments on commit b8d5cac

Please sign in to comment.