From 6ab49fdf1dfe0589dea4ff7d892b2da476f0dab2 Mon Sep 17 00:00:00 2001 From: Iordanis Grigoriou Date: Mon, 10 Jul 2017 15:43:35 +0200 Subject: [PATCH] Move back/forward logic to AbstractHistory, fix method names --- qutebrowser/browser/browsertab.py | 18 +++++++++++++---- qutebrowser/browser/commands.py | 8 ++++---- qutebrowser/browser/webengine/webenginetab.py | 20 ++----------------- qutebrowser/browser/webkit/webkittab.py | 20 ++----------------- 4 files changed, 22 insertions(+), 44 deletions(-) diff --git a/qutebrowser/browser/browsertab.py b/qutebrowser/browser/browsertab.py index ab26878e3..e935589a7 100644 --- a/qutebrowser/browser/browsertab.py +++ b/qutebrowser/browser/browsertab.py @@ -466,10 +466,20 @@ class AbstractHistory: raise NotImplementedError def back(self, count): - raise NotImplementedError + idx = self.current_idx() - count + if idx >= 0: + self._go_to_item(self._item_at(idx)) + else: + self._go_to_item(self._item_at(0)) + raise WebTabError("At beginning of history.") def forward(self, count): - raise NotImplementedError + idx = self.current_idx() + count + if idx < len(self): + self._go_to_item(self._item_at(idx)) + else: + self._go_to_item(self._item_at(len(self) - 1)) + raise WebTabError("At end of history.") def can_go_back(self): raise NotImplementedError @@ -477,10 +487,10 @@ class AbstractHistory: def can_go_forward(self): raise NotImplementedError - def itemAt(self, i): + def _item_at(self, i): raise NotImplementedError - def goToItem(self, item): + def _go_to_item(self, item): raise NotImplementedError def serialize(self): diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index e11b9a61b..682c8e3c7 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -543,13 +543,13 @@ class CommandDispatcher: if forward: try: widget.history.forward(count) - except IndexError: - raise cmdexc.CommandError("At end of history.") + except browsertab.WebTabError as e: + raise cmdexc.CommandError(e) else: try: widget.history.back(count) - except IndexError: - raise cmdexc.CommandError("At beginning of history.") + except browsertab.WebTabError as e: + raise cmdexc.CommandError(e) @cmdutils.register(instance='command-dispatcher', scope='window') @cmdutils.argument('count', count=True) diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index a86b9c6c1..8d328f5e8 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -411,32 +411,16 @@ class WebEngineHistory(browsertab.AbstractHistory): def current_idx(self): return self._history.currentItemIndex() - def back(self, count): - idx = self.current_idx() - count - if idx >= 0: - self.goToItem(self.itemAt(idx)) - else: - self.goToItem(self.itemAt(0)) - raise IndexError - - def forward(self, count): - idx = self.current_idx() + count - if idx < len(self): - self.goToItem(self.itemAt(idx)) - else: - self.goToItem(self.itemAt(len(self) - 1)) - raise IndexError - def can_go_back(self): return self._history.canGoBack() def can_go_forward(self): return self._history.canGoForward() - def itemAt(self, i): + def _item_at(self, i): return self._history.itemAt(i) - def goToItem(self, item): + def _go_to_item(self, item): return self._history.goToItem(item) def serialize(self): diff --git a/qutebrowser/browser/webkit/webkittab.py b/qutebrowser/browser/webkit/webkittab.py index 6306ac542..348d2c628 100644 --- a/qutebrowser/browser/webkit/webkittab.py +++ b/qutebrowser/browser/webkit/webkittab.py @@ -506,32 +506,16 @@ class WebKitHistory(browsertab.AbstractHistory): def current_idx(self): return self._history.currentItemIndex() - def back(self, count): - idx = self.current_idx() - count - if idx >= 0: - self.goToItem(self.itemAt(idx)) - else: - self.goToItem(self.itemAt(0)) - raise IndexError - - def forward(self, count): - idx = self.current_idx() + count - if idx < len(self): - self.goToItem(self.itemAt(idx)) - else: - self.goToItem(self.itemAt(len(self) - 1)) - raise IndexError - def can_go_back(self): return self._history.canGoBack() def can_go_forward(self): return self._history.canGoForward() - def itemAt(self, i): + def _item_at(self, i): return self._history.itemAt(i) - def goToItem(self, item): + def _go_to_item(self, item): return self._history.goToItem(item) def serialize(self):