Last active
August 29, 2015 14:16
-
-
Save FichteFoll/a1ec2e3225b96c5489e6 to your computer and use it in GitHub Desktop.
Bootstraps the sublime.log_* functions to automatically toggle when called with no parameter (or `None`)
This file contains hidden or 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
import sublime | |
from functools import partial | |
function_names = ('input', 'commands', 'result_regex', 'indexing') | |
# Cleanup | |
if hasattr(sublime, 'logging_unbootstrap'): | |
sublime.logging_unbootstrap() | |
# Assure this exists | |
if not hasattr(sublime, 'logging_cache'): | |
setattr(sublime, 'logging_cache', {}) | |
# Collect original functions | |
original_functions = {name: getattr(sublime, "log_" + name) | |
for name in function_names} | |
# Override functions with our new function | |
def toogle_logging(name, value=None): | |
if value is None: | |
value = not sublime.logging_cache.get(name, False) | |
if value in (True, False): | |
sublime.logging_cache[name] = value | |
original_functions[name](value) | |
for name in function_names: | |
setattr(sublime, "log_" + name, partial(toogle_logging, name)) | |
# Register cleanup | |
def unbootstrap(): | |
for name, func in original_functions.items(): | |
setattr(sublime, "log_" + name, func) | |
delattr(sublime, 'logging_unbootstrap') | |
setattr(sublime, 'logging_unbootstrap', unbootstrap) | |
# Done | |
print("Note: sublime.log* methods have been overridden and now toggle when " | |
"called without a parameter") | |
def plugin_unloaded(): | |
unbootstrap() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment