From 882c41c7df91a8851e904caa85f9a8259ac44570 Mon Sep 17 00:00:00 2001 From: David Schlachter Date: Thu, 8 Jan 2026 13:29:44 -0500 Subject: Allow invoking with a word to look up --- README.md | 24 ++++++++++++++++++++++++ main.go | 3 ++- ui.go | 13 +++++++++++-- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index ace4e96..91cc9d2 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,28 @@ command like this to start the app: go run . -rawDictionary=raw-wiktextract-data.jsonl -deck="Français" -model="Basic-830ae" ``` +In addition to starting the program and then looking up a word interactively, +it's also possible to provide the first word to look up as a command-line +argument. Then, for example, you could add a shell function to invoke the +program and immediatelly go to a definition by running something like this. + +For example, if you've compiled the program and placed it in your path, you +could add a shell function to your `.zshrc` or `.bashrc` with all the argument +you need, like this: + +``` +wk() { + french-wiktionary-flashcards dictionary=/path/to/dictionary.sqlite3 -deck="Français" -model="Basic-830ae" -initialWord="$*" +} +``` + +and then, to start the program and go immediately to a definition (e.g. +"poisson"), you could invoke it like this: + +``` +$ wk poisson +``` + ## Usage ``` @@ -50,6 +72,8 @@ Usage of french-wiktionary-flashcards: from rawDictionary. (default "dictionary.sqlite3") -model string Name of the card type ('model') for new Anki cards. + -initialWord string + Optional: first word to look up on program launch. -rawDictionary string Path to the raw wiktionary data. You can get this by downloading and unzipping diff --git a/main.go b/main.go index 809dd9d..f173107 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ func main() { deckName := flag.String("deck", "", "Name of the deck where new Anki cards will be created.") modelName := flag.String("model", "", "Name of the card type ('model') for new Anki cards.") apiURL := flag.String("apiURL", "http://localhost:8765", "Base URL to access the anki-connect plugin API.") + initialWord := flag.String("initialWord", "", "Optional: first word to look up on program launch.") flag.Parse() @@ -49,7 +50,7 @@ func main() { c := http.DefaultClient c.Timeout = 5 * time.Second - p := tea.NewProgram(initialModel(c, db, *apiURL, *deckName, *modelName)) + p := tea.NewProgram(initialModel(c, db, *apiURL, *deckName, *modelName, *initialWord)) if _, err := p.Run(); err != nil { log.Fatalf("Unexpected error encountered while running program: %s", err) } diff --git a/ui.go b/ui.go index 6b3f8e9..62de3cd 100644 --- a/ui.go +++ b/ui.go @@ -36,7 +36,7 @@ type ( wordAddedMsg string ) -func initialModel(c *http.Client, db *sql.DB, apiURL, ankiDeck, ankiModel string) model { +func initialModel(c *http.Client, db *sql.DB, apiURL, ankiDeck, ankiModel, firstWord string) model { ti := textinput.New() ti.Placeholder = "" ti.Focus() @@ -44,6 +44,10 @@ func initialModel(c *http.Client, db *sql.DB, apiURL, ankiDeck, ankiModel string ti.Width = 36 vp := viewport.New(80, 30) + if firstWord != "" { + ti.SetValue(firstWord) + } + return model{ wordInput: ti, err: nil, @@ -55,11 +59,16 @@ func initialModel(c *http.Client, db *sql.DB, apiURL, ankiDeck, ankiModel string ankiDeck: ankiDeck, ankiModel: ankiModel, apiURL: apiURL, + currentWord: firstWord, } } func (m model) Init() tea.Cmd { - return textinput.Blink + cmds := []tea.Cmd{textinput.Blink} + if m.currentWord != "" { + cmds = append(cmds, lookupWord(m.db, m.currentWord)) + } + return tea.Batch(cmds...) } func lookupWord(db *sql.DB, word string) tea.Cmd { -- cgit v1.2.3