summaryrefslogtreecommitdiff
path: root/ui.go
diff options
context:
space:
mode:
Diffstat (limited to 'ui.go')
-rw-r--r--ui.go32
1 files changed, 22 insertions, 10 deletions
diff --git a/ui.go b/ui.go
index 5e68171..9bac321 100644
--- a/ui.go
+++ b/ui.go
@@ -19,6 +19,7 @@ type model struct {
db *sql.DB
c *http.Client
p bluemonday.Policy
+ dp dictionaryPopulator
wordInput textinput.Model
definitionViewport viewport.Model
@@ -37,10 +38,11 @@ type model struct {
}
type (
- errMsg error
- definitionMsg string
- wordAddedMsg string
- isDBEmptyMsg bool
+ errMsg error
+ definitionMsg string
+ wordAddedMsg string
+ isDictionaryEmptyMsg bool
+ populatingDictionaryMsg *dictionaryPopulator
)
func initialModel(c *http.Client, db *sql.DB, apiURL, ankiDeck, ankiModel, rawDictionary, firstWord string) model {
@@ -66,10 +68,17 @@ func initialModel(c *http.Client, db *sql.DB, apiURL, ankiDeck, ankiModel, rawDi
),
}
+ dp := dictionaryPopulator{
+ db: db,
+ rawDictionaryPath: rawDictionary,
+ langCode: "fr",
+ }
+
return model{
db: db,
c: c,
p: *bluemonday.StrictPolicy(),
+ dp: dp,
wordInput: input,
definitionViewport: textbox,
@@ -95,16 +104,19 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var textPickerLongerCmds []tea.Cmd
switch msg := msg.(type) {
- case isDBEmptyMsg:
- if bool(msg) {
- // set up the database
- return m, populateDictionary(m.rawDictionaryPath, m.db)
- } else {
+ case isDictionaryEmptyMsg:
+ if bool(msg) { // We need to populate the dictionary
+ return m, setupPopulator(&m.dp)
+ } else { // The dictionary is ready
m.dictionaryReady = true
+ m.statusString = ""
if m.currentWord != "" {
textPickerLongerCmds = append(textPickerLongerCmds, lookupWord(m.db, m.currentWord))
}
}
+ case populatingDictionaryMsg:
+ m.statusString = fmt.Sprintf("Currently on line %d...", msg.currentLine)
+ return m, populateDictionary((*dictionaryPopulator)(msg))
case definitionMsg:
m.currentDefinition = string(msg)
m.err = nil
@@ -170,7 +182,7 @@ func (m model) View() string {
if m.err != nil {
return fmt.Sprintf("Failed to load dictionary! Error:\n\n%s\n\nExit with Control-C.\n", m.err)
}
- return fmt.Sprintln("Preparing dictionary...")
+ return fmt.Sprintf("Preparing dictionary...\n\n%s\n\n", m.statusString)
}
func formatDefinitionForDisplay(policy bluemonday.Policy, definition string, maxWidth int) string {