diff --git a/data/latest.md b/data/latest.md new file mode 100644 index 0000000..bb8a261 --- /dev/null +++ b/data/latest.md @@ -0,0 +1,2 @@ +nuh it just saves and loads a bunch of empty lines what the fukcf fsfsa fsadf +asdfsafsafsafdsafsafsdf \ No newline at end of file diff --git a/src/ui/ui.py b/src/ui/ui.py index f0f1692..d6128c9 100644 --- a/src/ui/ui.py +++ b/src/ui/ui.py @@ -12,6 +12,7 @@ getCurrentMetrics, getPopularWords, getTextFromFile, + saveTextOnExit, ) from components.AlertPopup import AlertPopup @@ -44,6 +45,8 @@ def updateMetrics( text=f"Time to read: {timeToRead} min" if timeToRead else "No words found" ) + saveTextOnExit(text) + def renderInputSection(root) -> tuple[CTkLabel, CTkTextbox]: getTextHeading = CTkLabel( @@ -155,12 +158,17 @@ def showPopularWords(text): def saveTextToFile(text): - if text == "" or text is None or len(text) == 0: - console.log("Failed to save text to file since it's empty") - AlertPopup("Cannot save an empty text!") + try: + filePath = f"data/{generateFileName(text)}.md" + except Exception as e: + console.log( + "Failed to save text to file, most likely due to input field being empty" + ) + AlertPopup( + "Could not save text to file. Make sure the input field is not empty." + ) return - filePath = f"data/{generateFileName(text)}.md" _, linesCount, symbolsCount, wordsCount, _ = getCurrentMetrics(text) metaData = f"""--- words: {wordsCount} @@ -174,7 +182,7 @@ def saveTextToFile(text): f.write(text) -def renderMainTab(root) -> None: +def renderMainTab(root) -> CTkTextbox: getTextHeading, getTextInput = renderInputSection(root) ( @@ -214,3 +222,17 @@ def renderMainTab(root) -> None: resultsReadingTime, ), ) + + try: + with open("data/latest.md", "r") as f: + loadedText = f.read() + getTextInput.insert("0.0", loadedText) + updateMetrics( + loadedText, + resultsLines, + resultsSymbols, + resultsWords, + resultsReadingTime, + ) + except Exception as e: + console.log("Failed to load file") diff --git a/src/util/utils.py b/src/util/utils.py index c9a07a7..5190982 100644 --- a/src/util/utils.py +++ b/src/util/utils.py @@ -79,3 +79,14 @@ def generateFileName(text): fileName = f"{firstTwoWords}_{currentTime}" return fileName + + +def saveTextOnExit(text): + text = [ + line for line in text.split("\n") if line != "" or line != "\n" or line != " " + ] + text.pop() + console.print(text) + filePath = f"data/latest.md" + with open(filePath, "w") as f: + f.write("\n".join(text))