From 53e60c7b59e885af858efd2893eb58dff1acdd3c Mon Sep 17 00:00:00 2001 From: Veronica Berglyd Olsen <1619840+vkbo@users.noreply.github.com> Date: Tue, 29 Apr 2025 23:31:25 +0200 Subject: [PATCH] Add character counts to session log --- novelwriter/core/sessions.py | 42 ++++++++++++++++++--------- tests/test_core/test_core_sessions.py | 14 +++++---- tests/test_core/test_core_storage.py | 8 +++-- 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/novelwriter/core/sessions.py b/novelwriter/core/sessions.py index 659b6f9e..6936ed99 100644 --- a/novelwriter/core/sessions.py +++ b/novelwriter/core/sessions.py @@ -79,29 +79,36 @@ class NWSessionLog: return False now = time() - iNovel, iNotes, _, _ = self._project.data.initCounts - cNovel, cNotes, _, _ = self._project.data.currCounts - iTotal = iNovel + iNotes - wDiff = cNovel + cNotes - iTotal + iWNovel, iWNotes, iCNovel, iCNotes = self._project.data.initCounts + cWNovel, cWNotes, cCNovel, cCNotes = self._project.data.currCounts + iWTotal = iWNovel + iWNotes + iCTotal = iCNovel + iCNotes + wDiff = cWNovel + cWNotes - iWTotal + cDiff = cCNovel + cCNotes - iCTotal sTime = now - self._start - logger.info("The session lasted %d sec and added %d words", int(sTime), wDiff) - if sTime < 300 and wDiff == 0: + logger.info( + "The session lasted %d sec and added %d words abd %d characters", + int(sTime), wDiff, cDiff + ) + if sTime < 300 and (wDiff == 0 or cDiff == 0): logger.info("Session too short, skipping log entry") return False try: if not sessFile.exists(): with open(sessFile, mode="w", encoding="utf-8") as fObj: - fObj.write(self.createInitial(iTotal)) + fObj.write(self.createInitial(iWTotal)) with open(sessFile, mode="a+", encoding="utf-8") as fObj: fObj.write(self.createRecord( start=formatTimeStamp(self._start), end=formatTimeStamp(now), - novel=cNovel, - notes=cNotes, - idle=round(idleTime) + novel=cWNovel, + notes=cWNotes, + idle=round(idleTime), + cnovel=cCNovel, + cnotes=cCNotes, )) except Exception: @@ -129,10 +136,19 @@ class NWSessionLog: data = json.dumps({"type": "initial", "offset": total}) return f"{data}\n" - def createRecord(self, start: str, end: str, novel: int, notes: int, idle: int) -> str: + def createRecord( + self, start: str, end: str, novel: int, notes: int, idle: int, + cnovel: int = 0, cnotes: int = 0, + ) -> str: """Low level function to create a log record.""" data = json.dumps({ - "type": "record", "start": start, "end": end, - "novel": novel, "notes": notes, "idle": idle, + "type": "record", + "start": start, + "end": end, + "novel": novel, + "notes": notes, + "cnovel": cnovel, + "cnotes": cnotes, + "idle": idle, }) return f"{data}\n" diff --git a/tests/test_core/test_core_sessions.py b/tests/test_core/test_core_sessions.py index 93b6942f..d601fcd7 100644 --- a/tests/test_core/test_core_sessions.py +++ b/tests/test_core/test_core_sessions.py @@ -43,8 +43,8 @@ def testCoreSessions_Main(monkeypatch, mockGUI, fncPath): assert isinstance(logFile, Path) # Set some mock word counts - project.data.setInitCounts(50, 60) - project.data.setCurrCounts(160, 150) + project.data.setInitCounts(50, 60, 500, 600) + project.data.setCurrCounts(160, 150, 1600, 1500) # The project init should already have created the session sessLog = project.session @@ -71,17 +71,19 @@ def testCoreSessions_Main(monkeypatch, mockGUI, fncPath): assert records[1]["type"] == "record" assert records[1]["novel"] == 160 assert records[1]["notes"] == 150 + assert records[1]["cnovel"] == 1600 + assert records[1]["cnotes"] == 1500 assert records[1]["idle"] == 1 # Should be rounded to full seconds # Adding another record without changing word count should do nothing - project.data.setInitCounts(160, 150) - project.data.setCurrCounts(160, 150) + project.data.setInitCounts(160, 150, 1600, 1500) + project.data.setCurrCounts(160, 150, 1600, 1500) assert sessLog.appendSession(1.6) is False assert len(list(sessLog.iterRecords())) == 2 # But adding when count has changed should - project.data.setInitCounts(160, 150) - project.data.setCurrCounts(270, 240) + project.data.setInitCounts(160, 150, 1600, 1500) + project.data.setCurrCounts(270, 240, 2700, 2400) sessLog._start -= 350.0 # Backdate the session start to allow logging assert sessLog.appendSession(1.6) is True records = list(sessLog.iterRecords()) diff --git a/tests/test_core/test_core_storage.py b/tests/test_core/test_core_storage.py index 7ce7deb3..6e006d43 100644 --- a/tests/test_core/test_core_storage.py +++ b/tests/test_core/test_core_storage.py @@ -435,8 +435,8 @@ def testCoreStorage_OldFormatConvert(monkeypatch, mockGUI, fncPath): sessLogOld.write_text(( "# Offset 150\n" "# Start Time End Time Novel Notes Idle\n" - "2021-02-02 02:02:02 2021-02-02 03:03:03 200 200 10\n" - "2021-03-03 03:03:03 2021-03-03 04:04:04 300 300 20\n" + "2021-02-02 02:02:02 2021-02-02 03:03:03 200 200 10\n" + "2021-03-03 03:03:03 2021-03-03 04:04:04 300 300 20\n" ), encoding="utf-8") assert sessLogOld.exists() is True @@ -496,6 +496,8 @@ def testCoreStorage_OldFormatConvert(monkeypatch, mockGUI, fncPath): "end": "2021-02-02 03:03:03", "novel": 200, "notes": 200, + "cnovel": 0, + "cnotes": 0, "idle": 10, } assert data[2] == { @@ -504,6 +506,8 @@ def testCoreStorage_OldFormatConvert(monkeypatch, mockGUI, fncPath): "end": "2021-03-03 04:04:04", "novel": 300, "notes": 300, + "cnovel": 0, + "cnotes": 0, "idle": 20, }