From 170c2dc19c63f6ce5b3a59432660a5625ff5bee3 Mon Sep 17 00:00:00 2001
From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com>
Date: Fri, 23 Oct 2020 20:26:29 +0200
Subject: [PATCH 1/4] Added a new formatTime function to common, and replaced
similar functions in other classes
---
nw/common.py | 10 ++++++++++
nw/gui/statusbar.py | 6 ++----
nw/gui/writingstats.py | 38 +++++++++++---------------------------
tests/test_common.py | 22 ++++++++++++++++++++--
4 files changed, 43 insertions(+), 33 deletions(-)
diff --git a/nw/common.py b/nw/common.py
index 17a8a337..021fa162 100644
--- a/nw/common.py
+++ b/nw/common.py
@@ -169,6 +169,16 @@ def formatTimeStamp(theTime, fileSafe=False):
else:
return datetime.fromtimestamp(theTime).strftime(nwConst.tStampFmt)
+def formatTime(tS):
+ """Format the time spent in 00:00:00 format.
+ """
+ if isinstance(tS, int):
+ if tS >= 86400:
+ return f"{tS//86400:d}-{tS//3600%24:02d}:{tS%3600//60:02d}:{tS%60:02d}"
+ else:
+ return f"{tS//3600:02d}:{tS%3600//60:02d}:{tS%60:02d}"
+ return "ERROR"
+
def splitVersionNumber(vString):
""" Splits a version string on the form aa.bb.cc into major, minor
and patch, and computes an integer value aabbcc.
diff --git a/nw/gui/statusbar.py b/nw/gui/statusbar.py
index 0b95ef16..ace755ae 100644
--- a/nw/gui/statusbar.py
+++ b/nw/gui/statusbar.py
@@ -35,6 +35,7 @@ from PyQt5.QtGui import QColor, QPainter
from PyQt5.QtWidgets import qApp, QStatusBar, QLabel, QAbstractButton
from nw.core import NWSpellCheck
+from nw.common import formatTime
logger = logging.getLogger(__name__)
@@ -206,10 +207,7 @@ class GuiMainStatus(QStatusBar):
if self.refTime is None:
self.timeText.setText("00:00:00")
else:
- tS = int(time() - self.refTime)
- self.timeText.setText(
- f"{tS//3600:02d}:{tS%3600//60:02d}:{tS%60:02d}"
- )
+ self.timeText.setText(formatTime(round(time() - self.refTime)))
return
# END Class GuiMainStatus
diff --git a/nw/gui/writingstats.py b/nw/gui/writingstats.py
index fa03a278..170d8cf8 100644
--- a/nw/gui/writingstats.py
+++ b/nw/gui/writingstats.py
@@ -39,6 +39,7 @@ from PyQt5.QtWidgets import (
QLabel, QGroupBox, QMenu, QAction, QFileDialog, QSpinBox, QHBoxLayout
)
+from nw.common import formatTime
from nw.constants import nwConst, nwFiles, nwAlert
from nw.gui.custom import QSwitch
@@ -123,11 +124,11 @@ class GuiWritingStats(QDialog):
self.infoForm = QGridLayout(self)
self.infoBox.setLayout(self.infoForm)
- self.labelTotal = QLabel(self._formatTime(0))
+ self.labelTotal = QLabel(formatTime(0))
self.labelTotal.setFont(self.theTheme.guiFontFixed)
self.labelTotal.setAlignment(Qt.AlignVCenter | Qt.AlignRight)
- self.labelFilter = QLabel(self._formatTime(0))
+ self.labelFilter = QLabel(formatTime(0))
self.labelFilter.setFont(self.theTheme.guiFontFixed)
self.labelFilter.setAlignment(Qt.AlignVCenter | Qt.AlignRight)
@@ -367,34 +368,26 @@ class GuiWritingStats(QDialog):
elif dataFmt == self.FMT_CSV:
outFile.write(
- "\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\n" % (
- "Date", "Length (sec)", "Words Changed", "Novel Words", "Note Words"
- )
+ '"Date","Length (sec)","Words Changed","Novel Words","Note Words"\n'
)
for _, sD, tT, wD, wA, wB in self.filterData:
- outFile.write(
- "\"%s\",%d,%d,%d,%d\n" % (sD, tT, wD, wA, wB)
- )
+ outFile.write(f'"{sD}",{tT:.0f},{wD},{wA},{wB}\n')
wSuccess = True
else:
errMsg = "Unknown format"
except Exception as e:
- errMsg = str(e)
+ errMsg = str(e).replace("\n", "
")
# Report to user
if wSuccess:
self.theParent.makeAlert(
- "%s file successfully written to:
%s" % (
- textFmt, savePath
- ), nwAlert.INFO
+ f"{textFmt} file successfully written to:
{savePath}", nwAlert.INFO
)
else:
self.theParent.makeAlert(
- "Failed to write %s file. %s" % (
- textFmt, errMsg
- ), nwAlert.ERROR
+ f"Failed to write {textFmt} file.
{errMsg}", nwAlert.ERROR
)
return True
@@ -455,7 +448,7 @@ class GuiWritingStats(QDialog):
return False
ttWords = ttNovel + ttNotes
- self.labelTotal.setText(self._formatTime(ttTime))
+ self.labelTotal.setText(formatTime(round(ttTime)))
self.novelWords.setText(f"{ttNovel:n}")
self.notesWords.setText(f"{ttNotes:n}")
self.totalWords.setText(f"{ttWords:n}")
@@ -544,7 +537,7 @@ class GuiWritingStats(QDialog):
newItem = QTreeWidgetItem()
newItem.setText(self.C_TIME, sStart)
- newItem.setText(self.C_LENGTH, self._formatTime(sDiff))
+ newItem.setText(self.C_LENGTH, formatTime(round(sDiff)))
newItem.setText(self.C_COUNT, f"{nWords:n}")
if nWords > 0 and listMax > 0:
@@ -567,17 +560,8 @@ class GuiWritingStats(QDialog):
self.listBox.addTopLevelItem(newItem)
self.timeFilter += sDiff
- self.labelFilter.setText(self._formatTime(self.timeFilter))
+ self.labelFilter.setText(formatTime(round(self.timeFilter)))
return True
- def _formatTime(self, tS):
- """Format the time spent in 00:00:00 format.
- """
- tM = int(tS/60)
- tH = int(tM/60)
- tM = tM - tH*60
- tS = tS - tM*60 - tH*3600
- return "%02d:%02d:%02d" % (tH, tM, tS)
-
# END Class GuiWritingStats
diff --git a/tests/test_common.py b/tests/test_common.py
index 4dc9cfcf..2a24f31f 100644
--- a/tests/test_common.py
+++ b/tests/test_common.py
@@ -7,7 +7,7 @@ import pytest
from nw.common import (
checkString, checkBool, checkInt, colRange, formatInt, transferCase,
- fuzzyTime, checkHandle, formatTimeStamp
+ fuzzyTime, checkHandle, formatTimeStamp, formatTime
)
from nwtools import cmpList
@@ -78,11 +78,29 @@ def testColRange():
)
@pytest.mark.core
-def testFormatTime():
+def testFormatTimeStamp():
tTime = time.mktime(time.gmtime(0))
assert formatTimeStamp(tTime, False) == "1970-01-01 00:00:00"
assert formatTimeStamp(tTime, True) == "1970-01-01 00.00.00"
+@pytest.mark.core
+def testFormatTime():
+ assert formatTime("1") == "ERROR"
+ assert formatTime(1.0) == "ERROR"
+ assert formatTime(1) == "00:00:01"
+ assert formatTime(59) == "00:00:59"
+ assert formatTime(60) == "00:01:00"
+ assert formatTime(180) == "00:03:00"
+ assert formatTime(194) == "00:03:14"
+ assert formatTime(3540) == "00:59:00"
+ assert formatTime(3599) == "00:59:59"
+ assert formatTime(3600) == "01:00:00"
+ assert formatTime(11640) == "03:14:00"
+ assert formatTime(11655) == "03:14:15"
+ assert formatTime(86399) == "23:59:59"
+ assert formatTime(86400) == "1-00:00:00"
+ assert formatTime(360000) == "4-04:00:00"
+
@pytest.mark.core
def testFormatInt():
assert formatInt(1000) == "1000"
From 8db7c17798691e6b23380c60da02ade2407f351c Mon Sep 17 00:00:00 2001
From: "Veronica K. B. Olsen" <1619840+vkbo@users.noreply.github.com>
Date: Fri, 23 Oct 2020 20:56:44 +0200
Subject: [PATCH 2/4] Drop using formatting a few places where it isn't
necessary
---
nw/gui/about.py | 13 ++++++-------
nw/gui/build.py | 8 ++++----
nw/gui/mainmenu.py | 6 +++---
nw/gui/writingstats.py | 8 ++++++--
4 files changed, 19 insertions(+), 16 deletions(-)
diff --git a/nw/gui/about.py b/nw/gui/about.py
index 248cf7d1..7f00cf11 100644
--- a/nw/gui/about.py
+++ b/nw/gui/about.py
@@ -55,14 +55,14 @@ class GuiAbout(QDialog):
self.innerBox = QHBoxLayout()
self.innerBox.setSpacing(self.mainConf.pxInt(16))
- self.setWindowTitle("About %s" % self.mainConf.appName)
+ self.setWindowTitle("About novelWriter")
self.setMinimumWidth(self.mainConf.pxInt(650))
self.setMinimumHeight(self.mainConf.pxInt(600))
nPx = self.mainConf.pxInt(96)
self.nwIcon = QLabel()
self.nwIcon.setPixmap(self.theParent.theTheme.getPixmap("novelwriter", (nPx, nPx)))
- self.lblName = QLabel("%s" % self.mainConf.appName)
+ self.lblName = QLabel("novelWriter")
self.lblVers = QLabel("v%s" % nw.__version__)
self.lblDate = QLabel(datetime.strptime(nw.__date__, "%Y-%m-%d").strftime("%x"))
@@ -115,17 +115,17 @@ class GuiAbout(QDialog):
"""
listPrefix = " • "
aboutMsg = (
- "
{copyright:s}.
" "Website: {domain:s}
" - "{name:s} is a markdown-like text editor designed for " + "
novelWriter is a markdown-like text editor designed for " "organising and writing novels. It is written in Python 3 with a " "Qt5 GUI, using PyQt5.
" - "{name:s} is free software: you can redistribute it and/or " + "
novelWriter is free software: you can redistribute it and/or " "modify it under the terms of the GNU General Public License as " "published by the Free Software Foundation, either version 3 of " "the License, or (at your option) any later version.
" - "{name:s} is distributed in the hope that it will be useful, " + "
novelWriter is distributed in the hope that it will be useful, " "but WITHOUT ANY WARRANTY; without even the implied warranty of " "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
" "See the License tab for the full text, or visit the GNU website " @@ -134,7 +134,6 @@ class GuiAbout(QDialog): "
{credits:s}
" ).format( - name = self.mainConf.appName, copyright = nw.__copyright__, website = nw.__url__, domain = nw.__domain__, diff --git a/nw/gui/build.py b/nw/gui/build.py index 45a1a073..9b8b42ac 100644 --- a/nw/gui/build.py +++ b/nw/gui/build.py @@ -391,11 +391,11 @@ class GuiBuildNovel(QDialog): self.savePDF.triggered.connect(lambda: self._saveDocument(self.FMT_PDF)) self.saveMenu.addAction(self.savePDF) - self.saveHTM = QAction("%s HTML (.htm)" % self.mainConf.appName, self) + self.saveHTM = QAction("novelWriter HTML (.htm)", self) self.saveHTM.triggered.connect(lambda: self._saveDocument(self.FMT_HTM)) self.saveMenu.addAction(self.saveHTM) - self.saveNWD = QAction("%s Markdown (.nwd)" % self.mainConf.appName, self) + self.saveNWD = QAction("novelWriter Markdown (.nwd)", self) self.saveNWD.triggered.connect(lambda: self._saveDocument(self.FMT_NWD)) self.saveMenu.addAction(self.saveNWD) @@ -408,11 +408,11 @@ class GuiBuildNovel(QDialog): self.saveTXT.triggered.connect(lambda: self._saveDocument(self.FMT_TXT)) self.saveMenu.addAction(self.saveTXT) - self.saveJsonH = QAction("JSON + %s HTML (.json)" % self.mainConf.appName, self) + self.saveJsonH = QAction("JSON + novelWriter HTML (.json)", self) self.saveJsonH.triggered.connect(lambda: self._saveDocument(self.FMT_JSON_H)) self.saveMenu.addAction(self.saveJsonH) - self.saveJsonM = QAction("JSON + %s Markdown (.json)" % self.mainConf.appName, self) + self.saveJsonM = QAction("JSON + novelWriters Markdown (.json)", self) self.saveJsonM.triggered.connect(lambda: self._saveDocument(self.FMT_JSON_M)) self.saveMenu.addAction(self.saveJsonM) diff --git a/nw/gui/mainmenu.py b/nw/gui/mainmenu.py index 6bb7edf1..7fcd2a89 100644 --- a/nw/gui/mainmenu.py +++ b/nw/gui/mainmenu.py @@ -274,7 +274,7 @@ class GuiMainMenu(QMenuBar): # Project > Exit self.aExitNW = QAction("Exit", self) - self.aExitNW.setStatusTip("Exit %s" % self.mainConf.appName) + self.aExitNW.setStatusTip("Exit novelWriter") self.aExitNW.setShortcut("Ctrl+Q") self.aExitNW.setMenuRole(QAction.QuitRole) self.aExitNW.triggered.connect(lambda: self.theParent.closeMain()) @@ -857,8 +857,8 @@ class GuiMainMenu(QMenuBar): self.helpMenu = self.addMenu("&Help") # Help > About - self.aAboutNW = QAction("About %s" % self.mainConf.appName, self) - self.aAboutNW.setStatusTip("About %s" % self.mainConf.appName) + self.aAboutNW = QAction("About novelWriter", self) + self.aAboutNW.setStatusTip("About novelWriter") self.aAboutNW.setMenuRole(QAction.AboutRole) self.aAboutNW.triggered.connect(lambda: self.theParent.showAboutNWDialog()) self.helpMenu.addAction(self.aAboutNW) diff --git a/nw/gui/writingstats.py b/nw/gui/writingstats.py index 170d8cf8..c394c43d 100644 --- a/nw/gui/writingstats.py +++ b/nw/gui/writingstats.py @@ -383,11 +383,15 @@ class GuiWritingStats(QDialog): # Report to user if wSuccess: self.theParent.makeAlert( - f"{textFmt} file successfully written to: