Skip to content
Merged

. #2

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
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
35 changes: 34 additions & 1 deletion cli-tips.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 " "
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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[@]}))

Expand Down