-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '160_F_feature' of https://github.com/AleixMT/Linux-Auto…
…-Customizer into develop
- Loading branch information
Showing
3 changed files
with
118 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,3 +73,4 @@ whoami | |
|
||
Linux-Auto-Customizer | ||
test | ||
src/AleixMT/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |