Skip to content

Instantly share code, notes, and snippets.

@Jacobboogiebear
Last active September 17, 2022 19:31
Show Gist options
  • Save Jacobboogiebear/bb2414761028853639c946905442f518 to your computer and use it in GitHub Desktop.
Save Jacobboogiebear/bb2414761028853639c946905442f518 to your computer and use it in GitHub Desktop.
A script to compile a simple google search tool for the terminal
#!/bin/bash
if which tput > /dev/null 2>&1 && [[ $(tput -T$TERM colors) -ge 8 ]] ; then
FANCY_TERMINAL=true
LICENSE_SCROLL=0
RENDERING_LICENSE=false
HELP_SCROLL=0
RENDERING_HELP=false
TERMINAL_WIDTH=$(tput cols)
TERMINAL_HEIGHT=$(tput lines)
else
FANCY_TERMINAL=false
fi
WSL_STATUS=false
REQUIRED_PACKAGES=""
if [[ -n "$IS_WSL" || -n "$WSL_DISTRO_NAME" ]] ; then
WSL_STATUS=true
fi
move_cursor() {
tput cup $2 $1
}
set_fg_color() {
tput setaf $1
}
set_bg_color() {
tput setab $1
}
render_box() {
printline=""
for (( i = 0 ; i < "$TERMINAL_WIDTH" ; i++ )) ; do
printline="$printline-"
done
set_fg_color 20
move_cursor 0 0
echo "$printline"
for (( i = 1 ; i < $(("$TERMINAL_HEIGHT" - 2)) ; i++ )) ; do
move_cursor 0 "$i"
echo "|"
move_cursor "$TERMINAL_WIDTH" "$i"
echo "|"
done
move_cursor 0 "$(("$TERMINAL_HEIGHT" - 2))"
echo $printline
}
render_help_header() {
set_fg_color 39
move_cursor "$(("$TERMINAL_WIDTH" / 2 - 23))" 1
echo "Created by"
set_fg_color 21
move_cursor "$(("$TERMINAL_WIDTH" / 2 - 12))" 1
echo "Jacob Allen Morris"
set_fg_color 39
move_cursor "$(("$TERMINAL_WIDTH" / 2 + 7))" 1
echo "under"
set_fg_color 160
move_cursor "$(("$TERMINAL_WIDTH" / 2 + 13))" 1
echo "MIT license"
set_fg_color 39
move_cursor "$(("$TERMINAL_WIDTH" / 2 - 17))" 2
echo "For help with command do -h <flag>"
}
render_license_header() {
move_cursor "$(("$TERMINAL_WIDTH" / 2 - 5))" 1
set_fg_color 160
echo "MIT License"
move_cursor "$(("$TERMINAL_WIDTH" / 2 - 18))" 2
echo "Copyright (c) 2022"
move_cursor "$(("$TERMINAL_WIDTH" / 2 + 1))" 2
set_fg_color 21
echo "Jacob Allen Morris"
}
render_license_scrolled() {
local license_text=(
"Permission is hereby granted, free of charge, to any person obtaining a copy"
"of this software and associated documentation files (the \"Software\"), to deal"
"in the Software without restriction, including without limitation the rights"
"to use, copy, modify, merge, publish, distribute, sublicense, and/or sell"
"copies of the Software, and to permit persons to whom the Software is"
"furnished to do so, subject to the following conditions:"
"The above copyright notice and this permission notice shall be included in all"
"copies or substantial portions of the Software."
"THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR"
"IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,"
"FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE"
"AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER"
"LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,"
"OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE"
"SOFTWARE."
)
set_fg_color 39
for i in "${!license_text[@]}";
do
if [ "$i" -lt $(("$TERMINAL_HEIGHT" - 5 + "$LICENSE_SCROLL")) ] && [ "$i" -gt $(("$LICENSE_SCROLL" - 1)) ] ; then
move_cursor $(("$TERMINAL_WIDTH" / 2 - ${#license_text[$i]} / 2 - ${#license_text[$i]} % 2)) $((3 + i - "$LICENSE_SCROLL"))
echo "${license_text[$i]}"
fi
done
}
render_help_scrolled() {
local helptext=(
"-i : compile/install the script/executable"
"-h <flag> : help page for script"
"-l : view license for this script"
"-f : disable fancy terminal settings"
)
set_fg_color 97
for i in "${!helptext[@]}";
do
if [ "$i" -lt $(("$TERMINAL_HEIGHT" - 5 + "$HELP_SCROLL")) ] && [ "$i" -gt $(("$HELP_SCROLL" - 1)) ] ; then
move_cursor $(("$TERMINAL_WIDTH" / 2 - ${#helptext[$i]} / 2 - ${#helptext[$i]} % 2)) $((3 + i - "$HELP_SCROLL"))
echo "${helptext[$i]}"
fi
done
}
render_help_fancy_scrolled() {
local helptext=(
"This flag will simply make the terminal application more basic"
"(Only use this if you must, I worked hard on this!)"
)
set_fg_color 97
for i in "${!helptext[@]}";
do
if [ "$i" -lt $(("$TERMINAL_HEIGHT" - 5 + "$HELP_SCROLL")) ] && [ "$i" -gt $(("$HELP_SCROLL" - 1)) ] ; then
move_cursor $(("$TERMINAL_WIDTH" / 2 - ${#helptext[$i]} / 2 - ${#helptext[$i]} % 2)) $((3 + i - "$HELP_SCROLL"))
echo "${helptext[$i]}"
fi
done
}
render_help_install_scrolled() {
local helptext=(
"This will download using Git, shc, and compile it then proceed to write and compile"
"script made for easily searching google through your terminal"
)
set_fg_color 97
for i in "${!helptext[@]}";
do
if [ "$i" -lt $(("$TERMINAL_HEIGHT" - 5 + "$HELP_SCROLL")) ] && [ "$i" -gt $(("$HELP_SCROLL" - 1)) ] ; then
move_cursor $(("$TERMINAL_WIDTH" / 2 - ${#helptext[$i]} / 2 - ${#helptext[$i]} % 2)) $((3 + i - "$HELP_SCROLL"))
echo "${helptext[$i]}"
fi
done
}
render_help_license_scrolled() {
local helptext=(
"This flag shows the license in it's entierty"
"(The MIT license to be exact)"
)
set_fg_color 97
for i in "${!helptext[@]}";
do
if [ "$i" -lt $(("$TERMINAL_HEIGHT" - 5 + "$HELP_SCROLL")) ] && [ "$i" -gt $(("$HELP_SCROLL" - 1)) ] ; then
move_cursor $(("$TERMINAL_WIDTH" / 2 - ${#helptext[$i]} / 2 - ${#helptext[$i]} % 2)) $((3 + i - "$HELP_SCROLL"))
echo "${helptext[$i]}"
fi
done
}
is_package_installed() {
stats=$(dpkg -s "$1" 2> /dev/null | grep "Status")
if [[ "$stats" == *"installed"* ]] ; then
echo "Installed"
else
echo "Noninstalled"
fi
}
compile_and_install() {
INSTALLED_WSLU=false
if [ "$(whoami)" != "root" ]; then
echo "To install/compile package, this script must be run as root"
exit
fi
if [ "$WSL_STATUS" == "true" ] && [ "$(which wslview)" == "" ] ; then
echo "We detected you are using WSL2 and don't have wslu installed, would you like to install it and setup compatability?"
read -r -n1 -s -p "[Y]es or [N]o" confirmation
echo ""
if [ "$confirmation" == "Y" ] || [ "$confirmation" == "y" ] ; then
if [ ! -f "/etc/wsl.conf" ] ; then
echo "[interop]" >> /etc/wsl.conf
echo "enabled=true" >> /etc/wsl.conf
fi
apt-add-repository ppa:wslutilities/wslu -y
apt update
apt-get install wslu
wslview -r
INSTALLED_WSLU=true
fi
elif [ "$WSL_STATUS" == "true" ] && [ "$(which wslview)" != "" ] && [ ! -f "/etc/wsl.conf" ] ; then
echo "We detected you are using WSL2 and don't a certian compatability patch installed, would you like to install it?"
read -r -n1 -s -p "[Y]es or [N]o" confirmation
echo ""
if [ "$confirmation" == "Y" ] || [ "$confirmation" == "y" ] ; then
if [ ! -f "/etc/wsl.conf" ] ; then
echo "[interop]" >> /etc/wsl.conf
echo "enabled=true" >> /etc/wsl.conf
fi
wslview -r
INSTALLED_WSLU=true
fi
fi
cd /tmp || return
git clone https://github.com/neurobin/shc.git
cd ./shc || return
./configure
make
echo "#!/bin/bash" >> /tmp/search-google.sh
echo "echo \$1 | sed 's/ /%20/g;s/!/%21/g;s/\"/%22/g;s/#/%23/g;s/\$/%24/g;s/\&/%26/g;s/'\''/%27/g;s/(/%28/g;s/)/%29/g;s/:/%3A/g' | xdg-open \"https://www.google.com/search?q=\$1\"" >> /tmp/search-google.sh
./src/shc -f /tmp/search-google.sh -o /tmp/search-google
cd ..
mv /tmp/search-google /bin/
rm -rf /tmp/search-google.sh /tmp/shc
if [ "$INSTALLED_WSLU" == "true" ] ; then
echo "Due to WSL2 compatability patches, would you allow us to restart WSL2?"
read -r -n1 -s -p "[Y]es or [N]o" reboot_confirmation
echo ""
if [ "$reboot_confirmation" == "Y" ] || [ "$reboot_confirmation" == "y" ] ; then
echo "Rebooting WSL2 in 5 seconds"
sleep 5
/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe "Start-Process -FilePath \"cmd\" -ArgumentList \"/c wsl.exe -d $WSL_DISTRO_NAME --shutdown\""
else
echo "If we cannot reboot, then please reboot yourself when you can using \"wsl --shutdown\""
fi
fi
}
help_section() {
if [ "$FANCY_TERMINAL" == true ] ; then
clear
render_box
render_help_header
render_help_scrolled
RENDERING_HELP=true
while [ "$RENDERING_HELP" == true ]
do
read -r -n1
if [ "$(("$TERMINAL_HEIGHT" - 5 + "$HELP_SCROLL"))" -ge 3 ] ; then
RENDERING_HELP=false
else
HELP_SCROLL=$(("$HELP_SCROLL" + 1))
fi
clear
render_box
render_help_header
render_help_scrolled
if [ "$RENDERING_HELP" == true ] ; then
sleep 1
fi
done
else
echo "Created by Jacob Allen Morris under MIT license (Visible with -l flag)"
echo "For help on a specific flag do \"install-search-google.sh -h (full flag or shorthand here)\""
echo " "
echo " "
echo "Flags included in this script are as follows"
echo " "
echo "-i : compile/install the script/executable"
echo "-h <flag> : help page for script"
echo "-l : view license for this script"
echo "-f : disable fancy terminal settings"
fi
}
help_section_fancy() {
if [ "$FANCY_TERMINAL" == true ] ; then
clear
render_box
render_help_header
render_help_fancy_scrolled
RENDERING_HELP=true
while [ "$RENDERING_HELP" == true ]
do
read -r -n1
if [ "$(("$TERMINAL_HEIGHT" - 5 + "$HELP_SCROLL"))" -ge 2 ] ; then
RENDERING_HELP=false
else
HELP_SCROLL=$(("$HELP_SCROLL" + 1))
fi
clear
render_box
render_help_header
render_help_fancy_scrolled
if [ "$RENDERING_HELP" == true ] ; then
sleep 1
fi
done
else
echo "Created by Jacob Allen Morris under MIT license (Visible with -l flag)"
echo "For help on a specific flag do \"install-search-google.sh -h (full flag or shorthand here)\""
echo " "
echo " "
echo "This flag will simply make the terminal application more basic"
echo "(Only use this if you must, I worked hard on this!)"
fi
}
help_section_install() {
if [ "$FANCY_TERMINAL" == true ] ; then
clear
render_box
render_help_header
render_help_install_scrolled
RENDERING_HELP=true
while [ "$RENDERING_HELP" == true ]
do
read -r -n1
if [ "$(("$TERMINAL_HEIGHT" - 5 + "$HELP_SCROLL"))" -ge 2 ] ; then
RENDERING_HELP=false
else
HELP_SCROLL=$(("$HELP_SCROLL" + 1))
fi
clear
render_box
render_help_header
render_help_install_scrolled
if [ "$RENDERING_HELP" == true ] ; then
sleep 1
fi
done
else
echo "Created by Jacob Allen Morris under MIT license (Visible with -l flag)"
echo "For help on a specific flag do \"install-search-google.sh -h (full flag or shorthand here)\""
echo " "
echo " "
echo "This will download using Git, shc, and compile it then proceed to write and compile"
echo "script made for easily searching google through your terminal"
fi
}
help_section_license() {
if [ "$FANCY_TERMINAL" == true ] ; then
clear
render_box
render_help_header
render_help_license_scrolled
RENDERING_HELP=true
while [ "$RENDERING_HELP" == true ]
do
read -r -n1
if [ "$(("$TERMINAL_HEIGHT" - 5 + "$HELP_SCROLL"))" -ge 2 ] ; then
RENDERING_HELP=false
else
HELP_SCROLL=$(("$HELP_SCROLL" + 1))
fi
clear
render_box
render_help_header
render_help_license_scrolled
if [ "$RENDERING_HELP" == true ] ; then
sleep 1
fi
done
else
echo "Created by Jacob Allen Morris under MIT license (Visible with -l flag)"
echo "For help on a specific flag do \"install-search-google.sh -h (full flag or shorthand here)\""
echo " "
echo " "
echo "This flag shows the license in it's entierty"
echo "(The MIT license to be exact)"
fi
}
license() {
if [ "$FANCY_TERMINAL" == true ] ; then
clear
render_box
render_license_header
render_license_scrolled
RENDERING_LICENSE=true
while [ "$RENDERING_LICENSE" == true ]
do
read -r -n1
if [ "$(("$TERMINAL_HEIGHT" - 5 + "$LICENSE_SCROLL"))" -ge 15 ] ; then
RENDERING_LICENSE=false
else
LICENSE_SCROLL=$(("$LICENSE_SCROLL" + 1))
fi
clear
render_box
render_license_header
render_license_scrolled
if [ "$RENDERING_LICENSE" == true ] ; then
sleep 1
fi
done
else
echo "MIT License"
echo ""
echo "Copyright (c) 2022 Jacob Allen Morris"
echo ""
echo "Permission is hereby granted, free of charge, to any person obtaining a copy" #1
echo "of this software and associated documentation files (the \"Software\"), to deal" #2
echo "in the Software without restriction, including without limitation the rights" #3
echo "to use, copy, modify, merge, publish, distribute, sublicense, and/or sell" #4
echo "copies of the Software, and to permit persons to whom the Software is" #5
echo "furnished to do so, subject to the following conditions:" #6
echo ""
echo "The above copyright notice and this permission notice shall be included in all" #7
echo "copies or substantial portions of the Software." #8
echo ""
echo "THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR" #9
echo "IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY," #10
echo "FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE" #11
echo "AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER" #12
echo "LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM," #13
echo "OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE" #14
echo "SOFTWARE."
fi
}
SHOW_LICENSE=false
SHOW_HELP_SECTION=false
SHOW_HELP_SECTION_LICENSE=false
SHOW_HELP_SECTION_INSTALL=false
SHOW_HELP_SECTION_FANCY=false
check_and_install_dependencies() {
if [ "$(is_package_installed binutils)" == "Noninstalled" ] ; then
REQUIRED_PACKAGES="$REQUIRED_PACKAGES binutils"
fi
if [ "$(is_package_installed make)" == "Noninstalled" ] ; then
REQUIRED_PACKAGES="$REQUIRED_PACKAGES make"
fi
if [ "$(is_package_installed gcc)" == "Noninstalled" ] ; then
REQUIRED_PACKAGES="$REQUIRED_PACKAGES gcc"
fi
if [ "$(is_package_installed git)" == "Noninstalled" ] ; then
REQUIRED_PACKAGES="$REQUIRED_PACKAGES git"
fi
if [ "$(is_package_installed software-properties-common)" == "Noninstalled" ] ; then
REQUIRED_PACKAGES="$REQUIRED_PACKAGES software-properties-common"
fi
if [ "$(is_package_installed xdg-utils)" == "Noninstalled" ] ; then
REQUIRED_PACKAGES="$REQUIRED_PACKAGES xdg-utils"
fi
if [ "$REQUIRED_PACKAGES" != "" ] ; then
if [ "$(whoami)" == "root" ] ; then
echo "Are you ok with this script installing these packages :$REQUIRED_PACKAGES, ?"
read -r -n1 -s -p "[Y]es or [N]o" confirmation
echo ""
if [ "$confirmation" == "Y" ] || [ "$confirmation" == "y" ] ; then
echo "Installing dependencies"
apt-get install $REQUIRED_PACKAGES -y
echo "Please re-launch this script"
fi
else
echo "This program requires these packages :$REQUIRED_PACKAGES, to run, please either re-run this script as root or copy and paste the command below to install"
echo ""
echo "sudo apt-get install$REQUIRED_PACKAGES"
fi
fi
}
while getopts :flih:: opt; do
case $opt in
f)
FANCY_TERMINAL=false
;;
l)
check_and_install_dependencies
SHOW_LICENSE=true
;;
h)
check_and_install_dependencies
if [ "$OPTARG" == "l" ] || [ "$OPTARG" == "=l" ]; then
SHOW_HELP_SECTION_LICENSE=true
elif [ "$OPTARG" == "i" ] || [ "$OPTARG" == "=i" ]; then
SHOW_HELP_SECTION_INSTALL=true
elif [ "$OPTARG" == "f" ] || [ "$OPTARG" == "=f" ]; then
SHOW_HELP_SECTION_FANCY=true
else
SHOW_HELP_SECTION=true
fi
;;
i)
check_and_install_dependencies
compile_and_install
exit
;;
*)
check_and_install_dependencies
SHOW_HELP_SECTION=true
;;
esac
done
if [ "$SHOW_HELP_SECTION" == true ] ; then
help_section
elif [ "$SHOW_HELP_SECTION_INSTALL" == true ] ; then
help_section_install
elif [ "$SHOW_HELP_SECTION_LICENSE" == true ] ; then
help_section_license
elif [ "$SHOW_HELP_SECTION_FANCY" == true ] ; then
help_section_fancy
elif [ "$SHOW_LICENSE" == true ] ; then
license
else
help_section
fi
if [ "$FANCY_TERMINAL" == true ] ; then
move_cursor 0 "$TERMINAL_HEIGHT"
fi
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment