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

Commit

Permalink
feat: add loading from file
Browse files Browse the repository at this point in the history
  • Loading branch information
seesmof committed Dec 22, 2023
1 parent ad800fe commit 9642555
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
32 changes: 29 additions & 3 deletions src/ui/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
console = Console()


from util.utils import getCurrentMetrics, showPopularWords
from util.utils import getCurrentMetrics, getTextFromFile, showPopularWords


def updateMetrics(
Expand Down Expand Up @@ -97,7 +97,26 @@ def renderButtonsSection(root) -> tuple[CTkLabel, CTkButton, CTkButton, CTkButto
loadTextFromFileButton.place(x=210, y=260)
saveTextToFileButton.place(x=210, y=300)

return interactWithTextHeading, showMostPopularWordsButton, loadTextFromFileButton
return (
interactWithTextHeading,
showMostPopularWordsButton,
loadTextFromFileButton,
saveTextToFileButton,
)


def loadTextFromFile(
textField: CTkTextbox,
resultsLines: CTkLabel,
resultsSymbols: CTkLabel,
resultsWords: CTkLabel,
resultsReadingTime: CTkLabel,
) -> None:
textFromFile = getTextFromFile()
textField.insert("0.0", textFromFile)
updateMetrics(
textFromFile, resultsLines, resultsSymbols, resultsWords, resultsReadingTime
)


def renderMainTab(root) -> None:
Expand All @@ -115,14 +134,21 @@ def renderMainTab(root) -> None:
interactWithTextHeading,
showMostPopularWordsButton,
loadTextFromFileButton,
saveTextToFileButton,
) = renderButtonsSection(root)

showMostPopularWordsButton.configure(
command=lambda: showPopularWords(getTextInput.get("0.0", "end"))
)
loadTextFromFileButton.configure(
command=lambda: console.print("TODO: load text from file")
command=lambda: loadTextFromFile(
getTextInput, resultsLines, resultsSymbols, resultsWords, resultsReadingTime
)
)
saveTextToFileButton.configure(
command=lambda: console.print("TODO: save text to file")
)

getTextInput.bind(
"<KeyRelease>",
lambda event: updateMetrics(
Expand Down
19 changes: 19 additions & 0 deletions src/util/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import defaultdict
from math import ceil
from rich.console import Console
from customtkinter import *

from components.AlertPopup import AlertPopup

Expand Down Expand Up @@ -78,3 +79,21 @@ def getCurrentMetrics(text) -> tuple[int, int, int, int]:
timeToRead = ceil(wordsCount / 200)

return lines, linesCount, symbolsCount, wordsCount, timeToRead


def readTextFromFile(filePath):
try:
with open(filePath, "r") as f:
text = f.read()
except Exception as e:
console.print(e)
text = ""
return text


def getTextFromFile():
getFilePath = CTkInputDialog(text="Enter file path", title="Load Text")
filePath = getFilePath.get_input()
textFromFile = readTextFromFile(filePath)

return textFromFile

0 comments on commit 9642555

Please sign in to comment.