崇高文本 3:根据远程主机更改颜色主题的简单插件



设置:我使用Sublime Text 3(ST),我经常使用RemoteSubl在不同的远程工作区中打开Sublime + iTerm2的2-3个不同的会话。

使用一个简单的批处理脚本,我将 iTerm2 设置为在我 ssh 到不同的主机时更改颜色(通过激活不同的 iTerm 用户)。

我想知道是否可以对 RemoteSubl 做同样的事情?这样,当我从特定的主机/ip/端口打开某些东西时,Sublime会以不同的配色方案打开,具体取决于主机/ip/端口。


解决方案尝试:到目前为止,这是我尝试构建一个小插件,该插件在主机remote_host时更改配色方案。

import sublime
import sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
def run(self, view):
try:
host = view.settings().get('remote_subl.host')
print(host)
if host == 'remote_host':
view.settings().set(
'color_scheme',
'Packages/Color Scheme - Default/Mariana.tmTheme')
print(view.settings().get('color_scheme'))
except:
print("Not on remote_host")
pass

问题:在控制台中使用view.settings().get('remote_subl.host')时,它工作正常,并返回remote_host。但是,在运行脚本view.run_command('example')时,我得到"不在remote_host"打印,表明 try 循环由于某种原因失败。


在基思的建议之后

import sublime
import sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
def run(self, view):
view = self.view
host = view.settings().get('remote_subl.host', None)
print(host)
if host:
view.settings().set(
'color_scheme',
'Packages/Color Scheme - Default/Mariana.tmTheme')
print(view.settings().get('color_scheme'))
if host is None:
view.settings().set(
'color_scheme',
'Packages/Color Scheme - Default/Monokai.tmTheme')
print(view.settings().get('color_scheme'))

view不是传递给TextCommandrun方法的参数。相反,它是self上的属性。将其更改为以下内容应该有效:

import sublime
import sublime_plugin

class ExampleCommand(sublime_plugin.TextCommand):
def run(self, edit):
view = self.view
try:
host = view.settings().get('remote_subl.host')
print(host)
if host == 'dsintmain':
view.settings().set(
'color_scheme',
'Packages/Color Scheme - Default/Mariana.tmTheme')
print(view.settings().get('color_scheme'))
except:
print("Not on remote_host")
pass

我还建议打印发生的异常,以帮助将来调试此类内容。更好的是,与其期望在正常使用中出现异常,不如在settings上为get方法提供一个默认值(即None),并完全删除异常处理。

host = view.settings().get('remote_subl.host', None)

这样,如果确实出错,您将在 ST 控制台中看到回溯。

最新更新