esconj

Conjugates Spanish verbs in the terminal
git clone git://mattcarlson.org/repos/esconj.git
Log | Files | Refs | README

commit 1aa146759428e938e92300d89a561cda99f2cac9
parent 859f1358dffea781f044aaaa88e6efce8cd3412b
Author: Matthew Carlson <matt@mcarlson.xyz>
Date:   Sun, 17 Jul 2022 03:16:54 -0400

made some slight improvements

Diffstat:
Mesconj | 218+++++++++++++++++++++++++++++++++++++++++--------------------------------------
Minstall.sh | 14++++++++------
2 files changed, 120 insertions(+), 112 deletions(-)

diff --git a/esconj b/esconj @@ -1,10 +1,10 @@ #!/bin/bash # Title : esconj # Description : Conjugates Spanish verbs in the terminal. -# Author : Matthew Carlson <matt@mcarlson.xyz> +# Author : Matthew Carlson <matt@mcarlson.xyz> # Date : 2020-09-03 # Version : 0.1 -# Usage : esconj [options] +# Usage : esconj [options] # Notes : Should handle common verbs. No guarantees for obscure verbs. # Copyright (C) 2020 Matthew Carlson <matt@mcarlson.xyz> @@ -22,132 +22,138 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>. -######################### -# Check if verb is valid. -# Arguments: -# Verb to check -######################### +NL=' +' +# file from https://github.com/ghidinelli/fred-jehle-spanish-verbs +VERBS='/usr/share/esconj/verbs.csv' +WORDS="$(cat ${VERBS})" + check() { - local verb - verb="$(echo $1 | tr '[:upper:]' '[:lower:]')" - - [[ ! "${verb}" =~ [a-z]*(ar|er|ir)(se)*$ ]] \ - && printf "Verb is in incorrect format.\nVerb must be a word ending in -ar, -er, -ir (with -se in the case of reflexives)." \ - && exit - - local suffix - suffix="${verb: -2}" - - case "${suffix}" in - se) - search_dict "${verb}" - [[ $? -eq 1 ]] \ - && conjugate "${verb}" \ - || check "${verb::-2}" - ;; - ar|er|ir) - search_dict "${verb}" - conjugate "${verb}" - ;; - esac + verb="$(printf '%s\n' "${1}" | tr '[:upper:]' '[:lower:]')" + + # check if valid spanish verb + if ! printf '%s\n' "${verb}" | grep -q '^[a-z]*\(ar\|er\|ir\)\{1\}\(se\)\?$'; then + printf '%s\n' "Verb is in incorrect format.${NL}Verb must be a word ending in -ar, -er, -ir (with -se in the case of reflexives)." + exit 1 + fi + + # handle suffixes + suffix="${verb#"${verb%??}"}" + case "${suffix}" in + # -se means reflexive + se) + # check if reflexive version of verb exists + if search_dict "${verb}"; then + conjugate "${verb}" + # if not, check without -se + else + check "${verb%??}" + fi + ;; + # standard -ar, -er, -ir + ar|er|ir) + search_dict "${verb}" + conjugate "${verb}" + ;; + esac } -read_csv() { rows="$(awk -v verb=$1 -F '","|^"|"$' '{sub("^\"","")} $1==verb' verbs.csv)" ; } - conjugate() { - declare -A conjugations - declare -a tenses + # declare arrays which will store conjugation table info + declare -A conjugations + declare -a tenses - read_csv "$1" + # get all rows containing verb + rows="$(awk -v verb="${1}" -F '","|^"|"$' '{sub("^\"","")} $1==verb' "${VERBS}")" - for i in {1..18}; do - tenses[$i]=$(for j in 5 8 9 10 11 12 13; do - conjugations[$i,$j]="$(echo "${rows}" | awk -v col=$j -F '","|^"|"$' '{print $col}' | head -$i | tail +$i)" - [[ -n "${conjugations[$i,$j]}" ]] && echo "${conjugations[$i,$j]}" || echo "-" - done) - done + # store info in arrays + for i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18; do + tenses[${i}]=$(for j in 5 8 9 10 11 12 13; do + conjugations[${i},${j}]="$(printf '%s\n' "${rows}" | awk -v col=${j} -F '","|^"|"$' '{print $col}' | head -${i} | tail +${i})" + [ -n "${conjugations[${i},${j}]}" ] && printf '%s\n' "${conjugations[${i},${j}]}" || printf '%s\n' '-' + done) + done - bold=$(tput bold) - normal=$(tput sgr0) - blue=$(tput setaf 4) + print_mood_or_aspect 'Indicativo' + print_tenses 1 5 - print_tense "Indicativo" - print_mood 1 5 + print_mood_or_aspect 'Subjuntivo' + print_tenses 11 3 - print_tense "Subjuntivo" - print_mood 11 3 + print_mood_or_aspect 'Aspecto Perfectivo' + print_tenses 6 5 - print_tense "Aspecto Perfectivo" - print_mood 6 5 + print_mood_or_aspect 'Aspecto Perfectivo de Subjuntivo' + print_tenses 14 3 - print_tense "Aspecto Perfectivo de Subjuntivo" - print_mood 14 3 - - print_tense "Imperativo (Afirmativo y Negativo)" - print_mood 17 2 + print_mood_or_aspect 'Imperativo (Afirmativo y Negativo)' + print_tenses 17 2 } -get_words() { words="$(awk -v col=$1 -F '","|^"|"$' '{sub("^\"","")} {print $col}' verbs.csv)" ; } - -print_mood() { - local tmp - tmp='' - - for i in "${tenses[@]:$1:$2}"; do - tmp=$(paste <(printf '%b\n' "$tmp") <(printf "$i") | column -s $'\t' -t) - done - - paste <(printf '\e[1;34m%s\e[m\n' '' \ - "yo" \ - "tu" \ - "él/ella/Ud." \ - "nosotros" \ - "vosotros" \ - "ellos/ellas/Uds.") \ - <(printf '%b\n' "$tmp") | column -s $'\t' -t - - printf '\n' +print_tenses() { + tmp='' + + # print tenses in neat columns + for i in "${tenses[@]:${1}:${2}}"; do + tmp=$(paste <(printf '%b\n' "${tmp}") <(printf '%s\n' "${i}") | column -s $'\t' -t) + done + + # print pronouns + paste <(printf '\e[1;32m%s\e[m\n' '' \ + 'yo' \ + 'tu' \ + 'él/ella/Ud.' \ + 'nosotros' \ + 'vosotros' \ + 'ellos/ellas/Uds.') \ + <(printf '%b\n' "${tmp}") | column -s $'\t' -t + + printf '\n' } -print_tense() { printf "${blue}${bold}$1\n" ; } +print_mood_or_aspect() { printf '\e[1;32m%s\n' "${1}" ; } search_dict() { - [[ $(grep -w "$1" <<< "${words}") ]] \ - && return 1 \ - || printf "Verb not found.\nIf you entered a reflexive verb, try without the -se ending." && exit + # grep csv for verb + # NOTE: also excludes the word 'er' + if ! printf '%s\n' "${WORDS}" | grep -w "${1}" | grep -qvw 'er'; then + printf '%s\n' "Verb not found.${NL}If you entered a reflexive verb, try it without the -se ending." + exit 1 + else + return 0 + fi } usage() { - printf "Usage: esconj [options]\n" - printf "Options: - -c, --conjugate Conjugate verb - -h, --help Print this message and exit\n" + printf '%s\n' "Usage: esconj [options]" + printf '%s\n' "Options: + -c, --conjugate Conjugate verb + -h, --help Print this message and exit" } main() { - [[ $# -eq 0 ]] && usage - - get_words - - for arg in "$@"; do - [[ "${arg:0:1}" = '-' ]] || continue - - case "$1" in - -c | --conjugate) - check "$2" - shift 2 - ;; - -h | --help) - usage - break - ;; - *) - echo "Invalid argument: $1" - usage - break - ;; - esac - done + [ ${#} -eq 0 ] && usage + + for arg in "${@}"; do + # check for dashed argument + [ "${arg%"${arg#?}"}" = '-' ] || continue + + case "${1}" in + -c|--conjugate) + check "${2}" + shift 2 + ;; + -h|--help) + usage + break + ;; + *) + printf '%s\n' "Invalid argument: ${1}" + usage + break + ;; + esac + done } -main "$@" +main "${@}" diff --git a/install.sh b/install.sh @@ -1,12 +1,14 @@ #!/bin/sh -[ "$EUID" -ne 0 ] && echo "Permission denied. Are you root?" && exit +[ "$(id -u)" -ne 0 ] && echo 'Permission denied. Are you root?' && exit 1 -DIR='/usr/share/esconj' +DIR='/usr/share/esconj/' PATH='/usr/bin/' -mkdir --parents "${DIR}" -echo "./verbs.csv -> ${DIR}/verbs.csv" && cp './verbs.csv' "${DIR}/verbs.csv" -echo "./esconj -> ${PATH}/esconj" && cp './esconj' "${PATH}/esconj" +mkdir -p "${DIR}" +echo "./verbs.csv -> ${DIR}/verbs.csv" +cp './verbs.csv' "${DIR}/verbs.csv" +echo "./esconj -> ${PATH}/esconj" +cp './esconj' "${PATH}/esconj" -[ $? -eq 0 ] && echo "Installation complete." +echo 'Installation complete.'