1
|
|
|
""" |
2
|
|
|
This module is about refreshing Rainmeter from within Sublime Text. |
3
|
|
|
|
4
|
|
|
This can be either activated via a command or as part of the build system. |
5
|
|
|
""" |
6
|
|
|
|
7
|
|
|
|
8
|
|
|
import os.path |
9
|
|
|
|
10
|
|
|
import sublime |
11
|
|
|
import sublime_plugin |
12
|
|
|
from . import rainmeter |
13
|
|
|
from .path.program_path_provider import get_cached_program_path |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
def calculate_refresh_commands(rm_exe, config, fil, activate, is_ini): |
17
|
|
|
"""Detect if an activate config flag is required or not.""" |
18
|
|
|
if activate: |
19
|
|
|
cmds = [rm_exe, "!ActivateConfig", config] |
20
|
|
|
if is_ini: |
21
|
|
|
cmds.append(fil) |
22
|
|
|
return cmds |
23
|
|
|
else: |
24
|
|
|
return None |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
class RainmeterRefreshConfigCommand(sublime_plugin.ApplicationCommand): |
28
|
|
|
"""Refresh a given skin file, or Rainmeter if no path is specified.""" |
29
|
|
|
|
30
|
|
|
def run(self, cmd): # pylint: disable=R0201; sublime text API, no need for class reference |
31
|
|
|
"""Called when the command is run.""" |
32
|
|
|
# Get Rainmeter exe path |
33
|
|
|
rm_path = get_cached_program_path() |
34
|
|
|
|
35
|
|
|
if not rm_path: |
36
|
|
|
sublime.error_message( |
37
|
|
|
"Error while trying to refresh Rainmeter" + |
38
|
|
|
" skin: The Rainmeter executable could not be found." + |
39
|
|
|
" Please check the value of your \"rainmeter_path\"" + |
40
|
|
|
" setting.") |
41
|
|
|
return |
42
|
|
|
rainmeter_exe = os.path.join(rm_path, "Rainmeter.exe") |
43
|
|
|
|
44
|
|
|
# Refresh skin (or whole rainmeter if no skin specified) |
45
|
|
|
if not cmd: |
46
|
|
|
sublime.status_message("Refreshing Rainmeter") |
47
|
|
|
sublime.active_window().run_command( |
48
|
|
|
"exec", |
49
|
|
|
{"cmd": [rainmeter_exe, "!RefreshApp"]} |
50
|
|
|
) |
51
|
|
|
else: |
52
|
|
|
config = rainmeter.get_current_config(cmd[0]) |
53
|
|
|
if not config: |
54
|
|
|
sublime.error_message( |
55
|
|
|
"Error while trying to refresh Rainmeter skin:" + |
56
|
|
|
" The config could not be found. Please check the" + |
57
|
|
|
" path of the config and your" + |
58
|
|
|
" \"rainmeter_skins_path\" setting.") |
59
|
|
|
|
60
|
|
|
# by rainmeter documentation |
61
|
|
|
# If not specified, the next .ini file variant in the config folder is activated. |
62
|
|
|
fil = rainmeter.get_current_file(cmd[0]) |
63
|
|
|
if not fil: |
64
|
|
|
fil = "" |
65
|
|
|
|
66
|
|
|
sublime.status_message("Refreshing config: " + config) |
67
|
|
|
|
68
|
|
|
# Load activate setting |
69
|
|
|
settings = sublime.load_settings("Rainmeter.sublime-settings") |
70
|
|
|
activate = settings.get("rainmeter_refresh_and_activate", True) |
71
|
|
|
is_ini = fil.endswith(".ini") |
72
|
|
|
|
73
|
|
|
refresh_commands = calculate_refresh_commands(rainmeter_exe, config, fil, activate, is_ini) |
74
|
|
|
if refresh_commands: |
75
|
|
|
sublime.active_window().run_command("exec", {"cmd": refresh_commands}) |
76
|
|
|
refresh_config = [ |
77
|
|
|
rainmeter_exe, "!Refresh", config |
78
|
|
|
] |
79
|
|
|
sublime.active_window().run_command("exec", {"cmd": refresh_config}) |
80
|
|
|
|
81
|
|
|
def description(self): # pylint: disable=R0201; sublime text API, no need for class reference |
82
|
|
|
""" |
83
|
|
|
Return a description of the command with the given arguments. |
84
|
|
|
|
85
|
|
|
Used in the menu, if no caption is provided. |
86
|
|
|
|
87
|
|
|
Return None to get the default description. |
88
|
|
|
""" |
89
|
|
|
return "Refresh Rainmeter Config" |
90
|
|
|
|
91
|
|
|
|
92
|
|
|
class RainmeterRefreshCommand(sublime_plugin.ApplicationCommand): # pylint: disable=R0903; sublime text API, methods are overriden |
93
|
|
|
"""Refresh Rainmeter.""" |
94
|
|
|
|
95
|
|
|
def run(self): # pylint: disable=R0201; sublime text API, no need for class reference |
96
|
|
|
"""Called when the command is run.""" |
97
|
|
|
sublime.run_command("rainmeter_refresh_config", {"cmd": []}) |
98
|
|
|
|
99
|
|
|
|
100
|
|
|
class RainmeterRefreshCurrentSkinCommand(sublime_plugin.TextCommand): |
101
|
|
|
""" |
102
|
|
|
TextCommands are instantiated once per view. |
103
|
|
|
|
104
|
|
|
The View object may be retrieved via self.view. |
105
|
|
|
|
106
|
|
|
Refresh the current skin file opened in a view. |
107
|
|
|
""" |
108
|
|
|
|
109
|
|
|
def run(self, _): # pylint: disable=R0201; sublime text API, no need for class reference |
110
|
|
|
""" |
111
|
|
|
Called when the command is run. |
112
|
|
|
|
113
|
|
|
edit param is not used |
114
|
|
|
""" |
115
|
|
|
# Get current file's path |
116
|
|
|
filepath = self.view.file_name() |
117
|
|
|
|
118
|
|
|
if not filepath: |
119
|
|
|
return |
120
|
|
|
|
121
|
|
|
# Refresh config |
122
|
|
|
sublime.run_command("rainmeter_refresh_config", {"cmd": [filepath]}) |
123
|
|
|
|
124
|
|
|
def is_enabled(self): # pylint: disable=R0201; sublime text API, no need for class reference |
125
|
|
|
""" |
126
|
|
|
Return True if the command is able to be run at this time. |
127
|
|
|
|
128
|
|
|
The default implementation simply always returns True. |
129
|
|
|
""" |
130
|
|
|
# Check if current syntax is rainmeter |
131
|
|
|
israinmeter = self.view.score_selector(self.view.sel()[0].a, |
132
|
|
|
"source.rainmeter") |
133
|
|
|
|
134
|
|
|
return israinmeter > 0 |
135
|
|
|
|
136
|
|
|
def description(self): # pylint: disable=R0201; sublime text API, no need for class reference |
137
|
|
|
""" |
138
|
|
|
Return a description of the command with the given arguments. |
139
|
|
|
|
140
|
|
|
Used in the menus, and for Undo/Redo descriptions. |
141
|
|
|
|
142
|
|
|
Return None to get the default description. |
143
|
|
|
""" |
144
|
|
|
return "Refresh Current Rainmeter Skin" |
145
|
|
|
|