##// END OF EJS Templates
Moved HistoryConsoleWidget to its own file to make console_widget.py a little smaller.
epatters -
Show More
@@ -0,0 +1,108 b''
1 # System library imports
2 from PyQt4 import QtGui
3
4 # Local imports
5 from console_widget import ConsoleWidget
6
7
8 class HistoryConsoleWidget(ConsoleWidget):
9 """ A ConsoleWidget that keeps a history of the commands that have been
10 executed.
11 """
12
13 #---------------------------------------------------------------------------
14 # 'object' interface
15 #---------------------------------------------------------------------------
16
17 def __init__(self, *args, **kw):
18 super(HistoryConsoleWidget, self).__init__(*args, **kw)
19 self._history = []
20 self._history_index = 0
21
22 #---------------------------------------------------------------------------
23 # 'ConsoleWidget' public interface
24 #---------------------------------------------------------------------------
25
26 def execute(self, source=None, hidden=False, interactive=False):
27 """ Reimplemented to the store history.
28 """
29 if not hidden:
30 history = self.input_buffer if source is None else source
31
32 executed = super(HistoryConsoleWidget, self).execute(
33 source, hidden, interactive)
34
35 if executed and not hidden:
36 # Save the command unless it was an empty string or was identical
37 # to the previous command.
38 history = history.rstrip()
39 if history and (not self._history or self._history[-1] != history):
40 self._history.append(history)
41
42 # Move the history index to the most recent item.
43 self._history_index = len(self._history)
44
45 return executed
46
47 #---------------------------------------------------------------------------
48 # 'ConsoleWidget' abstract interface
49 #---------------------------------------------------------------------------
50
51 def _up_pressed(self):
52 """ Called when the up key is pressed. Returns whether to continue
53 processing the event.
54 """
55 prompt_cursor = self._get_prompt_cursor()
56 if self._get_cursor().blockNumber() == prompt_cursor.blockNumber():
57 self.history_previous()
58
59 # Go to the first line of prompt for seemless history scrolling.
60 cursor = self._get_prompt_cursor()
61 cursor.movePosition(QtGui.QTextCursor.EndOfLine)
62 self._set_cursor(cursor)
63
64 return False
65 return True
66
67 def _down_pressed(self):
68 """ Called when the down key is pressed. Returns whether to continue
69 processing the event.
70 """
71 end_cursor = self._get_end_cursor()
72 if self._get_cursor().blockNumber() == end_cursor.blockNumber():
73 self.history_next()
74 return False
75 return True
76
77 #---------------------------------------------------------------------------
78 # 'HistoryConsoleWidget' public interface
79 #---------------------------------------------------------------------------
80
81 def history_previous(self):
82 """ If possible, set the input buffer to the previous item in the
83 history.
84 """
85 if self._history_index > 0:
86 self._history_index -= 1
87 self.input_buffer = self._history[self._history_index]
88
89 def history_next(self):
90 """ Set the input buffer to the next item in the history, or a blank
91 line if there is no subsequent item.
92 """
93 if self._history_index < len(self._history):
94 self._history_index += 1
95 if self._history_index < len(self._history):
96 self.input_buffer = self._history[self._history_index]
97 else:
98 self.input_buffer = ''
99
100 #---------------------------------------------------------------------------
101 # 'HistoryConsoleWidget' protected interface
102 #---------------------------------------------------------------------------
103
104 def _set_history(self, history):
105 """ Replace the current history with a sequence of history items.
106 """
107 self._history = list(history)
108 self._history_index = len(self._history)
@@ -1454,106 +1454,3 b' class ConsoleWidget(Configurable, QtGui.QWidget):'
1454 else:
1454 else:
1455 self._clear_temporary_buffer()
1455 self._clear_temporary_buffer()
1456 self._text_completing_pos = 0
1456 self._text_completing_pos = 0
1457
1458
1459 class HistoryConsoleWidget(ConsoleWidget):
1460 """ A ConsoleWidget that keeps a history of the commands that have been
1461 executed.
1462 """
1463
1464 #---------------------------------------------------------------------------
1465 # 'object' interface
1466 #---------------------------------------------------------------------------
1467
1468 def __init__(self, *args, **kw):
1469 super(HistoryConsoleWidget, self).__init__(*args, **kw)
1470 self._history = []
1471 self._history_index = 0
1472
1473 #---------------------------------------------------------------------------
1474 # 'ConsoleWidget' public interface
1475 #---------------------------------------------------------------------------
1476
1477 def execute(self, source=None, hidden=False, interactive=False):
1478 """ Reimplemented to the store history.
1479 """
1480 if not hidden:
1481 history = self.input_buffer if source is None else source
1482
1483 executed = super(HistoryConsoleWidget, self).execute(
1484 source, hidden, interactive)
1485
1486 if executed and not hidden:
1487 # Save the command unless it was an empty string or was identical
1488 # to the previous command.
1489 history = history.rstrip()
1490 if history and (not self._history or self._history[-1] != history):
1491 self._history.append(history)
1492
1493 # Move the history index to the most recent item.
1494 self._history_index = len(self._history)
1495
1496 return executed
1497
1498 #---------------------------------------------------------------------------
1499 # 'ConsoleWidget' abstract interface
1500 #---------------------------------------------------------------------------
1501
1502 def _up_pressed(self):
1503 """ Called when the up key is pressed. Returns whether to continue
1504 processing the event.
1505 """
1506 prompt_cursor = self._get_prompt_cursor()
1507 if self._get_cursor().blockNumber() == prompt_cursor.blockNumber():
1508 self.history_previous()
1509
1510 # Go to the first line of prompt for seemless history scrolling.
1511 cursor = self._get_prompt_cursor()
1512 cursor.movePosition(QtGui.QTextCursor.EndOfLine)
1513 self._set_cursor(cursor)
1514
1515 return False
1516 return True
1517
1518 def _down_pressed(self):
1519 """ Called when the down key is pressed. Returns whether to continue
1520 processing the event.
1521 """
1522 end_cursor = self._get_end_cursor()
1523 if self._get_cursor().blockNumber() == end_cursor.blockNumber():
1524 self.history_next()
1525 return False
1526 return True
1527
1528 #---------------------------------------------------------------------------
1529 # 'HistoryConsoleWidget' public interface
1530 #---------------------------------------------------------------------------
1531
1532 def history_previous(self):
1533 """ If possible, set the input buffer to the previous item in the
1534 history.
1535 """
1536 if self._history_index > 0:
1537 self._history_index -= 1
1538 self.input_buffer = self._history[self._history_index]
1539
1540 def history_next(self):
1541 """ Set the input buffer to the next item in the history, or a blank
1542 line if there is no subsequent item.
1543 """
1544 if self._history_index < len(self._history):
1545 self._history_index += 1
1546 if self._history_index < len(self._history):
1547 self.input_buffer = self._history[self._history_index]
1548 else:
1549 self.input_buffer = ''
1550
1551 #---------------------------------------------------------------------------
1552 # 'HistoryConsoleWidget' protected interface
1553 #---------------------------------------------------------------------------
1554
1555 def _set_history(self, history):
1556 """ Replace the current history with a sequence of history items.
1557 """
1558 self._history = list(history)
1559 self._history_index = len(self._history)
@@ -15,7 +15,7 b' from IPython.utils.traitlets import Bool'
15 from bracket_matcher import BracketMatcher
15 from bracket_matcher import BracketMatcher
16 from call_tip_widget import CallTipWidget
16 from call_tip_widget import CallTipWidget
17 from completion_lexer import CompletionLexer
17 from completion_lexer import CompletionLexer
18 from console_widget import HistoryConsoleWidget
18 from history_console_widget import HistoryConsoleWidget
19 from pygments_highlighter import PygmentsHighlighter
19 from pygments_highlighter import PygmentsHighlighter
20
20
21
21
General Comments 0
You need to be logged in to leave comments. Login now