File: commandhistory.py

package info (click to toggle)
python-pyqtconsole 1.2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 184 kB
  • sloc: python: 1,338; makefile: 3
file content (41 lines) | stat: -rw-r--r-- 1,241 bytes parent folder | download
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
# -*- coding: utf-8 -*-
from qtpy.QtCore import QObject


class CommandHistory(QObject):
    def __init__(self, parent):
        super(CommandHistory, self).__init__(parent)
        self._cmd_history = []
        self._idx = 0
        self._pending_input = ''

    def add(self, str_):
        if str_:
            self._cmd_history.append(str_)

        self._pending_input = ''
        self._idx = len(self._cmd_history)

    def inc(self):
        # index starts at 0 so + 1 to make sure that we are within the
        # limits of the list
        if self._cmd_history:
            self._idx = min(self._idx + 1, len(self._cmd_history))
            self._insert_in_editor(self.current())

    def dec(self, _input):
        if self._idx == len(self._cmd_history):
            self._pending_input = _input
        if len(self._cmd_history) and self._idx > 0:
            self._idx -= 1
            self._insert_in_editor(self.current())

    def current(self):
        if self._idx == len(self._cmd_history):
            return self._pending_input
        else:
            return self._cmd_history[self._idx]

    def _insert_in_editor(self, str_):
        self.parent().clear_input_buffer()
        self.parent().insert_input_text(str_)