Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
feat: add auto-saving and loading of text from previous session
Browse files Browse the repository at this point in the history
  • Loading branch information
seesmof committed Dec 22, 2023
1 parent dbf0db6 commit 626197e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
2 changes: 2 additions & 0 deletions data/latest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nuh it just saves and loads a bunch of empty lines what the fukcf fsfsa fsadf
asdfsafsafsafdsafsafsdf
32 changes: 27 additions & 5 deletions src/ui/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
getCurrentMetrics,
getPopularWords,
getTextFromFile,
saveTextOnExit,
)
from components.AlertPopup import AlertPopup

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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}
Expand All @@ -174,7 +182,7 @@ def saveTextToFile(text):
f.write(text)


def renderMainTab(root) -> None:
def renderMainTab(root) -> CTkTextbox:
getTextHeading, getTextInput = renderInputSection(root)

(
Expand Down Expand Up @@ -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")
11 changes: 11 additions & 0 deletions src/util/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit 626197e

Please sign in to comment.