Created
April 6, 2018 17:28
-
-
Save Trucido/d4c13b8c15436debd2281803d0e22491 to your computer and use it in GitHub Desktop.
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
# ~/.rc/bash_prompt.sh | |
# Check that we're zsh and real bash not sh bash | |
case "$(readlink /proc/$$/exe 2>/dev/null)" in | |
*/bash) ! shopt -oq posix || return;; | |
*/zsh) ;; | |
*) return;; | |
esac | |
# Interactive only | |
[[ $- == *i* ]] || return | |
# Still have no idea wtf the purpose of this is... | |
tty -s || return | |
# | |
# $PROMPT_COMMAND | |
# | |
# Prepend window title of X terminals and other GUI SSH terminal windows, | |
# i.e. user@host:dir in window/tab title or current command we're running. | |
# This variable may already be set by your distro, or they might be using PS1= and PS1+= instead. | |
# | |
# If not set, you may ptionally specify an expert setting for PROMPT_COMMAND in /etc/sysconfig, /etc/default, or your | |
# home .config directory which will skip the rest of the tests , but be warned that no error checking is performed. | |
# | |
# We prefer vte.sh where applicable (and if installed) before hardcoding PROMPT_COMMAND. | |
# The filename varies across distros (often appended with its version number), so | |
# if we can't find the file or libvte is not installed, then we glob for the file, | |
# otherwise we fallback to a rather sane default. | |
# | |
# Note: It's vte.sh's responsibility to 'not load' when it's not applicable, | |
# such as not in side a VTE term, so it does no harm checking/sourcing it. | |
# | |
# vte.sh is usually installed as part of libvte or other distro packages, | |
# primarily used in GTK+ terminals as it "improves usage of VTE based terminals". | |
# It's vte.sh's responsibility to 'not load' when it's not applicable, | |
# such as not in side a VTE term. | |
# | |
# This description is vague, and varies across distros regarding window title in xterms. | |
# For example, openSUSE claims KDE konsole in needs to have "%w" in the "tabs" setting. | |
# Debian & Ubuntu comment out their hardcoded PROMPT_COMMAND, claiming that it | |
# overwrites xterm -T "title" -n "icontitle". | |
# | |
# Your miligeage may vary, and you may download vte.sh from it's home at: | |
# https://github.com/jessevdk/vte/blob/master/src/vte.sh | |
# The below lines of code check for /etc/local/profile.d/vte.sh if you choose to manually | |
# download the script there. | |
# | |
# If for some reason you get a double PS1 prompt or error message ssaying: | |
# command not found: __vte_prompt_command then it's best you comment out this code or use | |
# a custom value such as debian's 'echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"' | |
# | |
if [ -z "$PROMPT_COMMAND" ]; then | |
case $TERM in | |
xterm*|vte*) | |
# check /etc/sysconfig and /etc/default for expert custom file first | |
# Debian example: 'echo -ne "\033]0;${USER}@${HOSTNAME}: ${PWD}\007"' | |
if [ -e /etc/sysconfig/bash-prompt-xterm ]; then | |
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm | |
elif [ -e /etc/default/bash-prompt-xterm ]; then | |
PROMPT_COMMAND=/etc/default/bash-prompt-xterm | |
elif [[ -e "${XDG_CONFIG_HOME:-$HOME/.config}/bash-prompt-xterm" ]]; then | |
PROMPT_COMMAND=${XDG_CONFIG_HOME:-$HOME/.config}/bash-prompt-xterm | |
elif [ "${VTE_VERSION:-0}" -ge 3405 ]; then | |
# If not found, search for vte.sh only if we're in a libvte terminal. | |
if [ -r /etc/profile.d/vte.sh ]; then | |
. /etc/profile.d/vte.sh && PROMPT_COMMAND="__vte_prompt_command" | |
elif [ -r /etc/profile.d/vte-2.91.sh ]; then | |
. /etc/profile.d/vte-2.91.sh && PROMPT_COMMAND="__vte_prompt_command" | |
elif [ -r /etc/local/profile.d/vte.sh ]; then | |
. /etc/local/profile.d/vte.sh && PROMPT_COMMAND="__vte_prompt_command" | |
else | |
for v in /etc/profile.d/vte*.sh; do | |
if [ -r "$v" ]; then | |
. "$v" && PROMPT_COMMAND="__vte_prompt_command" | |
else | |
# no file found so we fallback to hardcoded default. | |
PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"' | |
fi | |
done | |
unset v | |
fi | |
else | |
# Else no prompt or vte file found, or not in a libvte compatible terminal, | |
# fallback to hardcoded default. | |
PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"' | |
fi | |
;; | |
screen*) | |
# same thing, check if custom file first, else use fallback. | |
if [ -e /etc/sysconfig/bash-prompt-screen ]; then | |
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen | |
elif [ -e /etc/default/bash-prompt-screen ]; then | |
PROMPT_COMMAND=/etc/default/bash-prompt-screen | |
elif [[ -e "${XDG_CONFIG_HOME:-$HOME/.config}/bash-prompt-screen" ]]; then | |
PROMPT_COMMAND=${XDG_CONFIG_HOME:-$HOME/.config}/bash-prompt-screen | |
else | |
PROMPT_COMMAND='printf "\033k%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"' | |
fi | |
;; | |
#tmux*) # Should we add another case for tmux or byobu? Or do most use screen/xterm termcaps? | |
#;;# The catchall below seems to work, might have to add more and unset there. | |
*) | |
if [ -e /etc/sysconfig/bash-prompt-default ]; then | |
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default | |
elif [ -e /etc/default/bash-prompt-default ]; then | |
PROMPT_COMMAND=/etc/default/bash-prompt-default | |
elif [[ -e "${XDG_CONFIG_HOME:-$HOME/.config}/bash-prompt-default" ]]; then | |
PROMPT_COMMAND=${XDG_CONFIG_HOME:-$HOME/.config}/bash-prompt-default | |
else | |
# TODO: test if this breaks anything, seems to work in tmux and ssh terms where others didn't. | |
PROMPT_COMMAND='printf "\033k%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"' | |
fi | |
;; | |
esac | |
fi | |
# Gentoo's implementation without PROMPT_COMMAND: | |
# Note: Seems to require any subsequent PS1= to be PS1+= | |
# or $PS1 added to the end each of these? Would then have to be AFTER main PS1 then. | |
#case ${TERM} in | |
# [aEkx]term*|rxvt*|gnome*|konsole*|interix) | |
# PS1='\[\033]0;\u@\h:\w\007\]' | |
# ;; | |
# screen*) | |
# PS1='\[\033k\u@\h:\w\033\\\]' | |
# ;; | |
# *) | |
# unset PS1 | |
# ;; | |
#esac | |
# Source for git information in prompt. | |
# Avoid double sourcing if bash-completion code already handled this. | |
if ! type __git_ps1 >/dev/null 2>&1 && [ "${HIDE_GIT_PS1-}" != "1" ]; then | |
# Check for programmatic completion and posix mode | |
if shopt -q progcomp && ! shopt -oq posix; then | |
if [ -f /etc/bash_completion.d/git-prompt.sh ]; then | |
. /etc/bash_completion.d/git-prompt.sh | |
elif [ -f /usr/share/git-core/contrib/completion/git-prompt.sh ]; then | |
. /usr/share/git-core/contrib/completion/git-prompt.sh | |
fi | |
# Set git prompt information. | |
GIT_PS1_SHOWDIRTYSTATE=1 | |
GIT_PS1_SHOWSTASHSTATE=1 | |
GIT_PS1_SHOWUNTRACKEDFILES=1 | |
# GIT_PS1_SHOWUPSTREAM="verbose legacy git" | |
GIT_PS1_DESCRIBE_STYLE=default | |
# GIT_PS1_SHOWCOLORHINTS=1 | |
fi | |
fi | |
# Stub out function if above fails. | |
if ! type __git_ps1 >/dev/null 2>&1; then | |
__git_ps1() { :; } | |
fi | |
# Some common colors in raw escape codes. | |
# Note: tput commands don't always work and polutes environment. | |
BASE0=$'\E[38;5;244m' | |
BASE00=$'\E[38;5;241m' | |
BASE01=$'\E[38;5;240m' | |
BASE02=$'\E[38;5;235m' | |
BASE03=$'\E[38;5;234m' | |
BASE1=$'\E[38;5;245m' | |
BASE2=$'\E[38;5;254m' | |
BASE3=$'\E[38;5;230m' | |
BLUE=$'\E[38;5;33m' | |
BOLD=$'\E[1m' | |
CYAN=$'\E[38;5;37m' | |
GREEN=$'\E[38;5;64m' | |
NORMAL_GREEN=$'\E[38;5;32m' | |
BOLD_GREEN=$'\E[00;1;32m' | |
MAGENTA=$'\E[38;5;125m' | |
ORANGE=$'\E[38;5;166m' | |
RED=$'\E[38;5;160m' | |
BOLD_RED=$'\E[00;1;31m' | |
NORMAL_RED=$'\E[38;5;31m' | |
RESET=$'\E(B\E[m' | |
VIOLET=$'\E[38;5;61m' | |
YELLOW=$'\E[38;5;136m' | |
# root be red | |
# Note: check for WSL first and clean env. | |
if ! IS_WSL=`grep -i microsoft /proc/version`; then | |
if [ "${EUID}" = "0" ] || [ "$USER" = "root" ]; then | |
USER_COLOR=$'\E[38;5;160m' | |
elif [ "${EUID}" -ge "1000" ] || [ "$(id -u)" -ge "1000" ]; then | |
#USER_COLOR=$'\E[38;5;64m' | |
USER_COLOR=$'\E[00;0;32m' | |
else | |
USER_COLOR=$'\E[38;5;245m' | |
fi | |
unset IS_WSL | |
else | |
# if we're in WSL use ms blue | |
USER_COLOR=$'\E[38;5;33m' | |
# Note: Don't unset IS_WSL | |
fi | |
# Host colors for common distros. | |
#TODO: narrow down distro:flavor, and add more. also alphabetize. | |
DISTRO=$(grep -e '^NAME=' /etc/os-release) | |
case "$DISTRO" in | |
*Leap*) HOST_COLOR=$'\E[00;0;32m' ;; | |
*Tumbleweed*) HOST_COLOR=$'\E[38;5;37m' ;; | |
*SLE*) HOST_COLOR=$'\E[38;5;64m' ;; | |
*Debian*) HOST_COLOR=$'\E[38;5;31m' ;; | |
*Kali*) HOST_COLOR=$'\E[38;5;160m' ;; | |
*Sparky*) HOST_COLOR=$'\E[38;5;160m' ;; | |
*OpenMediaVault*) HOST_COLOR=$'\E[38;5;61m' ;; | |
*Solus*|*Fedora*|*Korora*) HOST_COLOR=$'\E[38;5;33m' ;; | |
*Red*|*Cent*|*Prox*) HOST_COLOR=$'\E[38;5;160m' ;; | |
*buntu*) HOST_COLOR=$'\E[38;5;166m' ;; | |
*Mageia*) HOST_COLOR=$'\E[38;5;33m' ;; | |
*Gentoo*|*Funtoo*|*Calculate*|sabayon) HOST_COLOR=$'\E[38;5;61m' ;; | |
*) HOST_COLOR=$'\E[38;5;245m' ;; | |
# If you get a gray host color now you know why. | |
# TODO: maybe grep for ANSI_COLOR? | |
esac | |
#PS1 | |
if [ "${EUID}" = "0" ] || [ "$USER" = "root" ];then | |
PS1="\[${RESET}\]\[${BASE00}\][\[${BASE1}\]\A\[${BASE00}\]]-\[${RESET}\]\[${BASE00}\](\[${USER_COLOR}\]\u\[${RESET}\]\[${BASE00}\]@\[${RESET}\]\[${HOST_COLOR}\]\h\[${RESET}\]\[${BASE00}\])-(\[${RESET}\]\[${CYAN}\]\w\[${YELLOW}\]\$(__git_ps1 \" (%s)\")\[${RESET}\]\[${BASE00}\])\[${USER_COLOR}\]#\[${RESET}\] " | |
elif [ "${EUID}" -ge "1000" ] || [ "$(id -u)" -ge "1000" ]; then | |
PS1="\[${RESET}\]\[${BASE00}\][\[${BASE1}\]\A\[${BASE00}\]]-\[${RESET}\]\[${BASE00}\](\[${USER_COLOR}\]\u\[${RESET}\]\[${BASE00}\]@\[${RESET}\]\[${HOST_COLOR}\]\h\[${RESET}\]\[${BASE00}\])-(\[${RESET}\]\[${CYAN}\]\w\[${YELLOW}\]\$(__git_ps1 \" (%s)\")\[${RESET}\]\[${BASE00}\])\[${USER_COLOR}\]\$\[${RESET}\] " | |
fi | |
unset DISTRO BASE0 BASE00 BASE01 BASE02 BASE03 BASE1 BASE2 BASE3 BLUE BOLD CYAN GREEN NORMAL_GREEN BOLD_GREEN MAGENTA ORANGE RED BOLD_RED NORMAL_RED RESET VIOLET YELLOW USER_COLOR HOST_COLOR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment