/
history.py
301 lines (256 loc) · 11.4 KB
/
history.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
# (see spyder/__init__.py for details)
"""Console History Plugin"""
# Standard library imports
import os.path as osp
import sys
# Third party imports
from qtpy.QtCore import Signal, Slot
from qtpy.QtWidgets import (QGroupBox, QHBoxLayout, QInputDialog,
QVBoxLayout, QWidget)
# Local imports
from spyder.utils import encoding
from spyder.config.base import _
from spyder.api.plugins import SpyderPluginWidget
from spyder.api.preferences import PluginConfigPage
from spyder.py3compat import is_text_string, to_text_string
from spyder.utils import icon_manager as ima
from spyder.utils.qthelpers import add_actions, create_action
from spyder.widgets.tabs import Tabs
from spyder.widgets.sourcecode import codeeditor
from spyder.widgets.findreplace import FindReplace
class HistoryConfigPage(PluginConfigPage):
def get_icon(self):
return ima.icon('history')
def setup_page(self):
settings_group = QGroupBox(_("Settings"))
hist_spin = self.create_spinbox(
_("History depth: "), _(" entries"),
'max_entries', min_=10, max_=10000, step=10,
tip=_("Set maximum line count"))
sourcecode_group = QGroupBox(_("Source code"))
wrap_mode_box = self.create_checkbox(_("Wrap lines"), 'wrap')
linenumbers_mode_box = self.create_checkbox(_("Show line numbers"),
'line_numbers')
go_to_eof_box = self.create_checkbox(
_("Scroll automatically to last entry"), 'go_to_eof')
settings_layout = QVBoxLayout()
settings_layout.addWidget(hist_spin)
settings_group.setLayout(settings_layout)
sourcecode_layout = QVBoxLayout()
sourcecode_layout.addWidget(wrap_mode_box)
sourcecode_layout.addWidget(linenumbers_mode_box)
sourcecode_layout.addWidget(go_to_eof_box)
sourcecode_group.setLayout(sourcecode_layout)
vlayout = QVBoxLayout()
vlayout.addWidget(settings_group)
vlayout.addWidget(sourcecode_group)
vlayout.addStretch(1)
self.setLayout(vlayout)
class HistoryLog(SpyderPluginWidget):
"""
History log widget
"""
CONF_SECTION = 'historylog'
CONFIGWIDGET_CLASS = HistoryConfigPage
focus_changed = Signal()
def __init__(self, parent):
SpyderPluginWidget.__init__(self, parent)
self.tabwidget = None
self.dockviewer = None
self.wrap_action = None
self.linenumbers_action = None
self.editors = []
self.filenames = []
self.histories = []
# Initialize plugin actions, toolbutton and general signals
self.initialize_plugin()
layout = QVBoxLayout()
self.tabwidget = Tabs(self, self.plugin_actions)
self.tabwidget.currentChanged.connect(self.refresh_plugin)
self.tabwidget.move_data.connect(self.move_tab)
if sys.platform == 'darwin':
tab_container = QWidget()
tab_container.setObjectName('tab-container')
tab_layout = QHBoxLayout(tab_container)
tab_layout.setContentsMargins(0, 0, 0, 0)
tab_layout.addWidget(self.tabwidget)
layout.addWidget(tab_container)
else:
layout.addWidget(self.tabwidget)
# Menu as corner widget
self.tabwidget.setCornerWidget(self.options_button)
# Find/replace widget
self.find_widget = FindReplace(self)
self.find_widget.hide()
self.register_widget_shortcuts(self.find_widget)
layout.addWidget(self.find_widget)
self.setLayout(layout)
#------ SpyderPluginWidget API ---------------------------------------------
def get_plugin_title(self):
"""Return widget title"""
return _('History log')
def get_plugin_icon(self):
"""Return widget icon"""
return ima.icon('history')
def get_focus_widget(self):
"""
Return the widget to give focus to when
this plugin's dockwidget is raised on top-level
"""
return self.tabwidget.currentWidget()
def closing_plugin(self, cancelable=False):
"""Perform actions before parent main window is closed"""
return True
def refresh_plugin(self):
"""Refresh tabwidget"""
if self.tabwidget.count():
editor = self.tabwidget.currentWidget()
else:
editor = None
self.find_widget.set_editor(editor)
def get_plugin_actions(self):
"""Return a list of actions related to plugin"""
self.history_action = create_action(self, _("History..."),
None, ima.icon('history'),
_("Set history maximum entries"),
triggered=self.change_history_depth)
self.wrap_action = create_action(self, _("Wrap lines"),
toggled=self.toggle_wrap_mode)
self.wrap_action.setChecked( self.get_option('wrap') )
self.linenumbers_action = create_action(
self, _("Show line numbers"), toggled=self.toggle_line_numbers)
self.linenumbers_action.setChecked(self.get_option('line_numbers'))
menu_actions = [self.history_action, self.wrap_action,
self.linenumbers_action]
return menu_actions
def on_first_registration(self):
"""Action to be performed on first plugin registration"""
self.main.tabify_plugins(self.main.ipyconsole, self)
def register_plugin(self):
"""Register plugin in Spyder's main window"""
self.focus_changed.connect(self.main.plugin_focus_changed)
self.main.add_dockwidget(self)
self.main.console.shell.refresh.connect(self.refresh_plugin)
def update_font(self):
"""Update font from Preferences"""
color_scheme = self.get_color_scheme()
font = self.get_plugin_font()
for editor in self.editors:
editor.set_font(font, color_scheme)
def apply_plugin_settings(self, options):
"""Apply configuration file's plugin settings"""
color_scheme_n = 'color_scheme_name'
color_scheme_o = self.get_color_scheme()
font_n = 'plugin_font'
font_o = self.get_plugin_font()
wrap_n = 'wrap'
wrap_o = self.get_option(wrap_n)
self.wrap_action.setChecked(wrap_o)
linenb_n = 'line_numbers'
linenb_o = self.get_option(linenb_n)
for editor in self.editors:
if font_n in options:
scs = color_scheme_o if color_scheme_n in options else None
editor.set_font(font_o, scs)
elif color_scheme_n in options:
editor.set_color_scheme(color_scheme_o)
if wrap_n in options:
editor.toggle_wrap_mode(wrap_o)
if linenb_n in options:
editor.toggle_line_numbers(linenumbers=linenb_o, markers=False)
#------ Private API --------------------------------------------------------
def move_tab(self, index_from, index_to):
"""
Move tab (tabs themselves have already been moved by the tabwidget)
"""
filename = self.filenames.pop(index_from)
editor = self.editors.pop(index_from)
self.filenames.insert(index_to, filename)
self.editors.insert(index_to, editor)
#------ Public API ---------------------------------------------------------
def add_history(self, filename, history_list):
"""
Add new history tab
Slot for add_history signal emitted by shell instance
"""
if filename in self.filenames:
return
#Limit history depth
history_list = history_list[-self.get_option('max_entries'):]
editor = codeeditor.CodeEditor(self)
editor.setup_editor(linenumbers=self.get_option('line_numbers'),
language='py',
scrollflagarea=False)
editor.focus_changed.connect(lambda: self.focus_changed.emit())
editor.setReadOnly(True)
color_scheme = self.get_color_scheme()
editor.set_font( self.get_plugin_font(), color_scheme )
editor.toggle_wrap_mode( self.get_option('wrap') )
editor.set_text('\n'.join(history_list) + '\n')
editor.set_cursor_position('eof')
self.histories.append(history_list)
self.editors.append(editor)
self.filenames.append(filename)
index = self.tabwidget.addTab(editor, osp.basename(filename))
self.find_widget.set_editor(editor)
self.tabwidget.setTabToolTip(index, filename)
self.tabwidget.setCurrentIndex(index)
@Slot(str, str)
def append_to_history(self, filename, command):
"""
Append an entry to history filename
Slot for append_to_history signal emitted by shell instance
"""
if not is_text_string(filename): # filename is a QString
filename = to_text_string(filename.toUtf8(), 'utf-8')
command = to_text_string(command)
index = self.filenames.index(filename)
self.histories[index].append(command)
self.editors[index].append(command)
if self.get_option('go_to_eof'):
self.editors[index].set_cursor_position('eof')
self.tabwidget.setCurrentIndex(index)
def set_history(self, filename, history_list):
"""
Update the history for history filename
Slot for set_history signal emitted by shell instance
"""
#Limit history depth
history_list = history_list[-self.get_option('max_entries'):]
if not is_text_string(filename): # filename is a QString
filename = to_text_string(filename.toUtf8(), 'utf-8')
index = self.filenames.index(filename)
self.histories[index] = history_list
self.editors[index].set_text('\n'.join(history_list) + '\n')
if self.get_option('go_to_eof'):
self.editors[index].set_cursor_position('eof')
self.tabwidget.setCurrentIndex(index)
@Slot()
def change_history_depth(self):
"Change history max entries"""
depth, valid = QInputDialog.getInt(self, _('History'),
_('Maximum entries'),
self.get_option('max_entries'),
10, 10000)
if valid:
self.set_option('max_entries', depth)
@Slot(bool)
def toggle_wrap_mode(self, checked):
"""Toggle wrap mode"""
if self.tabwidget is None:
return
for editor in self.editors:
editor.toggle_wrap_mode(checked)
self.set_option('wrap', checked)
@Slot(bool)
def toggle_line_numbers(self, checked):
"""Toggle line numbers."""
if self.tabwidget is None:
return
for editor in self.editors:
editor.toggle_line_numbers(linenumbers=checked, markers=False)
self.set_option('line_numbers', checked)