Add no color option (can be set as default variable).

master
l3n 2019-12-30 19:45:02 +01:00
parent c3d275a714
commit b26e703002
2 changed files with 46 additions and 15 deletions

View File

@ -23,6 +23,8 @@
To get help use flag ~-h~.
~-f~ option (fuzzy matching) helps if no results were found.
* Dependencies
This script uses following tools:
@ -31,7 +33,7 @@
- recode
- sed
* Installation
* Installation
First you have to register at pons.com [[https://login.pons.com/login?return_to=https%253A%252F%252Fen.pons.com%252Fopen_dict%252Fpublic_api][api registration form]].
@ -44,8 +46,9 @@
~PONS_LANG~ to specify in which language is the word you are looking
translation for).
Make your script executable and crate a symlink to it in your execution
path.
Make your script executable and crate a symlink to it in your execution path.
Simplier solution is to just a create ~alias~ in your ~.bashrc/.zshrc~ file
(~alias trans="<path-to-script>/trans.sh"~)
* How it works
@ -62,3 +65,11 @@
I will rewrite this application in haskell once I found more time. Even before
that I'll implement some error handling.
** to do
- error handling
- rewrite into haskell
- ...
** done
- no color option

View File

@ -54,7 +54,6 @@ pons_filter() {
# PONS_FILTER="$PONS_FILTER | select( .lang == \"$PONS_LANG\")"
# fi
if [ ! -z $PONS_RAW -a "$PONS_RAW" == "1" ] ; then
# echo "Raw json output."
PONS_FILTER="[$PONS_FILTER ]"
else
if [ $PONS_HITS -gt 0 ] ; then
@ -63,17 +62,36 @@ pons_filter() {
PONS_FILTER="[$PONS_FILTER | {\"language_p\": .lang, \"hits_p\": [.hits[] | .roms | .[] | {\"headword_p\": .headword, \"wordclass_p\": .wordclass, \"arabs_p\": [ .arabs | .[] | {\"header_p\": .header, \"translations_p\": [.translations[] | {\"source_p\": .source, \"target_p\": .target }] } ] }] }]"
fi
fi
#echo $PONS_FILTER
}
pons_tags() {
source_t="$1"
#echo "$source_t\n"
source_t=$( recode html..latin1 <<< $source_t )
old_color="$2"
spn_color="$3"
source_t=$( sed -e "s/<span[^>]*>/$(tput setaf ${spn_color})/g" -e "s/<strong[^>]*>/$(tput bold)/g" -e "s/<acronym[^>]*>/$(tput dim)/g" -re "s/(<\/acronym>|<\/strong>|<\/span>)+/$(tput sgr0)$(tput setaf ${old_color})/g" <<< $source_t )
printf " $(tput setaf ${old_color})%b$(tput sgr0)" "$source_t"
if [ -z $PONS_NOCOLOR ] ; then
source_t=$( sed -e "s/<span[^>]*>/$(tput setaf ${spn_color})/g" -e "s/<strong[^>]*>/$(tput bold)/g" -e "s/<acronym[^>]*>/$(tput dim)/g" -re "s/(<\/acronym>|<\/strong>|<\/span>)+/$(tput sgr0)$(tput setaf ${old_color})/g" <<< $source_t )
printf " $(tput setaf ${old_color})%b$(tput sgr0)" "$source_t"
else
source_t=$( sed -e "s/<span[^>]*>//g" -e "s/<strong[^>]*>/$(tput bold)/g" -e "s/<acronym[^>]*>/$(tput dim)/g" -re "s/(<\/acronym>|<\/strong>|<\/span>)+/$(tput sgr0)/g" <<< $source_t )
printf " %b" "$source_t"
fi
}
pons_prettyfy_header() {
if [ -z $PONS_NOCOLOR ] ; then
hd=$(echo $hd | sed 's/<span.*>\(.*\)<\/span>/\\033\[0;34m\1\\033\[0;32m/g' | sed 's/<strong.*>\(.*\)<\/strong>/\\033\[1;32m\1\\033\[0;32m/g')
else
hd=$(echo -e $hd | sed 's/<span.*>\(.*\)<\/span>/\1/g' | sed 's/<strong.*>\(.*\)<\/strong>/\$\(tput bold\)\1\$\(tput sgr0\)/g')
fi
}
pons_print_headword() {
if [ -z $PONS_NOCOLOR ] ; then
echo -e "\033[1;36m$hw\033[0m\t$cur_lang\t\033[0;34m$cw\033[0m"
else
echo -e "$(tput bold)$hw$(tput dim)\t$cur_lang\t$(tput sgr0)$cw"
fi
}
# 1. get language as variable and loop through it
@ -94,11 +112,14 @@ pons_parse() {
sub_hit=$(jq -r ".hits_p | .[$hit_key]" <<< $sub_lang);
hw=$(jq -r '"\(.headword_p)"' <<< $sub_hit ) #| sed -e 's/^"//' -e 's/"$//')
cw=$(jq -r '"\(.wordclass_p)"' <<< $sub_hit)
echo -e "\033[1;36m$hw\033[0m\t$cur_lang\t\033[0;34m$cw\033[0m"
#echo -e "\033[1;36m$hw\033[0m\t$cur_lang\t\033[0;34m$cw\033[0m"
pons_print_headword
PONS_COUNT=$((PONS_COUNT + 1))
for ara_key in $(jq ".arabs_p | keys | .[]" <<< $sub_hit); do
sub_ara=$(jq ".arabs_p | .[$ara_key]" <<< $sub_hit)
hd=$(jq -r " .header_p" <<< $sub_ara | sed 's/<span.*>\(.*\)<\/span>/\\033\[0;34m\1\\033\[0;32m/g' | sed 's/<strong.*>\(.*\)<\/strong>/\\033\[1;32m\1\\033\[0;32m/g')
# hd=$(jq -r " .header_p" <<< $sub_ara | sed 's/<span.*>\(.*\)<\/span>/\\033\[0;34m\1\\033\[0;32m/g' | sed 's/<strong.*>\(.*\)<\/strong>/\\033\[1;32m\1\\033\[0;32m/g')
hd=$(jq -r " .header_p" <<< $sub_ara )
pons_prettyfy_header
if [ ! -z "$hd" ] ; then
echo -e " $hd"
fi
@ -174,6 +195,8 @@ pons_help () {
echo -e "\t-r :: Raw; Return raw result from pons."
# echo -e "\t-t [type] :: Type; Return results in this type, txt by default (json, csv)." # long term plan
echo -e "\t-c :: Count; Count number of results."
echo -e "\t-n [num]:: Limit number of results."
echo -e "\t-p :: Show plain text without colors (black&white)."
}
pons_options () {
@ -188,7 +211,7 @@ pons_options () {
}
# FLAGS SETUP
while getopts "fl:d:n:t:rhoec" option
while getopts "fl:d:n:t:rhoecp" option
do
case "${option}"
in
@ -202,6 +225,7 @@ do
o) PONS_HELP=2;;
c) PONS_COUNT=1;;
e) PONS_REF=1;;
p) PONS_NOCOLOR=1;;
esac
done
shift $((OPTIND -1))
@ -209,8 +233,6 @@ PONS_WORD=$1
if [ "$PONS_HELP" == "1" ] ; then
pons_help
#return 0;
elif [ "$PONS_HELP" == "2" ] ; then
pons_options
elif [ "$PONS_RAW" == "1" ] ; then
@ -224,8 +246,6 @@ else
pons_request
pons_filter
PONS_JSON=$(echo $PONS_JSON | jq "$PONS_FILTER")
#echo -e $PONS_JSON | jq .
# pons_parse
if [ "$PONS_HITS" -gt 0 ] ; then
pons_parse_translate
else