def _get_selection_override(self, left, right, opposite): """Helper function for tab_close to get the tab to select. Args: left: Force selecting the tab to the left of the current tab. right: Force selecting the tab to the right of the current tab. opposite: Force selecting the tab in the oppsite direction of what's configured in 'tabs->select-on-remove'. Return: QTabBar.SelectLeftTab, QTabBar.SelectRightTab, or None if no change should be made. """ cmdutils.check_exclusive((left, right, opposite), 'lro') if left: return QTabBar.SelectLeftTab elif right: return QTabBar.SelectRightTab elif opposite: conf_selection = config.get('tabs', 'select-on-remove') if conf_selection == QTabBar.SelectLeftTab: return QTabBar.SelectRightTab elif conf_selection == QTabBar.SelectRightTab: return QTabBar.SelectLeftTab elif conf_selection == QTabBar.SelectPreviousTab: raise cmdexc.CommandError( "-o is not supported with 'tabs->select-on-remove' set to " "'previous'!") return None
def _open(self, url, tab, background, window): """Helper function to open a page. Args: url: The URL to open as QUrl. tab: Whether to open in a new tab. background: Whether to open in the background. window: Whether to open in a new window """ if not url.isValid(): errstr = "Invalid URL {}" if url.errorString(): errstr += " - {}".format(url.errorString()) raise cmdexc.CommandError(errstr) tabbed_browser = self._tabbed_browser() cmdutils.check_exclusive((tab, background, window), 'tbw') if window: tabbed_browser = self._tabbed_browser(window=True) tabbed_browser.tabopen(url) elif tab: tabbed_browser.tabopen(url, background=False, explicit=True) elif background: tabbed_browser.tabopen(url, background=True, explicit=True) else: widget = self._current_widget() widget.openurl(url)
def navigate( self, where: {'type': ('prev', 'next', 'up', 'increment', 'decrement')}, tab=False, bg=False, window=False): """Open typical prev/next links or navigate using the URL path. This tries to automatically click on typical _Previous Page_ or _Next Page_ links using some heuristics. Alternatively it can navigate by changing the current URL. Args: where: What to open. - `prev`: Open a _previous_ link. - `next`: Open a _next_ link. - `up`: Go up a level in the current URL. - `increment`: Increment the last number in the URL. - `decrement`: Decrement the last number in the URL. tab: Open in a new tab. bg: Open in a background tab. window: Open in a new window. """ cmdutils.check_exclusive((tab, bg, window), 'tbw') widget = self._current_widget() frame = widget.page().currentFrame() url = self._current_url() if frame is None: raise cmdexc.CommandError("No frame focused!") hintmanager = objreg.get('hintmanager', scope='tab') if where == 'prev': hintmanager.follow_prevnext(frame, url, prev=True, tab=tab, background=bg, window=window) elif where == 'next': hintmanager.follow_prevnext(frame, url, prev=False, tab=tab, background=bg, window=window) elif where == 'up': self._navigate_up(url, tab, bg, window) elif where in ('decrement', 'increment'): self._navigate_incdec(url, where, tab, bg, window) else: raise ValueError("Got called with invalid value {} for " "`where'.".format(where))
def tab_only(self, left=False, right=False): """Close all tabs except for the current one. Args: left: Keep tabs to the left of the current. right: Keep tabs to the right of the current. """ cmdutils.check_exclusive((left, right), 'lr') tabbed_browser = self._tabbed_browser() cur_idx = tabbed_browser.currentIndex() assert cur_idx != -1 for i, tab in enumerate(tabbed_browser.widgets()): if (i == cur_idx or (left and i < cur_idx) or (right and i > cur_idx)): continue else: tabbed_browser.close_tab(tab)
def navigate(self, where: {'type': ('prev', 'next', 'up', 'increment', 'decrement')}, tab=False, bg=False, window=False): """Open typical prev/next links or navigate using the URL path. This tries to automatically click on typical _Previous Page_ or _Next Page_ links using some heuristics. Alternatively it can navigate by changing the current URL. Args: where: What to open. - `prev`: Open a _previous_ link. - `next`: Open a _next_ link. - `up`: Go up a level in the current URL. - `increment`: Increment the last number in the URL. - `decrement`: Decrement the last number in the URL. tab: Open in a new tab. bg: Open in a background tab. window: Open in a new window. """ cmdutils.check_exclusive((tab, bg, window), 'tbw') widget = self._current_widget() frame = widget.page().currentFrame() url = self._current_url() if frame is None: raise cmdexc.CommandError("No frame focused!") hintmanager = objreg.get('hintmanager', scope='tab') if where == 'prev': hintmanager.follow_prevnext(frame, url, prev=True, tab=tab, background=bg, window=window) elif where == 'next': hintmanager.follow_prevnext(frame, url, prev=False, tab=tab, background=bg, window=window) elif where == 'up': self._navigate_up(url, tab, bg, window) elif where in ('decrement', 'increment'): self._navigate_incdec(url, where, tab, bg, window) else: raise ValueError("Got called with invalid value {} for " "`where'.".format(where))
def _open(self, url, tab, background, window): """Helper function to open a page. Args: url: The URL to open as QUrl. tab: Whether to open in a new tab. background: Whether to open in the background. window: Whether to open in a new window """ urlutils.raise_cmdexc_if_invalid(url) tabbed_browser = self._tabbed_browser() cmdutils.check_exclusive((tab, background, window), 'tbw') if window: tabbed_browser = self._tabbed_browser(window=True) tabbed_browser.tabopen(url) elif tab: tabbed_browser.tabopen(url, background=False, explicit=True) elif background: tabbed_browser.tabopen(url, background=True, explicit=True) else: widget = self._current_widget() widget.openurl(url)
def test_bad(self): with pytest.raises(cmdexc.CommandError, match="Only one of -x/-y/-z can be given!"): cmdutils.check_exclusive([True, True], 'xyz')
def test_good(self, flags): cmdutils.check_exclusive(flags, [])
def test_bad(self): with pytest.raises(cmdexc.CommandError) as excinfo: cmdutils.check_exclusive([True, True], 'xyz') assert str(excinfo.value) == "Only one of -x/-y/-z can be given!"