Also added a fuzzy time string to the build doc header to make it easier to see the age of the build

This commit is contained in:
Veronica K. B. Olsen
2020-06-23 20:23:52 +02:00
parent b47d6b6546
commit bc8873fdd4
2 changed files with 49 additions and 7 deletions
+24
View File
@@ -193,3 +193,27 @@ def transferCase(theSource, theTarget):
theResult = theTarget.lower() theResult = theTarget.lower()
return theResult 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"
+25 -7
View File
@@ -33,7 +33,7 @@ from os import path
from time import time from time import time
from datetime import datetime 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.QtPrintSupport import QPrinter, QPrintPreviewDialog
from PyQt5.QtGui import ( from PyQt5.QtGui import (
QPalette, QColor, QTextDocumentWriter, QFont QPalette, QColor, QTextDocumentWriter, QFont
@@ -44,6 +44,7 @@ from PyQt5.QtWidgets import (
QFileDialog, QFontDialog, QSpinBox QFileDialog, QFontDialog, QSpinBox
) )
from nw.common import fuzzyTime
from nw.gui.custom import QSwitch from nw.gui.custom import QSwitch
from nw.core import ToHtml from nw.core import ToHtml
from nw.constants import ( from nw.constants import (
@@ -933,6 +934,7 @@ class GuiBuildNovelDocView(QTextBrowser):
self.theProject = theProject self.theProject = theProject
self.theParent = theParent self.theParent = theParent
self.theTheme = theParent.theTheme self.theTheme = theParent.theTheme
self.buildTime = 0
self.setMinimumWidth(40*self.theParent.theTheme.textNWidth) self.setMinimumWidth(40*self.theParent.theTheme.textNWidth)
self.setOpenExternalLinks(False) self.setOpenExternalLinks(False)
@@ -979,6 +981,12 @@ class GuiBuildNovelDocView(QTextBrowser):
self._updateDocMargins() self._updateDocMargins()
self.setStyleSheet() 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") logger.debug("GuiBuildNovelDocView initialisation complete")
return return
@@ -1009,16 +1017,13 @@ class GuiBuildNovelDocView(QTextBrowser):
if isinstance(theText, list): if isinstance(theText, list):
theText = "".join(theText) theText = "".join(theText)
self.buildTime = timeStamp
theText = theText.replace("&emsp;", "&nbsp;"*4) theText = theText.replace("&emsp;", "&nbsp;"*4)
theText = theText.replace("<del>", "<span style='text-decoration: line-through;'>") theText = theText.replace("<del>", "<span style='text-decoration: line-through;'>")
theText = theText.replace("</del>", "</span>") theText = theText.replace("</del>", "</span>")
self.setHtml(theText) self.setHtml(theText)
self._updateBuildAge()
if timeStamp > 0:
strBuildTime = datetime.fromtimestamp(timeStamp).strftime("%x %X")
else:
strBuildTime = "Unknown"
self.theTitle.setText("Build Time: %s" % strBuildTime)
return return
@@ -1056,6 +1061,19 @@ class GuiBuildNovelDocView(QTextBrowser):
# Internal Functions # 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): def _updateDocMargins(self):
"""Automatically adjust the header to fill the top of the """Automatically adjust the header to fill the top of the
document within the viewport. document within the viewport.