aboutsummaryrefslogtreecommitdiff
path: root/openrussian-completion.bash
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2019-04-18 23:20:46 +0300
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2019-04-18 23:20:46 +0300
commit481c69be02c95fbf20567d8de617315484e2d692 (patch)
tree71bf406543586f278a5e779a0befa57f0fe529e9 /openrussian-completion.bash
parent07d4fa1fe85eba554becb20bb4a872acb320a423 (diff)
downloadopenrussian-cli-481c69be02c95fbf20567d8de617315484e2d692.tar.gz
major changes (unfortunately not done systematically)
* keep the openrussian-sql.zip to avoid bitrot (sudden breakage when building the project against a recent DB) * allow lookups against inflections. A special table `bare_inflections` was added to the DB in order to speed this up. * authentic autocompletions taking every parameter into account, using a magic `-C` parameter * language selection via locale or -L<lang> * translation lookup * allow multiple search terms on the command line without escaping * all SQL strings are properly escaped now to avoid (accidental) code insertion * luautf8 is mandatory now and used much more, which is safer than handling Unicode strings with the builtin functions * ignore "disable" database entries * added option -V (verbatim) * more protections against empty fields * print "DERIVED FROM", "AUDIO", "USAGE" and various word relation sections * print word ranking and level (A1-C2...)
Diffstat (limited to 'openrussian-completion.bash')
-rw-r--r--openrussian-completion.bash37
1 files changed, 12 insertions, 25 deletions
diff --git a/openrussian-completion.bash b/openrussian-completion.bash
index fd27c06..9892d65 100644
--- a/openrussian-completion.bash
+++ b/openrussian-completion.bash
@@ -1,33 +1,20 @@
#/bin/bash
-# NOTE: Uses SQL for matching instead of compgen, since it's probably not
-# a good idea to always retrieve all the words, although this happens anyway
-# when completing an empty word.
-
_openrussian_completions()
{
- # NOTE: sqlite3's command-line tool does not provide a way to properly
- # embed strings, so we try to escape it here.
- WORD=$(echo -n "$2" | sed 's/"/""/g')
-
- # This is a rather convoluted way of writing
- # SELECT bare FROM words WHERE bare LIKE "${WORD}%";
- # but allowing $WORD to contain accentuation characters, which can easily
- # happen when cutting/pasting words from the openrussian.lua manpages or the
- # internet.
- # NOTE: This is merely a workaround since all completions will begin with
- # $WORD including accents and end without accents, so the suggested completions
- # will likely be with wrong accents.
- # It seems to be impossible, at least in Bash, to rubout $WORD first.
- SQL=$(printf 'SELECT "%s" || SUBSTR(bare, LENGTH(REPLACE("%s", "\u0301", ""))+1)
- FROM words WHERE bare LIKE REPLACE("%s%%", "\u0301", "")' \
- "$WORD" "$WORD" "$WORD")
-
- # Calculate database path based on the installation path of the `openrussian`
- # CLI tool. This way, we can avoid preprocessing the script during installation.
- PREFIX=$(dirname $(which openrussian))/..
+ # Autocompletion of all search terms can be outsourced
+ # to the openrussian.lua script, which has the advantage
+ # of taking all command line flags into account.
+ #
+ # NOTE: mapfile is used to properly capture lines containing
+ # whitespace
+ # FIXME: Does not work if the last token contains spaces
+ # FIXME: Also fails when trying to complete multiple tokens
+ # (in order to avoid white-space quoting).
+ mapfile -t COMPREPLY < <(openrussian -C "${COMP_WORDS[@]:1}" 2>/dev/null)
- COMPREPLY=($(sqlite3 "$PREFIX/share/openrussian/openrussian-sqlite3.db" "$SQL"))
+ # NOTE: openrussian.lua currently does not complete switch-names.
+ COMPREPLY+=($(compgen -W "-Len -Lde -V -p" -- "$2"))
}
complete -F _openrussian_completions openrussian