summaryrefslogtreecommitdiff
path: root/ui.go
diff options
context:
space:
mode:
Diffstat (limited to 'ui.go')
-rw-r--r--ui.go55
1 files changed, 37 insertions, 18 deletions
diff --git a/ui.go b/ui.go
index eb8d077..5e68171 100644
--- a/ui.go
+++ b/ui.go
@@ -23,9 +23,12 @@ type model struct {
wordInput textinput.Model
definitionViewport viewport.Model
- ankiDeck string
- ankiModel string
- apiURL string
+ ankiDeck string
+ ankiModel string
+ apiURL string
+ rawDictionaryPath string
+
+ dictionaryReady bool
currentWord string
currentDefinition string
@@ -37,9 +40,10 @@ type (
errMsg error
definitionMsg string
wordAddedMsg string
+ isDBEmptyMsg bool
)
-func initialModel(c *http.Client, db *sql.DB, apiURL, ankiDeck, ankiModel, firstWord string) model {
+func initialModel(c *http.Client, db *sql.DB, apiURL, ankiDeck, ankiModel, rawDictionary, firstWord string) model {
input := textinput.New()
input.Placeholder = ""
input.Focus()
@@ -70,9 +74,10 @@ func initialModel(c *http.Client, db *sql.DB, apiURL, ankiDeck, ankiModel, first
wordInput: input,
definitionViewport: textbox,
- ankiDeck: ankiDeck,
- ankiModel: ankiModel,
- apiURL: apiURL,
+ ankiDeck: ankiDeck,
+ ankiModel: ankiModel,
+ apiURL: apiURL,
+ rawDictionaryPath: rawDictionary,
currentWord: firstWord,
}
@@ -80,9 +85,7 @@ func initialModel(c *http.Client, db *sql.DB, apiURL, ankiDeck, ankiModel, first
func (m model) Init() tea.Cmd {
cmds := []tea.Cmd{textinput.Blink}
- if m.currentWord != "" {
- cmds = append(cmds, lookupWord(m.db, m.currentWord))
- }
+ cmds = append(cmds, isDatabaseEmpty(m.db))
return tea.Batch(cmds...)
}
@@ -92,6 +95,16 @@ 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 {
+ m.dictionaryReady = true
+ if m.currentWord != "" {
+ textPickerLongerCmds = append(textPickerLongerCmds, lookupWord(m.db, m.currentWord))
+ }
+ }
case definitionMsg:
m.currentDefinition = string(msg)
m.err = nil
@@ -144,14 +157,20 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
func (m model) View() string {
- return fmt.Sprintf(
- "\x1b[1;30;42mLook up a word:\x1b[0m\n\n%s\n\n\x1b[1;30;42mStatus:\x1b[0m %s\n\n%s\n\n%s\n%s",
- m.wordInput.View(),
- formatStatus(m.err, m.statusString),
- "(Ctrl-C to quit, Esc to clear, Enter to add to Anki, PgUp/Down to scroll)",
- "\x1b[1;30;42mCurrent definition:\x1b[0m\n",
- m.definitionViewport.View(),
- ) + "\n"
+ if m.dictionaryReady {
+ return fmt.Sprintf(
+ "\x1b[1;30;42mLook up a word:\x1b[0m\n\n%s\n\n\x1b[1;30;42mStatus:\x1b[0m %s\n\n%s\n\n%s\n%s",
+ m.wordInput.View(),
+ formatStatus(m.err, m.statusString),
+ "(Ctrl-C to quit, Esc to clear, Enter to add to Anki, PgUp/Down to scroll)",
+ "\x1b[1;30;42mCurrent definition:\x1b[0m\n",
+ m.definitionViewport.View(),
+ ) + "\n"
+ }
+ 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...")
}
func formatDefinitionForDisplay(policy bluemonday.Policy, definition string, maxWidth int) string {