Last active
August 29, 2015 14:03
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
[ | |
{ "caption": "Update Spots", "command": "update_spots" } | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ "keys": ["ctrl+shift+8"], "command": "update_spots" } | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"spots-manager-cli": "...", | |
"spots": [ | |
"...", | |
"..." | |
], | |
"variants": [ | |
"...", | |
"..." | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment