diff options
author | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2019-03-15 22:29:55 +0100 |
---|---|---|
committer | Robin Haberkorn <robin.haberkorn@googlemail.com> | 2019-03-15 22:29:55 +0100 |
commit | 7e2ff6435cd1a5a504eab34823a4f63c7a46df1f (patch) | |
tree | 042c0c23ecf01cf7e25713fbd070bea0f8c63d5b | |
parent | 92f8b625b5bdde6f87a99c4415ee218496fec343 (diff) | |
download | openrussian-cli-7e2ff6435cd1a5a504eab34823a4f63c7a46df1f.tar.gz |
added Makefile and simple Bash completions
* this allows precompiling openrussian.lua,
automatically downloads, converts and VACUUMs the openrussian
database and provides an `install` target
* Bash completions allow completing "bare" Russian words for the
time being.
Unfortunately we cannot complete with accents, since Bash completions
do not allow to change the already typed word (apparently) and users
aren't going to type in accents.
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile | 36 | ||||
-rw-r--r-- | openrussian-completion.bash | 21 | ||||
-rwxr-xr-x | update-sqlite3 | 15 |
4 files changed, 58 insertions, 16 deletions
@@ -1,3 +1,3 @@ +openrussian openrussian-sql.zip -openrussian.sql openrussian-sqlite3.db diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8743043 --- /dev/null +++ b/Makefile @@ -0,0 +1,36 @@ +PREFIX ?= /usr/local + +LUA ?= lua5.2 +LUAC ?= luac5.2 + +COMPLETIONSDIR ?= $(shell pkg-config --variable=completionsdir bash-completion) + +all : openrussian openrussian-sqlite3.db + +openrussian : openrussian.lua + echo "#!$(shell which $(LUA))" >$@ + $(LUAC) -o - $< >>$@ + chmod a+x $@ + +# Marking the ZIP intermediate makes sure it is automatically deleted after +# generating the SQLITE database while we don't always re-download it. +# Effectively, it will behave like a temporary file. +.INTERMEDIATE: openrussian-sql.zip +openrussian-sql.zip: + wget -O $@ 'https://en.openrussian.org/downloads/openrussian-sql.zip' + +# NOTE: VACUUMing the database saves a few megabytes +openrussian-sqlite3.db : openrussian-sql.zip mysql2sqlite + unzip -p $< openrussian.sql | ./mysql2sqlite - | sqlite3 $@ + sqlite3 $@ VACUUM + +# NOTE: Installation of the Bash completions depends on the Debain bash-completion +# package being installed or something similar +install : openrussian openrussian-sqlite3.db openrussian-completion.bash + mkdir -p $(DESTDIR)$(PREFIX)/bin $(DESTDIR)$(PREFIX)/share/openrussian + install openrussian $(DESTDIR)$(PREFIX)/bin + cp openrussian-sqlite3.db $(DESTDIR)$(PREFIX)/share/openrussian + cp openrussian-completion.bash $(DESTDIR)$(COMPLETIONSDIR)/openrussian + +clean: + $(RM) -f openrussian openrussian-sql.zip openrussian-sqlite3.db diff --git a/openrussian-completion.bash b/openrussian-completion.bash new file mode 100644 index 0000000..0facd31 --- /dev/null +++ b/openrussian-completion.bash @@ -0,0 +1,21 @@ +#/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. +# +# FIXME: sqlite3 command-line tool does not provide a safe method for embedding +# strings, so we should either properly escape $2 or write an external Lua script. + +_openrussian_completions() +{ + SQL="SELECT bare FROM words WHERE bare LIKE \"$2%\"" + + # 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))/.. + + COMPREPLY=($(sqlite3 "$PREFIX/share/openrussian/openrussian-sqlite3.db" "$SQL")) +} + +complete -F _openrussian_completions openrussian diff --git a/update-sqlite3 b/update-sqlite3 deleted file mode 100755 index da86baf..0000000 --- a/update-sqlite3 +++ /dev/null @@ -1,15 +0,0 @@ -#!/bin/sh -# Downloads latest OpenRussian MySQL database dump and convert -# it to Sqlite3. -# -# NOTE: unzip cannot read from stdin yet. bsdtar could in principle, -# but does not allow extracting to stdout. - -TMPFILE=`mktemp` - -wget -O $TMPFILE https://en.openrussian.org/downloads/openrussian-sql.zip -unzip -p $TMPFILE openrussian.sql | \ -~/working-copies/mysql2sqlite/mysql2sqlite - | \ -sqlite3 openrussian-sqlite3.db - -rm $TMPFILE |