diff --git a/README.md b/README.md index 2c230e0..a770bc2 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,17 @@ LANG=de cli-tips # Output: Verwenden Sie 'uniq', um doppelte Zeilen aus einer Datei zu entfernen ``` +### Use the `--about` Flag + +To display a random tip containing a specific keyword, use the `--about` option: + +```bash +cli-tips --about=git +# Output: Use 'git status' to check the status of your git repository +``` + +If no tips contain the specified keyword, no tip will be output. + ### Available Languages Here is a list of all available languages: diff --git a/cli-tips.sh b/cli-tips.sh index dd82781..e077900 100755 --- a/cli-tips.sh +++ b/cli-tips.sh @@ -20,9 +20,11 @@ if [ ! -n "$TIPS_FOLDER" ]; then TIPS_FOLDER="$prefix/share/cli-tips" fi +SYSTEM_LANGUAGE="$(echo "$LANG" | cut -d'_' -f1)" + # Default language is based on the user's environment if [ ! -n "$TIPS_LANGUAGE" ]; then - LANGUAGE="$(echo "$LANG" | cut -d'_' -f1)" + LANGUAGE="$SYSTEM_LANGUAGE" else LANGUAGE="$TIPS_LANGUAGE" fi @@ -33,6 +35,7 @@ show_help() { echo -e "\e[1mOptions:\e[0m" echo -e " \e[1;34m-h, --help\e[0m Show this help message and exit" echo -e " \e[1;34m-l, --lang, --language\e[0m Specify the language for tips" + echo -e " \e[1;34m--about\e[0m Specify a keyword to filter tips" echo echo -e "\e[1mAvailable Languages:\e[0m" printf " " @@ -44,6 +47,7 @@ show_help() { echo -e "\e[1mExamples:\e[0m" echo -e " $(basename "$0") \e[1;34m--language\e[0m en" echo -e " $(basename "$0") \e[1;34m--language\e[0m es" + echo -e " $(basename "$0") \e[1;34m--about\e[0m git" echo echo echo -e "\e[1mGitHub:\e[0m \e[0;30mhttps://github.com/cli-stuff/cli-tips\e[0m" @@ -54,11 +58,19 @@ while [[ "$#" -gt 0 ]]; do case $1 in -l | --language | --lang) LANGUAGE="$2" + if [[ ! -n "$LANGUAGE" ]]; then + echo "Error: No language specified" + exit 1 + fi shift ;; --language=* | --lang=*) LANGUAGE="${1#*=}" ;; + --about) + KEYWORD="$2" + shift + ;; -h | --help) show_help exit 0 @@ -71,6 +83,11 @@ while [[ "$#" -gt 0 ]]; do shift done +if [[ ! -n "$LANGUAGE" ]]; then + echo "Error: No language specified" + exit 1 +fi + # Find the localized tips file if [ -f "$TIPS_FOLDER/${LANGUAGE}.txt" ]; then localized_file="$TIPS_FOLDER/${LANGUAGE}.txt" @@ -81,6 +98,22 @@ fi # Read tips from the file into an array mapfile -t tips <"$localized_file" +# Filter tips based on the specified keyword +if [ -n "$KEYWORD" ]; then + filtered_tips=() + for tip in "${tips[@]}"; do + if [[ "$tip" == *"$KEYWORD"* ]]; then + filtered_tips+=("$tip") + fi + done + tips=("${filtered_tips[@]}") +fi + +# If there are no matching tips, exit +if [ ${#tips[@]} -eq 0 ]; then + exit 0 +fi + # Generate random index tip_index=$((RANDOM % ${#tips[@]}))