Skip to content

Commit

Permalink
UPDATED: f function. Proceeding to test
Browse files Browse the repository at this point in the history
  • Loading branch information
AleixMT committed Apr 26, 2024
1 parent 119bbaf commit 03449ff
Showing 1 changed file with 74 additions and 22 deletions.
96 changes: 74 additions & 22 deletions data/features/f/f.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,83 @@
#!/usr/bin/env bash

f()
{
if [ $# -eq 0 ]; then # No arguments given
find . 2>/dev/null
elif [ $# -eq 1 ]; then
if [ -f "$1" ]; then # Searches therm in a file
cat "$1"
elif [ -d "$1" ]; then # Searches files in directory
find "$1"
else
more ./* | grep "$1" # Searches therm in all files
fi
elif [ $# -gt 1 ]; then
local temp="$1"
while [ $# -gt 1 ]; do
if [ -f "$temp" ]; then # Searches therm in a file
more "$temp" | grep "$2"
elif [ -d "$temp" ]; then # Searches file in directory
if [ -n "$(find "$temp" -name "$2")" ]; then # Show files matching argument
more $(find "$temp" -name "$2")
default_ignored_dirs=("node_modules" ".git")
find_ignored_dirs_shell_command=""
# Since we expect f to be called as a binary in the PATH, we can expect the working directory to be the same that
# we are using f against
skip_defaults="false"
ignore_all_levels="true"
find_dir=""
custom_command=""

# Process arguments
while [ -n "$1" ]; do
case "$1" in
"--skip-defaults" | "-s")
skip_defaults="true"
;;
"--ignore-all" | "-i")
ignore_all_levels="true"
;;
"--literal" | "-l")
ignore_all_levels="false"
;;
"--dir" | "-d" | "--dir-exclude")
if [ $# -eq 1 ]; then
echo "ERROR: the \"$1\" option needs to be followed by another argument"
exit 3
fi
shift
if [ "${ignore_all_levels}" = "true" ]; then
find_ignored_dirs_shell_command+=" -o -path ./'*'/$1/'*'"
else
ls -lah "$temp" | grep "$2" # Show list of other matching files in elements of directory
find_ignored_dirs_shell_command+=" -o -path ./$1/'*'"
fi
else # Literal search in therm
echo "$temp" | grep "$2"
;;
"--custom" | "-c" | "--custom-command")
if [ $# -eq 1 ]; then
echo "ERROR: the \"$1\" option needs to be followed by another argument"
exit 4
fi
shift
custom_command="$1"
;;

*) # Error
if [ $# -eq 1 ]; then # If only one argument this is the last argument and means that is the search dir
find_dir="$1"
else
echo "ERROR: \"$1\" not recognized argument. Aborting..."
exit 1
fi
;;
esac
shift
done

# Remove the space and the -o from the beginning of the ignored dirs
find_ignored_dirs_shell_command="${find_ignored_dirs_shell_command:3}"

# Add the default ignored paths depending on the state of the flag
if [ "${skip_defaults}" = "false" ]; then
for dir in "${default_ignored_dirs[@]}"; do
if [ "${ignore_all_levels}" = "true" ]; then
find_ignored_dirs_shell_command+=" -o -path ./'*'/${dir}/'*'"
else
find_ignored_dirs_shell_command+=" -o -path ./${dir}/'*'"
fi
shift
done
fi

if [ -z "${find_dir}" ]; then
find_dir="."
else
if [ ! -d "${find_dir}" ]; then
echo "ERROR: \"${find_dir}\" is not a valid directory. Aborting..."
exit 2
fi
fi

find "${find_dir}" -type f -not \( ${find_ignored_dirs_shell_command} \) 2>/dev/null
}

0 comments on commit 03449ff

Please sign in to comment.