diff options
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | main.go | 14 |
2 files changed, 11 insertions, 4 deletions
@@ -5,7 +5,6 @@ You need a copy of https://kaikki.org/frwiktionary/raw-wiktextract-data.jsonl.gz # TODO -- height of viewport should be dynamic - no hardcoded paths, or Anki info - better setup instructions (intall plugin, set deck & card, properties, etc) - clear the screen after we finish the initial import, probably?
\ No newline at end of file @@ -94,7 +94,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case definitionMsg: m.currentDefinition = string(msg) m.err = nil - m.vp.SetContent(formatDefinitionForDisplay(m.p, m.currentDefinition)) + m.vp.SetContent(formatDefinitionForDisplay(m.p, m.currentDefinition, m.vp.Width)) return m, nil case wordAddedMsg: m.wordAddStatus = fmt.Sprintf("✅ Added '%s' to Anki", string(msg)) @@ -103,6 +103,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { m.wordInput.SetValue("") m.vp.SetContent("") m.err = nil + case tea.WindowSizeMsg: + // headerHeight is the height of everything above the definition window. + headerHeight := 11 + m.vp.Width = msg.Width + m.vp.Height = msg.Height - headerHeight + m.vp.SetContent(formatDefinitionForDisplay(m.p, m.currentDefinition, m.vp.Width)) case tea.KeyMsg: switch msg.Type { case tea.KeyCtrlC: @@ -148,14 +154,16 @@ func (m model) View() string { ) + "\n" } -func formatDefinitionForDisplay(policy bluemonday.Policy, definition string) string { +func formatDefinitionForDisplay(policy bluemonday.Policy, definition string, maxWidth int) string { str := strings.ReplaceAll(definition, "<li class=sense>", "<li class=sense>- ") str = strings.ReplaceAll(str, "\t<ul><li><i>", "\n\t<ul><li><i>\x1b[3;39;49m") str = strings.ReplaceAll(str, "</i></li></ul></li>", "</i></li></ul></li>\x1b[0m") str = policy.Sanitize(str) str = strings.ReplaceAll(str, "\t- ", "\x1b[0;33;49m•\x1b[0m ") - return wordwrap.String(str, 72) + width := min(maxWidth, 80) + + return wordwrap.String(str, width) } func formatStatus(lastError error, lastSuccess string) string { |
