From c092840c0470ee6ac84e1b4e8a57691a145b72dd Mon Sep 17 00:00:00 2001 From: Daniel Fiser Date: Sun, 5 Feb 2017 15:58:35 +0100 Subject: [PATCH 1/6] Add optional argument --only-active-window to :session-save. The new optional argument --only-active-window makes :session-save to save only the tabs in the currently active window. --- doc/help/commands.asciidoc | 3 ++- qutebrowser/misc/sessions.py | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/doc/help/commands.asciidoc b/doc/help/commands.asciidoc index d6db9f350..74b802dfb 100644 --- a/doc/help/commands.asciidoc +++ b/doc/help/commands.asciidoc @@ -723,7 +723,7 @@ Load a session. [[session-save]] === session-save -Syntax: +:session-save [*--current*] [*--quiet*] [*--force*] ['name']+ +Syntax: +:session-save [*--current*] [*--quiet*] [*--force*] [*--only-active-window*] ['name']+ Save a session. @@ -735,6 +735,7 @@ Save a session. * +*-c*+, +*--current*+: Save the current session instead of the default. * +*-q*+, +*--quiet*+: Don't show confirmation message. * +*-f*+, +*--force*+: Force saving internal sessions (starting with an underline). +* +*-o*+, +*--only-active-window*+: Saves only tabs of the currently active window. [[set]] === set diff --git a/qutebrowser/misc/sessions.py b/qutebrowser/misc/sessions.py index 3b18c39a5..437275b4c 100644 --- a/qutebrowser/misc/sessions.py +++ b/qutebrowser/misc/sessions.py @@ -214,10 +214,18 @@ class SessionManager(QObject): data['history'].append(item_data) return data - def _save_all(self): + def _save_all(self, only_active_window = False): """Get a dict with data for all windows/tabs.""" data = {'windows': []} - for win_id in objreg.window_registry: + winlist = objreg.window_registry + if only_active_window: + active_window = QApplication.instance().activeWindow() + winlist = [getattr(active_window, 'win_id', None)] + if winlist[0] is None: + # Fall back to all windows + winlist = objreg.window_registry + + for win_id in winlist: tabbed_browser = objreg.get('tabbed-browser', scope='window', window=win_id) main_window = objreg.get('main-window', scope='window', @@ -255,7 +263,8 @@ class SessionManager(QObject): name = 'default' return name - def save(self, name, last_window=False, load_next_time=False): + def save(self, name, last_window=False, load_next_time=False, + only_active_window=False): """Save a named session. Args: @@ -278,7 +287,7 @@ class SessionManager(QObject): log.sessions.error("last_window_session is None while saving!") return else: - data = self._save_all() + data = self._save_all(only_active_window = only_active_window) log.sessions.vdebug("Saving data: {}".format(data)) try: with qtutils.savefile_open(path) as f: @@ -418,7 +427,7 @@ class SessionManager(QObject): @cmdutils.register(name=['session-save', 'w'], instance='session-manager') @cmdutils.argument('name', completion=usertypes.Completion.sessions) def session_save(self, name: str=default, current=False, quiet=False, - force=False): + force=False, only_active_window=False): """Save a session. Args: @@ -439,7 +448,7 @@ class SessionManager(QObject): name = self._current assert not name.startswith('_') try: - name = self.save(name) + name = self.save(name, only_active_window = only_active_window) except SessionError as e: raise cmdexc.CommandError("Error while saving session: {}" .format(e)) From bb74b2703fb4d0227f4f2f390c3fcd4510115372 Mon Sep 17 00:00:00 2001 From: Daniel Fiser Date: Wed, 22 Feb 2017 09:59:29 +0100 Subject: [PATCH 2/6] session-save: doc generated from docstring. --- README.asciidoc | 1 + doc/help/commands.asciidoc | 3 ++- qutebrowser/misc/sessions.py | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.asciidoc b/README.asciidoc index a33738589..0123e6271 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -275,6 +275,7 @@ Contributors, sorted by the number of commits in descending order: * Dietrich Daroch * Derek Sivers * Daniel Lu +* Daniel Fiser * Arseniy Seroka * Andy Balaam * Andreas Fischer diff --git a/doc/help/commands.asciidoc b/doc/help/commands.asciidoc index 74b802dfb..86485b7de 100644 --- a/doc/help/commands.asciidoc +++ b/doc/help/commands.asciidoc @@ -723,7 +723,8 @@ Load a session. [[session-save]] === session-save -Syntax: +:session-save [*--current*] [*--quiet*] [*--force*] [*--only-active-window*] ['name']+ +Syntax: +:session-save [*--current*] [*--quiet*] [*--force*] [*--only-active-window*] + ['name']+ Save a session. diff --git a/qutebrowser/misc/sessions.py b/qutebrowser/misc/sessions.py index 437275b4c..d3bb2b3ce 100644 --- a/qutebrowser/misc/sessions.py +++ b/qutebrowser/misc/sessions.py @@ -436,6 +436,7 @@ class SessionManager(QObject): current: Save the current session instead of the default. quiet: Don't show confirmation message. force: Force saving internal sessions (starting with an underline). + only_active_window: Saves only tabs of the currently active window. """ if (name is not default and name.startswith('_') and # pylint: disable=no-member From bb5825e0431b6b1cacb2d513366a39af4c0fd006 Mon Sep 17 00:00:00 2001 From: Daniel Fiser Date: Wed, 22 Feb 2017 10:22:30 +0100 Subject: [PATCH 3/6] session-save: in case of --only-active-window, the win-id is determined in session_save(). --- README.asciidoc | 2 +- qutebrowser/misc/sessions.py | 26 ++++++++++++++------------ 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/README.asciidoc b/README.asciidoc index 0123e6271..d51759273 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -239,6 +239,7 @@ Contributors, sorted by the number of commits in descending order: * Jean-Louis Fuchs * Franz Fellner * Eric Drechsel +* Daniel Fiser * zwarag * xd1le * rmortens @@ -275,7 +276,6 @@ Contributors, sorted by the number of commits in descending order: * Dietrich Daroch * Derek Sivers * Daniel Lu -* Daniel Fiser * Arseniy Seroka * Andy Balaam * Andreas Fischer diff --git a/qutebrowser/misc/sessions.py b/qutebrowser/misc/sessions.py index d3bb2b3ce..a61df90e2 100644 --- a/qutebrowser/misc/sessions.py +++ b/qutebrowser/misc/sessions.py @@ -214,16 +214,13 @@ class SessionManager(QObject): data['history'].append(item_data) return data - def _save_all(self, only_active_window = False): + def _save_all(self, *, only_window=None): """Get a dict with data for all windows/tabs.""" data = {'windows': []} - winlist = objreg.window_registry - if only_active_window: - active_window = QApplication.instance().activeWindow() - winlist = [getattr(active_window, 'win_id', None)] - if winlist[0] is None: - # Fall back to all windows - winlist = objreg.window_registry + if only_window is not None: + winlist = [only_window] + else: + winlist = objreg.window_registry for win_id in winlist: tabbed_browser = objreg.get('tabbed-browser', scope='window', @@ -264,7 +261,7 @@ class SessionManager(QObject): return name def save(self, name, last_window=False, load_next_time=False, - only_active_window=False): + only_window=None): """Save a named session. Args: @@ -273,6 +270,7 @@ class SessionManager(QObject): last_window: If set, saves the saved self._last_window_session instead of the currently open state. load_next_time: If set, prepares this session to be load next time. + only_window: If set, only tabs in the specified window is saved. Return: The name of the saved session. @@ -287,7 +285,7 @@ class SessionManager(QObject): log.sessions.error("last_window_session is None while saving!") return else: - data = self._save_all(only_active_window = only_active_window) + data = self._save_all(only_window=only_window) log.sessions.vdebug("Saving data: {}".format(data)) try: with qtutils.savefile_open(path) as f: @@ -426,8 +424,9 @@ class SessionManager(QObject): @cmdutils.register(name=['session-save', 'w'], instance='session-manager') @cmdutils.argument('name', completion=usertypes.Completion.sessions) + @cmdutils.argument('win_id', win_id=True) def session_save(self, name: str=default, current=False, quiet=False, - force=False, only_active_window=False): + force=False, only_active_window=False, win_id=None): """Save a session. Args: @@ -449,7 +448,10 @@ class SessionManager(QObject): name = self._current assert not name.startswith('_') try: - name = self.save(name, only_active_window = only_active_window) + if only_active_window: + name = self.save(name, only_window=win_id) + else: + name = self.save(name) except SessionError as e: raise cmdexc.CommandError("Error while saving session: {}" .format(e)) From 6e1ac8be78b57da26975823f8c449f8f05f3c816 Mon Sep 17 00:00:00 2001 From: Daniel Fiser Date: Wed, 22 Feb 2017 10:37:55 +0100 Subject: [PATCH 4/6] Fixed indentation of SessionManager.save() arguments. --- qutebrowser/misc/sessions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qutebrowser/misc/sessions.py b/qutebrowser/misc/sessions.py index a61df90e2..ea62ee999 100644 --- a/qutebrowser/misc/sessions.py +++ b/qutebrowser/misc/sessions.py @@ -261,7 +261,7 @@ class SessionManager(QObject): return name def save(self, name, last_window=False, load_next_time=False, - only_window=None): + only_window=None): """Save a named session. Args: From 76bb11c6aa3b22de1a4c9730eaf5e49a22d7a816 Mon Sep 17 00:00:00 2001 From: Daniel Fiser Date: Wed, 22 Feb 2017 14:16:46 +0100 Subject: [PATCH 5/6] Added test for :session-save --only-active-window --- tests/end2end/features/sessions.feature | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/end2end/features/sessions.feature b/tests/end2end/features/sessions.feature index abb399453..45f413ce5 100644 --- a/tests/end2end/features/sessions.feature +++ b/tests/end2end/features/sessions.feature @@ -278,6 +278,32 @@ Feature: Saving and loading sessions Then "Saved session quiet_session." should not be logged And the session quiet_session should exist + Scenario: Saving session with --only-active-window + When I open data/numbers/1.txt + And I open data/numbers/2.txt in a new tab + And I open data/numbers/3.txt in a new window + And I open data/numbers/4.txt in a new tab + And I open data/numbers/5.txt in a new tab + And I run :session-save --only-active-window window_session_name + And I run :window-only + And I run :tab-only + And I run :session-load window_session_name + Then the session should look like: + windows: + - active: true + tabs: + - history: + - active: true + url: http://localhost:*/data/numbers/5.txt + - tabs: + - history: + - url: http://localhost:*/data/numbers/3.txt + - history: + - url: http://localhost:*/data/numbers/4.txt + - history: + - active: true + url: http://localhost:*/data/numbers/5.txt + # :session-delete Scenario: Deleting a directory From 81a24bdbef832dd40f8e0c5d0bc2b485f9ac0bc2 Mon Sep 17 00:00:00 2001 From: Daniel Fiser Date: Wed, 22 Feb 2017 17:04:57 +0100 Subject: [PATCH 6/6] Fixed test of :session-save --only-active-window --- tests/end2end/features/sessions.feature | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/end2end/features/sessions.feature b/tests/end2end/features/sessions.feature index 45f413ce5..bba8023c9 100644 --- a/tests/end2end/features/sessions.feature +++ b/tests/end2end/features/sessions.feature @@ -290,8 +290,7 @@ Feature: Saving and loading sessions And I run :session-load window_session_name Then the session should look like: windows: - - active: true - tabs: + - tabs: - history: - active: true url: http://localhost:*/data/numbers/5.txt