Skip to content

Commit

Permalink
Merge branch '160_F_feature' of https://github.com/AleixMT/Linux-Auto…
Browse files Browse the repository at this point in the history
…-Customizer into develop
  • Loading branch information
AleixMT committed Apr 27, 2024
2 parents 7bbaa06 + 6e7f688 commit 53d225d
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 41 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,4 @@ whoami

Linux-Auto-Customizer
test
src/AleixMT/
65 changes: 46 additions & 19 deletions data/features/FFunction/F.sh
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
#!/usr/bin/env bash

F() {
if [ $# -eq 0 ]; then # No arguments given
find / 2>/dev/null
previous_lines=5
following_lines=5
grep_query_string=""
read_from_stdin=""

# Process arguments
while [ -n "$1" ]; do
case "$1" in
"-A")
if [ $# -eq 1 ]; then
echo "ERROR: the \"$1\" option needs to be followed by another argument"
exit 3
fi
shift
previous_lines=$1
;;
"-B")
if [ $# -eq 1 ]; then
echo "ERROR: the \"$1\" option needs to be followed by another argument"
exit 3
fi
shift
following_lines=$1
;;
"-")
read_from_stdin="true"
;;
*) # Error
if [ $# -eq 1 ]; then # If only one argument this is the last argument and means that is the search dir
grep_query_string="$1"
else
echo "ERROR: \"$1\" not recognized argument. Aborting..."
exit 1
fi
;;
esac
shift
done

if [ "${read_from_stdin}" = "true" ]; then
# Process stdin
while IFS= read -r line; do
grep -hnI -B ${following_lines} -A ${previous_lines} --color='auto' "${grep_query_string}" < "${line}" 2>/dev/null
done
else
if [ -d "$1" ]; then
first_argument="$1"
shift
else
first_argument="."
fi
IFS=$'\n'
while [ -n "$1" ]; do
for filename in $(find "${first_argument}" -type f -not -path '*/\.git/*' 2>/dev/null); do
local result="$(grep "$1" < "${filename}" 2>/dev/null)"
if [ -n "$(echo "${result}")" ]; then
echo
echo -e "\e[0;33m${filename}\e[0m"
grep -hnI -B 5 -A 5 --color='auto' "$1" < "${filename}" 2>/dev/null
fi
done
shift
while IFS= read -r line; do
grep -hnI -B ${following_lines} -A ${previous_lines} --color='auto' "${grep_query_string}" < "${line}" 2>/dev/null
done
fi
}
93 changes: 71 additions & 22 deletions data/features/f/f.sh
Original file line number Diff line number Diff line change
@@ -1,31 +1,80 @@
#!/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=""
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") # TODO: Unluckily, I didn't make it work...
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

# 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

# Set current directory if query directory not present
if [ -z "${find_dir}" ]; then
# 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
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 \( -type d ${find_ignored_dirs_shell_command} \) 2> /dev/null
}

0 comments on commit 53d225d

Please sign in to comment.