diff --git a/nw/common.py b/nw/common.py index 0019c38a..8af90084 100644 --- a/nw/common.py +++ b/nw/common.py @@ -193,3 +193,27 @@ def transferCase(theSource, theTarget): theResult = theTarget.lower() return theResult + +def fuzzyTime(secDiff): + """Converts a time difference in seconds into a fuzzy time string. + """ + if secDiff < 0: + return "in the future" + elif secDiff < 15: + return "just now" + elif secDiff < 45: + return "a few seconds ago" + elif secDiff < 90: + return "a minute ago" + elif secDiff < 3300: # 55 minutes + return "%d minutes ago" % int(round(secDiff/60)) + elif secDiff < 5400: # 90 minutes + return "an hour ago" + elif secDiff < 84600: # 23.5 hours + return "%d hours ago" % int(round(secDiff/3600)) + elif secDiff < 129600: # 1.5 days + return "a day ago" + elif secDiff < 8640000: # 100 days + return "%d days ago" % int(round(secDiff/86400)) + else: + return "ages ago" diff --git a/nw/gui/build.py b/nw/gui/build.py index b7126580..4765bd7e 100644 --- a/nw/gui/build.py +++ b/nw/gui/build.py @@ -33,7 +33,7 @@ from os import path from time import time from datetime import datetime -from PyQt5.QtCore import Qt, QByteArray +from PyQt5.QtCore import Qt, QByteArray, QTimer from PyQt5.QtPrintSupport import QPrinter, QPrintPreviewDialog from PyQt5.QtGui import ( QPalette, QColor, QTextDocumentWriter, QFont @@ -44,6 +44,7 @@ from PyQt5.QtWidgets import ( QFileDialog, QFontDialog, QSpinBox ) +from nw.common import fuzzyTime from nw.gui.custom import QSwitch from nw.core import ToHtml from nw.constants import ( @@ -933,6 +934,7 @@ class GuiBuildNovelDocView(QTextBrowser): self.theProject = theProject self.theParent = theParent self.theTheme = theParent.theTheme + self.buildTime = 0 self.setMinimumWidth(40*self.theParent.theTheme.textNWidth) self.setOpenExternalLinks(False) @@ -979,6 +981,12 @@ class GuiBuildNovelDocView(QTextBrowser): self._updateDocMargins() self.setStyleSheet() + # Age Timer + self.ageTimer = QTimer() + self.ageTimer.setInterval(5000) + self.ageTimer.timeout.connect(self._updateBuildAge) + self.ageTimer.start() + logger.debug("GuiBuildNovelDocView initialisation complete") return @@ -1009,16 +1017,13 @@ class GuiBuildNovelDocView(QTextBrowser): if isinstance(theText, list): theText = "".join(theText) + self.buildTime = timeStamp + theText = theText.replace(" ", " "*4) theText = theText.replace("", "") theText = theText.replace("", "") self.setHtml(theText) - - if timeStamp > 0: - strBuildTime = datetime.fromtimestamp(timeStamp).strftime("%x %X") - else: - strBuildTime = "Unknown" - self.theTitle.setText("Build Time: %s" % strBuildTime) + self._updateBuildAge() return @@ -1056,6 +1061,19 @@ class GuiBuildNovelDocView(QTextBrowser): # Internal Functions ## + def _updateBuildAge(self): + """ + """ + if self.buildTime > 0: + strBuildTime = "%s (%s)" % ( + datetime.fromtimestamp(self.buildTime).strftime("%x %X"), + fuzzyTime(time() - self.buildTime) + ) + else: + strBuildTime = "Unknown" + self.theTitle.setText("Build Time: %s" % strBuildTime) + + def _updateDocMargins(self): """Automatically adjust the header to fill the top of the document within the viewport.