Last active
May 17, 2024 10:18
-
-
Save ldotlopez/1ef05919e3b30ef6eadce7e4bef2b24c to your computer and use it in GitHub Desktop.
ps1 bash module
This file contains hidden or 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
#!/usr/bin/env bash | |
# | |
# Copyright (C) 2024 Luis Lรณpez <[email protected]> | |
# | |
# This program is free software; you can redistribute it and/or | |
# modify it under the terms of the GNU General Public License | |
# as published by the Free Software Foundation; either version 2 | |
# of the License, or (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program; if not, write to the Free Software | |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, | |
# USA. | |
# | |
# Bash 'strict-mode' | |
# set -o errexit | |
# set -o errtrace | |
# set -o nounset | |
# set -o pipefail | |
# export IFS=$'\n' | |
# Basic usage: | |
# ``` | |
# $ source ps1.sh | |
# $ export PS1=$(ps1_get_prompt) | |
# ``` | |
function ps1_get_prompt() { | |
local color="${1:-yes}" | |
if [[ "$color" = yes ]]; then | |
# Wrap color codes inside \[ and \] | |
# https://superuser.com/questions/376790/how-can-i-fix-my-custom-ps1-to-wrap-a-long-command-over-multiple-lines-instead-o | |
local PS1_ICON="${PS1_ICON:-$(ps1_get_icon "$(hostname -s)")}" | |
local PS1_COLOR='\['"${PS1_COLOR:-$(ps1_get_color "$(hostname -s)")}"'\]' | |
local PATH_PORTION_COLOR='\[\033[01;34m\]' | |
local COLOR_RESET='\[\033[00m\]' | |
local PS1_USER_HOST_PORTION="${PS1_COLOR}"'\u@\h'"${COLOR_RESET}" | |
local PS1_PATH_PORTION="${PATH_PORTION_COLOR}"'\w'"${COLOR_RESET}" | |
echo "${PS1_ICON} ${PS1_USER_HOST_PORTION}:${PS1_PATH_PORTION}\$ " | |
else | |
printf '\u@\h:\w\$ ' | |
fi | |
} | |
function ps1_get_color() { | |
local hostname="${1:-$(hostname -s)}" | |
hostname="${hostname/.*/}" | |
if [[ "$(uname -s)" == "Darwin" ]]; then | |
hashfunc=$(type -p md5) | |
else | |
hashfunc=$(type -p md5sum) | |
fi | |
local hash | |
hash=$("$hashfunc" <<<"$hostname") | |
hash=${hash/ */} | |
if [[ $TERM == *256* ]]; then | |
_ps1_hash_to_rgb_color "$hash" | |
else | |
_ps1_hash_to_15bit_color "$hash" | |
fi | |
} | |
function ps1_get_icon() { | |
local hostname="${1:-$(hostname -s)}" | |
hostname="${hostname/.*/}" | |
local -a icons=(๐ธ ๐ถ โจ ๐ฅ ๐ โญ ๐ต ๐ฆ ๐ฆ ๐ด ๐ซ ๐ฆ ๐ฎ ๐ท ๐ ๐ป ๐ฆฅ ๐ฆฆ ๐) | |
local hashfunc hash | |
if [[ "$(uname -s)" == "Darwin" ]]; then | |
hashfunc=$(type -p md5) | |
else | |
hashfunc=$(type -p md5sum) | |
fi | |
hash="$("$hashfunc" <<<"$hostname")" | |
hash="${hash/ */}" | |
sum=0 | |
for letter in $(echo "$hash" | sed -e 's,\.,,g' -e 's,\(.\),\1 ,g'); do | |
sum=$((sum + $((16#$letter)))) | |
done | |
len=$((${#icons[@]} + 1)) | |
idx=sum%$len | |
echo "${icons[$idx]}" | |
} | |
function _ps1_hash_to_15bit_color() { | |
local hash="$1" | |
local bright=${hash:0:12} | |
local color=${hash:20:12} | |
printf '\033[%d;%dm' \ | |
$(($(printf '%d' "0x${bright}") % 3)) \ | |
$(($(($(printf '%d' "0x${color}") % 5)) + 32)) | |
} | |
function _ps1_hash_to_rgb_color() { | |
local hash="$1" | |
local R=${hash:0:8} | |
local G=${hash:12:8} | |
local B=${hash:24:8} | |
printf '\033[38;2;%d;%d;%dm' \ | |
$(($(printf '%d' "0x${R}") % 256)) \ | |
$(($(printf '%d' "0x${G}") % 256)) \ | |
$(($(printf '%d' "0x${B}") % 256)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment