forked from ThomasGerstenberg/serial_monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview_commands.py
67 lines (56 loc) · 2.38 KB
/
view_commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import sublime
import sublime_plugin
class SerialMonitorWriteCommand(sublime_plugin.TextCommand):
"""
Writes text (or a file) to the serial output view the command is run on
"""
def run(self, edit, **args):
"""
Runs the command to write to a serial output view
:param args: The args for writing to the view. Needs to contain:
"text": string of text to write to the view
or
"view_id": The id of the input view
"region_begin": Starting index for the input view
"region_end": Ending index for the input view
:type args: dict
:return:
"""
# Check if the end of the output file is visible. If so, enable the auto-scroll
should_autoscroll = self.view.visible_region().contains(self.view.size())
self.view.set_read_only(False)
if "text" in args:
self.view.insert(edit, self.view.size(), args["text"])
else:
view = sublime.View(args["view_id"])
begin = args["region_begin"]
end = args["region_end"]
self.view.insert(edit, self.view.size(), view.substr(sublime.Region(begin, end)))
self.view.set_read_only(True)
if should_autoscroll and not self.view.visible_region().contains(self.view.size()):
self.view.window().run_command("serial_monitor_scroll", {"view_id": self.view.id()})
class SerialMonitorEraseCommand(sublime_plugin.TextCommand):
"""
Clears the view
"""
def run(self, edit, **args):
self.view.set_read_only(False)
self.view.erase(edit, sublime.Region(0, self.view.size()))
self.view.set_read_only(True)
class SerialMonitorScrollCommand(sublime_plugin.WindowCommand):
"""
Scrolls to the end of a view
"""
def run(self, view_id):
last_focused = self.window.active_view()
view = sublime.View(view_id)
self.window.focus_view(view)
view.show(view.size())
self.window.focus_view(last_focused)
class SerialMonitorUpdateEntryCommand(sublime_plugin.TextCommand):
"""
Updates the serial monitor input view with the text provided
"""
def run(self, edit, text):
self.view.erase(edit, sublime.Region(0, self.view.size()))
self.view.insert(edit, 0, text)