Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added long options and bash completion #199

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ to process and return results, and `recode` to unescape html.
cURL **tuxi** to your **$PATH** and give execute permissions.

```sh
$ sudo curl -sL "https://raw.githubusercontent.com/Bugswriter/tuxi/main/tuxi" -o /usr/local/bin/tuxi
$ sudo chmod +x /usr/local/bin/tuxi
sudo curl -sL "https://raw.githubusercontent.com/Bugswriter/tuxi/main/tuxi" -o /usr/local/bin/tuxi
sudo chmod +x /usr/local/bin/tuxi
```
> To update, just do `curl` again, no need to `chmod` anymore.
> To uninstall, simply remove `tuxi` from your **$PATH**, for example `sudo rm -f /usr/local/bin/tuxi`.

### Make
```sh
$ git clone https://github.com/Bugswriter/tuxi.git && cd tuxi/
$ sudo make install
git clone https://github.com/Bugswriter/tuxi.git && cd tuxi/
sudo make install
```
> To update, just `git pull` on your local tuxi repository and reinstall with `sudo make install`.
> To uninstall, simply run `sudo make uninstall`.
Expand All @@ -49,7 +49,7 @@ Tuxi is available as the [`tuxi-git`](https://aur.archlinux.org/packages/tuxi-gi
Note: the AUR package is not maintaned by the project maintainer, nor contribuitor, it is often outdated and it's recommended that you just clone this project.

```sh
$ yay -S tuxi-git
yay -S tuxi-git
```

## Usage
Expand All @@ -76,52 +76,52 @@ Usage: tuxi [options] query
OR: query source | tuxi [options]

Options:
-h Show this help message and exit.
-v Print tuxi version info and exit.
-h, --help Show this help message and exit.
-v, --version Print tuxi version info and exit.

-r Raw search results.
(no pretty output, no colors)
-r, --raw Raw search results.
(no pretty output, no colors)

-q Only output search results.
(silences "Did you mean?", greeting, usage)
-q, --quiet Only output search results.
(silences "Did you mean?", greeting, usage)

-a Prints all valid answers.
-a, --all Prints all valid answers.

-u Prints out the top handful of URLs for your search query
(this is automatically printed out if tuxi can't find you an answer)
-u, --top-links Prints out the top handful of URLs for your search query
(this is automatically printed out if tuxi can't find you an answer)

-b Tries to select the best answer based on keywords at the start and end of your query.
(experimental - eg: define WORD, SONG lyrics, PERSON quotes, weather CITY, FILM cast)
-b, --best Tries to select the best answer based on keywords at the start and end of your query.
(experimental - eg: define WORD, SONG lyrics, PERSON quotes, weather CITY, FILM cast)

-t Pick answers to test.
(you can specify multiple answers using tuxi_NAME in your query)
-t, --test Pick answers to test.
(you can specify multiple answers using tuxi_NAME in your query)

-l use LANG_[lang] in your query to override the language used
(eg: tuxi -l LANG_en_US my search query)
-l, --lang use LANG_[lang] in your query to override the language used
(eg: tuxi -l LANG_en_US my search query)

tuxi supports the following environment variables:
TUXI_LANG=[lang] sets default search language (eg: TUXI_LANG='en_US')

TUXI_DELAY=[int] if you find more than one answer is being printed (and you're not using -a)
increase this number by a little (you want it to be as low as possible)
default value is 250 (eg: TUXI_DELAY=270)
TUXI_DELAY=[int] if you find more than one answer is being printed (and you're not using -a)
increase this number by a little (you want it to be as low as possible)
default value is 250 (eg: TUXI_DELAY=270)

developer flags:
-d prints debug info along with results
-s saves HTML for this query to /home/dave/.cache/tuxi/[date]-[query].html
-d, --debug prints debug info along with results
-s, --save-html saves HTML for this query to /home/dave/.cache/tuxi/[date]-[query].html

-c use most recent cached result and query
this can be combined with -t flag to more quickly test for different answers
-c, --use-cache use most recent cached result and query
this can be combined with -t flag to more quickly test for different answers

-p disable pipe support (it can break some scripts including our own test script)
-p, --disable-pipe disable pipe support (it can break some scripts including our own test script)

Report bugs at https://github.com/Bugswriter/tuxi/issues
```

## Features

**Easily change query language**
Line [8](./tuxi#L8) in `tuxi` contains the language variable which can be changed according the user's preference. However, tuxi will use the system default langauge if none is set.
Line [86](./tuxi#L86) in `tuxi` contains the language variable which can be changed according the user's preference. However, tuxi will use the system default langauge if none is set.

**Gives corrections**
```sh
Expand Down
53 changes: 53 additions & 0 deletions completions/tuxi_bash_completion
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# bash completion for tuxi

_tuxi_parameterless() {
local param
# parameter-less completion
# param= COMMAND parameters
param="$dashoptions"
COMPREPLY=($(compgen -W "$param" -- $cur))

}

_tuxi() {
local cur dashoptions prev params

COMPREPLY=()
cur=$(_get_cword)
prev=${COMP_WORDS[COMP_CWORD - 1]}

# available options
dashoptions='--help --version --raw --quiet --all --top-links --best --test --lang --debug --save-html --use-cache --disable-pipe'

# language word list
lang_codes='LANG_de LANG_en LANG_es LANG_es-419 LANG_fr LANG_hr LANG_it LANG_nl LANG_pl LANG_pt-BR LANG_pt-PT LANG_vi LANG_tr LANG_ru LANG_ar LANG_th LANG_ko LANG_zh-CN LANG_zh-TW LANG_ja LANG_ach LANG_af LANG_ak LANG_az LANG_ban LANG_su LANG_ceb LANG_xx-bork LANG_bs LANG_br LANG_ca LANG_cs LANG_sn LANG_co LANG_cy LANG_da LANG_yo LANG_et LANG_xx-elmer LANG_eo LANG_eu LANG_ee LANG_tl LANG_fil LANG_fo LANG_fy LANG_gaa LANG_ga LANG_gd LANG_gl LANG_gn LANG_xx-hacker LANG_ht LANG_ha LANG_haw LANG_bem LANG_ig LANG_rn LANG_id LANG_ia LANG_xh LANG_zu LANG_is LANG_jw LANG_rw LANG_sw LANG_tlh LANG_kg LANG_mfe LANG_kri LANG_la LANG_lv LANG_to LANG_lt LANG_ln LANG_loz LANG_lua LANG_lg LANG_hu LANG_mg LANG_mt LANG_ms LANG_pcm LANG_no LANG_nn LANG_nso LANG_ny LANG_uz LANG_oc LANG_om LANG_xx-pirate LANG_ro LANG_rm LANG_qu LANG_nyn LANG_crs LANG_sq LANG_sk LANG_sl LANG_so LANG_st LANG_sr-ME LANG_sr-Latn LANG_fi LANG_sv LANG_mi LANG_tn LANG_tum LANG_tk LANG_tw LANG_wo LANG_el LANG_be LANG_bg LANG_ky LANG_kk LANG_mk LANG_mn LANG_sr LANG_tt LANG_tg LANG_uk LANG_ka LANG_hy LANG_yi LANG_iw LANG_ug LANG_ur LANG_ps LANG_sd LANG_fa LANG_ckb LANG_ti LANG_am LANG_ne LANG_mr LANG_hi LANG_bn LANG_pa LANG_gu LANG_or LANG_ta LANG_te LANG_kn LANG_ml LANG_si LANG_lo LANG_my LANG_km LANG_chr'

case "$prev" in
tuxi)
case "$cur" in
--*)
# parameter completion
COMPREPLY=($(compgen -W "$dashoptions" -- $cur))
;;
*)
# no parameter given
_tuxi_parameterless
;;

esac
;;
-a | --all | -b | --best | -c | --use-cache | -d | --debug | -q | --quiet | -r | --raw | -s | --save-html | -t | --test | -u | --top-links)
COMPREPLY=($(compgen -W "$dashoptions" -- $cur))
;;
-l | --lang)
COMPREPLY=($(compgen -W "$lang_codes" -- $cur))
;;
-h | --help | -v | --version)
# standalone parameters
return 0

;;
esac
}

complete -F _tuxi tuxi
110 changes: 66 additions & 44 deletions tuxi
Original file line number Diff line number Diff line change
Expand Up @@ -127,39 +127,60 @@ help_text() {
done <<-EOF
\rUsage: tuxi [OPTS] [QUERY]

\r -h - Display this help information.
\r -v - Print Tuxi version information.
\r -a - Print all valid answers.
\r -b - Auto-determine the best answer.
\r -c - Use cache from previous QUERY.
\r -d - Include debugging information.
\r -l LANG_[STR] - Override LANG with STR.
\r -p - Disable pipe support.
\r -q - Output only query results.
\r -r - Output query in raw format.
\r -s [FILE] - Save query result HTML to FILE.
\r -t - Choose answers to test.
\r -u - Print top bulk of query URLs.
\r -h, --help - Display this help information.
\r -v, --version - Print Tuxi version information.
\r -a, --all - Print all valid answers.
\r -b, --best - Auto-determine the best answer.
\r -c, --use-cache - Use cache from previous QUERY.
\r -d, --debug - Include debugging information.
\r -l, --lang LANG_[STR] - Override LANG with STR.
\r -p, --disable-pipe - Disable pipe support.
\r -q, --quiet - Output only query results.
\r -r, --raw - Output query in raw format.
\r -s, --save-html [FILE] - Save query result HTML to FILE.
\r -t, --test - Choose answers to test.
\r -u, --top-links - Print top bulk of query URLs.
EOF
}

#############################
##### Getopts #####
#############################

# -r : raw output
# -v : version info
# -h : help
# -q : silences greeting and did you mean
# -a : print all answers
# -b : best match
# -t : specify answer type
# -l : specify language using LANG_[code] - eg LANG_en_US
# -d : print debug info
# -s : save google HTML response
# -c : use most recent cached results
# -p : disable pipe support (needed for test script)
# -u : also print out the top links
# -r, --raw : raw output
# -v, --version : version info
# -h, --help : help
# -q, --quiet : silences greeting and did you mean
# -a, --all : print all answers
# -b, --best : best match
# -t, --test : specify answer type
# -l, --lang : specify language using LANG_[code] - eg LANG_en_US
# -d, --debug : print debug info
# -s, --save-html : save google HTML response
# -c, --use-cache : use most recent cached results
# -p, --disable-pipe : disable pipe support (needed for test script)
# -u, --top-links : also print out the top links

# loops through arguments and changes sets long options to short
for arg in "$@"; do
shift
case "$arg" in
"--help") set -- "$@" "-h" ;;
"--version") set -- "$@" "-v" ;;
"--quiet") set -- "$@" "-q" ;;
"--all") set -- "$@" "-a" ;;
"--top-links") set -- "$@" "-u" ;;
"--best") set -- "$@" "-b" ;;
"--test") set -- "$@" "-t" ;;
"--lang") set -- "$@" "-l" ;;
"--debug") set -- "$@" "-d" ;;
"--save-html") set -- "$@" "-s" ;;
"--use-cache") set -- "$@" "-c" ;;
"--disable-pipe") set -- "$@" "-p" ;;
*) set -- "$@" "$arg" ;;
esac
done

while getopts "rvhqabtldscpu" OPT; do
case $OPT in
r)
Expand Down Expand Up @@ -258,15 +279,15 @@ error_msg() {

# Checks for required executable files in PATH.
check_deps() {
dep_err=0
dep_err=0
for dep in "$@"; do
if ! command -v "$dep" 1> /dev/null 2>&1; then
if ! command -v "$dep" 1>/dev/null 2>&1; then
error_msg "\"$dep\" not found!"
dep_err=$((dep_err + 1))
dep_err=$((dep_err + 1))
fi
done

[ $dep_err -gt 0 ] && exit 2
[ $dep_err -gt 0 ] && exit 2
}

# Dependencies
Expand Down Expand Up @@ -319,8 +340,8 @@ if $pick_search; then
for pick_words in $(printf '%b\n' "$query" | grep 'tuxi_' | sed 's/tuxi_//g'); do
for check_pick_words in $(printf '%b\n' "$snippet_check"); do
if [ "$check_pick_words" = "$pick_words" ]; then
[ -z "$priority" ] && priority="$(printf '%s\n' "$pick_words")" \
|| priority="$(printf '%b\n%s\n' "$priority" "$pick_words")"
[ -z "$priority" ] && priority="$(printf '%s\n' "$pick_words")" ||
priority="$(printf '%b\n%s\n' "$priority" "$pick_words")"
matched=true
fi
done
Expand Down Expand Up @@ -487,8 +508,8 @@ a_define() {
dfn_start=true
;;
*)
$dfn_sim_op && printf '\t%s\n' "$dfn_foo" \
|| printf '\t%b%s%b\n' "$B" "$dfn_foo" "$N"
$dfn_sim_op && printf '\t%s\n' "$dfn_foo" ||
printf '\t%b%s%b\n' "$B" "$dfn_foo" "$N"
;;
esac
fi
Expand Down Expand Up @@ -592,7 +613,8 @@ a_currency() {

# Translate ( eg: Vais para cascais? em ingles ) //credit @Genghius
a_trans() {
echo "$google_html" | pup 'pre.XcVN5d json{}' | jq -r '[.[] | .children | .[] | select(.class!="BCGytf")][1] | .text' | sed 's/null//g' | recode html..ISO-8859-1
echo "$google_html" | pup '#tw-target-text span.Y2IQFc text{}' | sed 's/null//g' | recode html..ISO-8859-1
echo "$google_html" | pup '#tw-target-rmn span.Y2IQFc text{}' | sed 's/null//g' | recode html..ISO-8859-1
}

# Knowledge Graph - right ( eg: the office ) //credit @Bugswriter
Expand Down Expand Up @@ -697,17 +719,17 @@ a_sport_fixture() {

# Learn to pronounce ( eg: pronounce linux ) //credit @sdushantha
a_pronounce() {
echo "$google_html" | pup 'div.fQ02Rb.eDzgme span.seLqNc text{}' | paste -s -d ' ' | sed 's/\s/∙/g'
echo "$google_html" | pup 'div.TQ7enb span.RES9jf text{}' | paste -s -d ' ' | sed 's/\s/∙/g'
}
# Album track lists ( eg: noisia outer edges tracklist )
a_tracklist() {
tracklist="$(echo "$google_html" | pup 'div.mR2gOd json{}')"
if [ -n "$tracklist" ]; then
tracklist_songs="$(printf '%s\n' "$tracklist" | jq -r '.[0].children[0].children[].children[0].children[].children[0].children[0].children[1].children[1].children[0].children[0].text' \
| sed '/^null$/d' | recode html..ISO-8859-1)"
[ -z "$tracklist_songs" ] && tracklist_songs="$(printf '%s\n' "$tracklist" \
| jq -r '.[0].children[0].children[].children[0].children[].children[0].children[0].children[1].children[0].children[0].children[0].text' \
| sed '/^null$/d' | recode html..ISO-8859-1)"
tracklist_songs="$(printf '%s\n' "$tracklist" | jq -r '.[0].children[0].children[].children[0].children[].children[0].children[0].children[1].children[1].children[0].children[0].text' |
sed '/^null$/d' | recode html..ISO-8859-1)"
[ -z "$tracklist_songs" ] && tracklist_songs="$(printf '%s\n' "$tracklist" |
jq -r '.[0].children[0].children[].children[0].children[].children[0].children[0].children[1].children[0].children[0].children[0].text' |
sed '/^null$/d' | recode html..ISO-8859-1)"
[ -z "$tracklist_songs" ] && exit
tracklist_links="$(printf '%s\n' "$tracklist" | jq -r '.[1].children[0].children[0].children[].href' | sed '/^null$/d')"
printf '%s\n' "$tracklist_links" | grep -q 'youtube.com' || unset tracklist_links
Expand Down Expand Up @@ -837,9 +859,9 @@ test_answers() {

# this finds and prints out the top links for your query
top_links() {
url_list="$(echo "$google_html" | pup 'div.tF2Cxc json{}' \
| jq -r '.[].children[0].children[0].children[1].text, .[].children[0].children[0].children[1].children[0].text, .[].children[0].children[0].href' \
| sed '/^null$/d' | recode html..ISO-8859-1)"
url_list="$(echo "$google_html" | pup 'div.tF2Cxc json{}' |
jq -r '.[].children[0].children[0].children[1].text, .[].children[0].children[0].children[1].children[0].text, .[].children[0].children[0].href' |
sed '/^null$/d' | recode html..ISO-8859-1)"
if [ -n "$url_list" ]; then
if [ -n "$1" ]; then
info_msg "Sorry about that! Perhaps one of these links may be of use?"
Expand Down