2014-06-19 09:04:37 +02:00
|
|
|
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
|
|
|
|
|
2016-01-04 07:12:39 +01:00
|
|
|
# Copyright 2014-2016 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
|
2014-04-17 11:08:14 +02:00
|
|
|
#
|
|
|
|
# This file is part of qutebrowser.
|
|
|
|
#
|
|
|
|
# qutebrowser is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# qutebrowser is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""Command history for the status bar."""
|
|
|
|
|
2015-01-31 22:56:23 +01:00
|
|
|
from PyQt5.QtCore import pyqtSlot, pyqtSignal, QObject
|
2014-04-17 11:08:14 +02:00
|
|
|
|
2014-12-10 13:19:42 +01:00
|
|
|
from qutebrowser.config import config
|
2014-08-26 20:15:41 +02:00
|
|
|
from qutebrowser.utils import usertypes, log
|
2014-04-17 11:08:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
class HistoryEmptyError(Exception):
|
|
|
|
|
|
|
|
"""Raised when the history is empty."""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class HistoryEndReachedError(Exception):
|
|
|
|
|
|
|
|
"""Raised when the end of the history is reached."""
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-01-31 22:56:23 +01:00
|
|
|
class History(QObject):
|
2014-04-17 11:08:14 +02:00
|
|
|
|
|
|
|
"""Command history.
|
|
|
|
|
|
|
|
Attributes:
|
2014-12-10 13:19:42 +01:00
|
|
|
handle_private_mode: Whether to ignore history in private mode.
|
2014-08-13 06:53:02 +02:00
|
|
|
history: A list of executed commands, with newer commands at the end.
|
2014-04-17 11:08:14 +02:00
|
|
|
_tmphist: Temporary history for history browsing (as NeighborList)
|
2015-01-31 22:56:23 +01:00
|
|
|
|
|
|
|
Signals:
|
|
|
|
changed: Emitted when an entry was added to the history.
|
2014-04-17 11:08:14 +02:00
|
|
|
"""
|
|
|
|
|
2015-01-31 22:56:23 +01:00
|
|
|
changed = pyqtSignal()
|
|
|
|
|
|
|
|
def __init__(self, history=None, parent=None):
|
2014-08-07 14:39:42 +02:00
|
|
|
"""Constructor.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
history: The initial history to set.
|
|
|
|
"""
|
2015-01-31 22:56:23 +01:00
|
|
|
super().__init__(parent)
|
2014-12-10 13:19:42 +01:00
|
|
|
self.handle_private_mode = False
|
2014-04-17 11:08:14 +02:00
|
|
|
self._tmphist = None
|
2014-05-05 17:56:14 +02:00
|
|
|
if history is None:
|
2014-08-13 06:53:02 +02:00
|
|
|
self.history = []
|
2014-04-17 11:08:14 +02:00
|
|
|
else:
|
2014-08-13 06:53:02 +02:00
|
|
|
self.history = history
|
2014-04-17 11:08:14 +02:00
|
|
|
|
2014-06-25 22:22:30 +02:00
|
|
|
def __getitem__(self, idx):
|
2014-08-13 06:53:02 +02:00
|
|
|
return self.history[idx]
|
2014-06-25 22:22:30 +02:00
|
|
|
|
2014-09-02 21:54:07 +02:00
|
|
|
def is_browsing(self):
|
2014-04-17 11:08:14 +02:00
|
|
|
"""Check _tmphist to see if we're browsing."""
|
|
|
|
return self._tmphist is not None
|
|
|
|
|
|
|
|
def start(self, text):
|
|
|
|
"""Start browsing to the history.
|
|
|
|
|
|
|
|
Called when the user presses the up/down key and wasn't browsing the
|
|
|
|
history already.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
text: The preset text.
|
|
|
|
"""
|
2014-08-26 20:15:41 +02:00
|
|
|
log.misc.debug("Preset text: '{}'".format(text))
|
2014-04-17 11:08:14 +02:00
|
|
|
if text:
|
2014-08-13 06:53:02 +02:00
|
|
|
items = [e for e in self.history if e.startswith(text)]
|
2014-04-17 11:08:14 +02:00
|
|
|
else:
|
2014-08-13 06:53:02 +02:00
|
|
|
items = self.history
|
2014-04-17 11:08:14 +02:00
|
|
|
if not items:
|
|
|
|
raise HistoryEmptyError
|
2014-08-26 19:10:14 +02:00
|
|
|
self._tmphist = usertypes.NeighborList(items)
|
2014-04-17 11:08:14 +02:00
|
|
|
return self._tmphist.lastitem()
|
|
|
|
|
|
|
|
@pyqtSlot()
|
|
|
|
def stop(self):
|
|
|
|
"""Stop browsing the history."""
|
|
|
|
self._tmphist = None
|
|
|
|
|
|
|
|
def previtem(self):
|
2014-04-17 17:44:27 +02:00
|
|
|
"""Get the previous item in the temp history.
|
|
|
|
|
|
|
|
start() needs to be called before calling this.
|
|
|
|
"""
|
2014-09-02 21:54:07 +02:00
|
|
|
if not self.is_browsing():
|
2014-04-17 11:08:14 +02:00
|
|
|
raise ValueError("Currently not browsing history")
|
|
|
|
try:
|
|
|
|
return self._tmphist.previtem()
|
|
|
|
except IndexError:
|
|
|
|
raise HistoryEndReachedError
|
|
|
|
|
|
|
|
def nextitem(self):
|
2014-04-17 17:44:27 +02:00
|
|
|
"""Get the next item in the temp history.
|
|
|
|
|
|
|
|
start() needs to be called before calling this.
|
|
|
|
"""
|
2014-09-02 21:54:07 +02:00
|
|
|
if not self.is_browsing():
|
2014-04-17 11:08:14 +02:00
|
|
|
raise ValueError("Currently not browsing history")
|
|
|
|
try:
|
|
|
|
return self._tmphist.nextitem()
|
|
|
|
except IndexError:
|
|
|
|
raise HistoryEndReachedError
|
|
|
|
|
|
|
|
def append(self, text):
|
|
|
|
"""Append a new item to the history.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
text: The text to append.
|
|
|
|
"""
|
2014-12-10 13:19:42 +01:00
|
|
|
if (self.handle_private_mode and
|
|
|
|
config.get('general', 'private-browsing')):
|
|
|
|
return
|
2014-08-13 06:53:02 +02:00
|
|
|
if not self.history or text != self.history[-1]:
|
|
|
|
self.history.append(text)
|
2015-01-31 22:56:23 +01:00
|
|
|
self.changed.emit()
|