From 8023b1456d4ba57c4e4addeec5dc092401093faf Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 12 Mar 2015 15:25:39 +0100 Subject: [PATCH] Make it possible to configure the timestamp format. --- doc/help/settings.asciidoc | 7 +++++ qutebrowser/completion/models/urlmodel.py | 32 ++++++++++++++++++----- qutebrowser/config/configdata.py | 4 +++ 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/doc/help/settings.asciidoc b/doc/help/settings.asciidoc index a3bcac21e..f524cd7c4 100644 --- a/doc/help/settings.asciidoc +++ b/doc/help/settings.asciidoc @@ -60,6 +60,7 @@ |============== |Setting|Description |<>|What to display in the download filename input. +|<>|How to format timestamps (e.g. for history) |<>|Whether to show the autocompletion window. |<>|The height of the completion, in px or as percentage of the window. |<>|How many commands to save in the history. @@ -615,6 +616,12 @@ Valid values: Default: +pass:[path]+ +[[completion-timestamp-format]] +=== timestamp-format +How to format timestamps (e.g. for history) + +Default: +pass:[%Y-%m-%d]+ + [[completion-show]] === show Whether to show the autocompletion window. diff --git a/qutebrowser/completion/models/urlmodel.py b/qutebrowser/completion/models/urlmodel.py index 64940f3a2..e7a43e94c 100644 --- a/qutebrowser/completion/models/urlmodel.py +++ b/qutebrowser/completion/models/urlmodel.py @@ -19,11 +19,14 @@ """CompletionModels for URLs.""" +import datetime + from PyQt5.QtCore import pyqtSlot from PyQt5.QtGui import QStandardItem from qutebrowser.utils import objreg from qutebrowser.completion.models import base +from qutebrowser.config import config class UrlCompletionModel(base.BaseCompletionModel): @@ -48,10 +51,27 @@ class UrlCompletionModel(base.BaseCompletionModel): for entry in self._history: atime = int(entry.atime) - self.new_item(self._history_cat, entry.url, "", str(atime), - sort=atime) + self.new_item(self._history_cat, entry.url, "", + self._fmt_atime(atime), sort=atime) self._history.item_added.connect(self.on_history_item_added) + objreg.get('config').changed.connect(self.reformat_timestamps) + + def _fmt_atime(self, atime): + """Format an atime to a human-readable string.""" + fmt = config.get('completion', 'timestamp-format') + if fmt is None: + return '' + return datetime.datetime.fromtimestamp(atime).strftime(fmt) + + @config.change_filter('completion', 'timestamp-format') + def reformat_timestamps(self): + """Reformat the timestamps if the config option was changed.""" + for i in range(self._history_cat.rowCount()): + name_item = self._history_cat.child(i, 0) + atime_item = self._history_cat.child(i, 2) + atime = int(name_item.data(base.Role.sort)) + atime_item.setText(self._fmt_atime(atime)) @pyqtSlot(object) def on_history_item_added(self, item): @@ -64,10 +84,10 @@ class UrlCompletionModel(base.BaseCompletionModel): if not name: continue if name.text() == item.url: - self._history_cat.setChild(i, 2, - QStandardItem(str(atime))) + self._history_cat.setChild( + i, 2, QStandardItem(self._fmt_atime(atime))) name.setData(str(atime), base.Role.sort) break else: - self.new_item(self._history_cat, item.url, "", str(atime), - sort=atime) + self.new_item(self._history_cat, item.url, "", + self._fmt_atime(atime), sort=atime) diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index e3ed9f647..e237121c8 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -309,6 +309,10 @@ DATA = collections.OrderedDict([ SettingValue(typ.DownloadPathSuggestion(), 'path'), "What to display in the download filename input."), + ('timestamp-format', + SettingValue(typ.String(none_ok=True), '%Y-%m-%d'), + "How to format timestamps (e.g. for history)"), + ('show', SettingValue(typ.Bool(), 'true'), "Whether to show the autocompletion window."),