Skip to content

Instantly share code, notes, and snippets.

@luavixen
Last active January 31, 2022 01:25
Show Gist options
  • Save luavixen/7c87f3eb0ac116207502e838169bfd1e to your computer and use it in GitHub Desktop.
Save luavixen/7c87f3eb0ac116207502e838169bfd1e to your computer and use it in GitHub Desktop.
Lua's shell profile
#!/bin/sh
#
# Lua's shell profile
#
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or distribute
# this software, either in source code form or as a compiled binary, for any
# purpose, commercial or non-commercial, and by any means.
#
# In jurisdictions that recognize copyright laws, the author or authors of this
# software dedicate any and all copyright interest in the software to the public
# domain. We make this dedication for the benefit of the public at large and to
# the detriment of our heirs and successors. We intend this dedication to be an
# overt act of relinquishment in perpetuity of all present and future rights to
# this software under copyright law.
#
# 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 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.
#
# For more information, please refer to <http://unlicense.org/>
#
export COLOR1=013
export COLOR2=213
export COLOR3=141
__lua_promptsetup() {
promptwrite() {
printf %s "$*"
}
if command -v sed >/dev/null 2>&1; then
promptcolor() {
promptwrite "\[\033[$(promptwrite "$*" | command sed "s/ /;/g")m\]"
}
promptclrgb() {
local code
for code in "$@"; do
promptcolor 38 5 "$code"
done
}
else
promptcolor() {
return 0
}
promptclrgb() {
return 0
}
fi
local sub_user="\u"
local sub_host="\h"
local sub_fqdn="\H"
local sub_cwd="\w"
local sub_dir="\W"
local sub_shell="\s"
local sub_time="\@"
local sub_prompt="\\$"
prompt1() {
promptcolor 0
promptclrgb $COLOR1
promptwrite "["
promptcolor 0
promptclrgb $COLOR2
promptwrite $sub_user
promptwrite "@"
promptwrite $sub_host
promptcolor 0
promptwrite " "
promptclrgb $COLOR3
promptwrite $sub_cwd
promptclrgb $COLOR1
promptwrite "]"
promptwrite $sub_prompt
promptcolor 0
promptwrite " "
}
prompt2() {
promptcolor 0
promptclrgb $COLOR1
promptwrite ">"
promptcolor 0
promptwrite " "
}
export PS1="$(prompt1)"
export PS2="$(prompt2)"
unset -f promptwrite
unset -f promptcolor
unset -f promptclrgb
unset -f prompt1
unset -f prompt2
}
__lua_promptsetup
if [ -n "$__lua_active" ]; then
return 0
else
__lua_active="yes"
fi
__lua_has() {
local usage="Checks if all provided COMMANDs are available on this system
Usage: has COMMAND [COMMAND]..."
if [ $# -eq 0 ]; then
echo "$usage" >&2
return 1
fi
local cmd
if [ -n "$BASH" ]; then
for cmd in "$@"; do
if ! type -P "$cmd" >/dev/null 2>&1; then
return 1
fi
done
else
for cmd in "$@"; do
(
unalias -a "$cmd"
unset -f "$cmd"
command -v "$cmd"
) >/dev/null 2>&1
if [ $? -ne 0 ]; then
return 1
fi
done
fi
return 0
}
alias has="__lua_has"
__lua_hasrequire() {
local usage="Outputs an error if any of the provided COMMANDs are not available on this system
Usage: hasrequire COMMAND [COMMAND]..."
if [ $# -eq 0 ]; then
echo "$usage" >&2
return 1
fi
local cmd
for cmd in "$@"; do
if ! __lua_has "$cmd"; then
echo "Command '$cmd' is required but not available." >&2
return 1
fi
done
return 0
}
alias hasrequire="__lua_hasrequire"
__lua_hasfirst() {
local usage="Outputs the first provided COMMAND that is available on this system
If none of the provided COMMANDs are available, then the last provided COMMAND is outputted
Usage: hasfirst COMMAND [COMMAND]..."
if [ $# -eq 0 ]; then
echo "$usage" >&2
return 1
fi
local cmd
for cmd in "$@"; do
if __lua_has "$cmd"; then
echo "$cmd"
return 0
fi
done
echo "$cmd"
return 1
}
alias hasfirst="__lua_hasfirst"
__lua_resolve() {
local usage="Resolves each PATH into absolute form, following symlinks
Usage: resolve PATH [PATH]..."
if [ $# -eq 0 ]; then
echo "$usage" >&2
return 1
fi
if ! __lua_hasrequire readlink; then return 1; fi
local resolved
local path_old
local path_new
for path_old in "$@"; do
path_new="$(command readlink -f "$path_old")"
if [ -z "$path_new" ] || ! [ -e "$path_new" ]; then
return 1
fi
if [ -z "$resolved" ]; then
resolved="$path_new"
else
resolved="$resolved
$path_new"
fi
done
echo "$resolved"
return 0
}
alias resolve="__lua_resolve"
__lua_resolvecmd() {
local usage="Outputs the absolute path to the binary that COMMAND refers to
Usage: resolvecmd COMMAND"
if [ $# -eq 0 ]; then
echo "$usage" >&2
return 1
fi
local cmd="$1"
local cmd_path
if __lua_has which; then
cmd_path="$(command which "$cmd" 2>/dev/null)"
else
cmd_path="$(__lua_resolve "$(command -v "$cmd" 2>/dev/null)")"
fi
if [ -f "$cmd_path" ]; then
echo "$cmd_path"
return 0
fi
return 1
}
alias resolvecmd="__lua_resolvecmd"
__lua_hasfirstresolve() {
local usage="Outputs the absolute path to the binary of the first provided COMMAND that is available on this system
If none of the provided COMMANDs are available on this system then the last provided COMMAND is outputted as provided
Usage: hasfirstresolve COMMAND [COMMAND]..."
if [ $# -eq 0 ]; then
echo "$usage" >&2
return 1
fi
local cmd
local cmd_path
for cmd in "$@"; do
if cmd_path="$(__lua_resolvecmd "$cmd")"; then
echo "$cmd_path"
return 0
fi
done
echo "$cmd"
return 1
}
alias hasfirstresolve="__lua_hasfirstresolve"
__lua_confirm() {
local usage="Presents a [y/N] confirmation dialog and returns the result in the exit code
Usage: confirm MESSAGE"
if [ $# -eq 0 ]; then
echo "$usage" >&2
return 1
fi
printf %s "$*"
local response
read -r -p " [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
return 0
;;
esac
return 1
}
alias confirm="__lua_confirm"
if __lua_has date; then
__lua_now() {
command date '+%s' 2>/dev/null
return $?
}
else
__lua_now() {
echo "0"
return 0
}
fi
alias now="__lua_now"
__lua_profile_locate() {
local path
if [ -n "$BASH_SOURCE" ]; then
path="$BASH_SOURCE"
elif [ -n "$LUA_SCRIPT" ]; then
path="$LUA_SCRIPT"
else
path="/etc/profile.d/lua.sh"
fi
local path_resolved="$(__lua_resolve "$path")"
if [ -n "$path_resolved" ]; then
path="$path_resolved"
fi
if ! [ -f "$path" ]; then
echo "Could not find the path of the lua.sh script" >&2
else
export LUA_SCRIPT="$path"
export LUA_SCRIPT_DIR="$(dirname "$path")"
fi
}
__lua_profile_locate
__lua_profile_env() {
export LUA_ENV="$LUA_SCRIPT"
export LUA_ENV_DIR="$LUA_SCRIPT_DIR"
export ENV="$LUA_ENV"
export SHINIT="$LUA_ENV"
export LUA_SETUP="source '$LUA_SCRIPT'"
if [ -n "$LUA_SETUP_ENABLE" ]; then
export PROMPT_COMMAND="$LUA_SETUP"
fi
}
__lua_profile_env
if __lua_has exa; then
__lua_ls() {
command exa --color=always --color-scale "$@"
return $?
}
else
__lua_ls() {
command ls "$@"
return $?
}
fi
__lua_alias_ll() {
__lua_ls -la "$@"
return $?
}
__lua_alias_l() {
__lua_ls -l "$@"
return $?
}
alias ls="__lua_ls"
alias ll="__lua_alias_ll"
alias l="__lua_alias_l"
if __lua_has trash; then
__lua_trash() {
command trash "$@"
return $?
}
else
__lua_trash() {
command rm "$@"
return $?
}
fi
alias rm="__lua_trash"
if __lua_has bat; then
__lua_cat() {
command bat "$@"
return $?
}
else
__lua_cat() {
command cat "$@"
return $?
}
fi
alias cat="__lua_cat"
__lua_clear__last=0
__lua_clear__curr=0
__lua_clear() {
__lua_clear__curr="$(__lua_now)"
if [ $(( $__lua_clear__curr - $__lua_clear__last )) -lt 3 ]; then
command reset
fi
__lua_clear__last="$__lua_clear__curr"
if __lua_has tput; then
command tput reset
elif __lua_has reset; then
command reset
fi
if __lua_has clear; then
command clear
fi
printf "\e[3J"
return 0
}
alias clear="__lua_clear"
alias reset="__lua_clear"
__lua_sudo() {
if ! __lua_hasrequire sudo; then return 1; fi
if [ $# -eq 0 ]; then
command sudo -h
return 1
fi
if [ "$*" = "su" ]; then
__lua_profile_env
command sudo ENV="$LUA_ENV" -E -- \
"$(__lua_hasfirstresolve bash dash ash zsh sh)" -i
return $?
fi
command sudo "$@"
return $?
}
alias sudo="__lua_sudo"
__lua_asroot() {
local usage="Runs COMMANDS using 'eval' if this shell is running as root, otherwise uses sudo to run COMMANDS
Usage: asroot COMMANDS"
if [ $# -eq 0 ]; then
echo "$usage" >&2
return 1
fi
local uid
if __lua_has id; then
uid="$(command id -u)"
else
uid="$UID"
fi
if [ -n "$uid" ] && [ "$uid" -eq 0 ]; then
eval "$*"
return $?
elif __lua_has sudo && __lua_confirm "Not running as root, use sudo?"; then
command sudo -- sh -c "$*"
return $?
fi
echo "Permission denied! Must be run as root" >&2
return 1
}
alias asroot="__lua_asroot"
export HISTSIZE=-1
export HISTFILESIZE=-1
export EDITOR="$(__lua_hasfirstresolve micro nano vim vi)"
alias micro="$EDITOR"
alias nano="$EDITOR"
export PACKAGE="$(__lua_hasfirstresolve dnf yum apt apt-get apk pacman yay nix brew apt)"
if ! __lua_has apk; then alias apk="$PACKAGE"; fi
if ! __lua_has apt; then alias apt="$PACKAGE"; fi
if ! __lua_has dnf; then alias dnf="$PACKAGE"; fi
if __lua_has java grep sed; then
export JAVA_HOME="$(command java -XshowSettings:properties -version 2>&1 | command grep java.home | command sed -e "s/\s*java.home = //")"
fi
__lua_detectruby() {
if ! __lua_has ruby gem; then return 1; fi
local gem_syst_dir="$(command ruby -e 'puts Gem.dir' 2>/dev/null)"
local gem_user_dir="$(command ruby -e 'puts Gem.user_dir' 2>/dev/null)"
if [ -d "$gem_syst_dir" ]; then
export GEM_HOME="$gem_user_dir"
export PATH="$PATH:$gem_syst_dir/bin"
fi
if [ -d "$gem_user_dir" ]; then
export GEM_HOME="$gem_user_dir"
export PATH="$PATH:$gem_user_dir/bin"
fi
}
__lua_detectruby
__lua_setexecute() {
local usage="Sets the execute bit on the provided FILEs
Usage: setexecute FILE [FILE]..."
if [ $# -eq 0 ]; then
echo "$usage" >&2
return 1
fi
if ! __lua_hasrequire chmod; then return 1; fi
local file
for file in "$@"; do
if ! command chmod +x "$file"; then
if __lua_has sudo && __lua_confirm "Failed to chmod! Retry using sudo?"; then
if ! command sudo chmod +x "$file"; then
return 1
fi
else
return 1
fi
fi
done
return 0
}
alias setexecute="__lua_setexecute"
alias mx="__lua_setexecute"
__lua_httpget() {
local usage="Downloads FILE from URL with a HTTP GET request
Usage: httpget [-v|--verbose] URL [FILE]"
local verbose=0
while [ $# -ne 0 ]; do
case "$1" in
-h|-\?|--help)
echo "$usage" >&2
return 1
;;
-v|--verbose)
verbose=1
shift
;;
-*|--*)
echo "Unknown option $1" >&2
return 1
;;
*)
break
;;
esac
done
if [ $# -eq 0 ]; then
echo "$usage" >&2
return 1
fi
if ! __lua_hasrequire wget; then return 1; fi
local arg0
local arg1
local arg2
if [ $# -eq 2 ]; then
arg0=-O
arg1="$2"
arg2="$1"
else
arg1="$1"
fi
if [ $verbose -ne 0 ]; then
command wget "$arg0" "$arg1" "$arg2"
return $?
else
command wget "$arg0" "$arg1" "$arg2" >/dev/null 2>&1
return $?
fi
}
alias httpget="__lua_httpget"
alias get="__lua_httpget"
__lua_updateprofile() {
if ! __lua_hasrequire mktemp chmod chown sh; then return 1; fi
local url_profile="https://gist.githubusercontent.com/luavixen/7c87f3eb0ac116207502e838169bfd1e/raw/lua.sh"
local path_file="$LUA_SCRIPT"
local path_dir="$LUA_SCRIPT_DIR"
local path_temp="$(command mktemp)"
local commands="cat '$path_temp' >'$path_file' && chown root:root '$path_file' && chmod 644 '$path_file'"
if ! [ -d "$path_dir" ]; then
echo "Cannot update profile, '$path_dir' is not a valid directory" >&2
return 1
fi
echo "Downloading new profile..." >&2
if ! __lua_httpget "$url_profile" "$path_temp"; then
echo "Failed to download profile" >&2
return 1
fi
echo "Writing profile to '$path_file'..." >&2
__lua_asroot "$commands"
return $?
}
alias updateprofile="__lua_updateprofile"
alias profileupdate="__lua_updateprofile"
__lua_editprofile() {
local editor
if ! editor="$(__lua_resolvecmd "$EDITOR")"; then
echo "Configured EDITOR is unset or could not be resolved" >&2
return 1
fi
local script="$LUA_SCRIPT"
if ! [ -f "$LUA_SCRIPT" ]; then
echo "Couldn't find the lua.sh script for editing" >&2
return 1
fi
__lua_asroot "command '$EDITOR' '$LUA_SCRIPT'"
return $?
}
alias editprofile="__lua_editprofile"
alias profileedit="__lua_editprofile"
__lua_neofetch_download() {
if ! __lua_hasrequire mktemp sh rm; then return 1; fi
local url_bash="https://github.com/luavixen/bash/raw/2b7877297fa2c45c113c0d3719ee4bcdf6126e11/bash"
local url_neofetch="https://raw.githubusercontent.com/dylanaraps/neofetch/master/neofetch"
local temp="$(mktemp)"
if ! __lua_httpget "$url_neofetch" "$temp"; then
echo "Failed to download neofetch" >&2
return 1
fi
local shell=bash
local shell_delete=0
if ! __lua_has "$shell"; then
if ! __lua_hasrequire uname chmod; then return 1; fi
if [ "$(command uname -s)" = Linux ] \
&& [ "$(command uname -m)" = x86_64 ]; then
shell="$(command mktemp)"
shell_delete=1
if ! __lua_httpget "$url_bash" "$shell"; then
echo "Failed to download bash" >&2
return 1
fi
command chmod u+x "$shell"
else
shell=sh
fi
fi
command "$shell" "$temp" "$@"
local code=$?
command rm -f "$temp"
if [ $shell_delete -ne 0 ]; then
command rm -f "$shell"
fi
return $code
}
__lua_neofetch_default() {
if __lua_has neofetch; then
command neofetch "$@"
return $?
else
__lua_neofetch_download "$@"
return $?
fi
}
__lua_neofetch() {
__lua_neofetch_default \
--ascii_colors $COLOR2 015 005 013 $COLOR3 219 \
--ascii_bold off \
--colors $COLOR2 $COLOR2 015 $COLOR1 015 015 \
--bold off \
"$@"
return $?
}
alias neofetch="__lua_neofetch"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment