Skip to content

Instantly share code, notes, and snippets.

@jbgutierrez
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbgutierrez/733f56072b5644642aab to your computer and use it in GitHub Desktop.
Save jbgutierrez/733f56072b5644642aab to your computer and use it in GitHub Desktop.
Using sublime quick panel and hooks to retrieve/save content from an external ruby script (Sublime Plugin)
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sublime, sublime_plugin
import tempfile, subprocess, collections
DEBUG_MODE = False
IS_OSX = sublime.platform() == 'osx'
settings = sublime.load_settings('SpotsManager.sublime-settings')
spots = [ "spot:" + item for item in settings.get('spots') ]
variants = [ "variant:" + item for item in settings.get('variants') ]
list = spots + variants
file_names = {}
def perform(action,file_name):
if file_names[file_name] == 'variant': action += '_variant'
args = collections.deque(["ruby", settings.get('spots-manager-cli'), action, "--file", file_name])
if IS_OSX: args.extendleft(reversed(["/usr/local/Cellar/rbenv/0.4.0/libexec/rbenv", "exec"]))
sublime.active_window().run_command("show_panel", {"panel": "console", "show": True})
if DEBUG_MODE:
print action + '::' + file_name
else:
proc = subprocess.Popen(args, stderr = subprocess.PIPE)
print proc.stderr.read()
class UpdateSpotsListener(sublime_plugin.EventListener):
def on_post_save(self, view):
file_name = view.file_name()
if file_name in file_names: perform('upload', file_name)
class UpdateSpotsCommand(sublime_plugin.TextCommand):
def on_done(self, idx):
if idx == -1: return
tokens = list[idx].split(':')
kind = tokens.pop(0)
file_name = tempfile.gettempdir() + '/' + ':'.join(tokens)
file_names[file_name] = kind
perform('download',file_name)
view = self.view.window().open_file(file_name)
def run(self,view):
self.view.window().show_quick_panel(list, self.on_done)
[
{ "caption": "Update Spots", "command": "update_spots" }
]
[
{ "keys": ["ctrl+shift+8"], "command": "update_spots" }
]
{
"spots-manager-cli": "...",
"spots": [
"...",
"..."
],
"variants": [
"...",
"..."
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment