Skip to content

Instantly share code, notes, and snippets.

@allex
Last active May 8, 2023 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allex/a7c062df0aba27c631aa92971ea83850 to your computer and use it in GitHub Desktop.
Save allex/a7c062df0aba27c631aa92971ea83850 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Personal bash PS1 by @allex_wang (https://iallex.com/) {{{
# Last Modified: Mon May 08, 2023 14:22
# GistID: a7c062df0aba27c631aa92971ea83850
# GistURL: https://gist.github.com/a7c062df0aba27c631aa92971ea83850
{
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
xterm-256color | xterm-color) color_prompt=yes;;
esac
# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes
if [ -n "$force_color_prompt" ]; then
if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
color_prompt=yes
else
color_prompt=
fi
fi
if [ "$color_prompt" = yes ]; then
PS1='\[\e[1;32m\][\u@\h \W]\$\[\e[0m\] '
else
# set variable identifying the chroot you work in (used in the prompt below)
if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
debian_chroot=$(cat /etc/debian_chroot)
fi
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
if type -t __git_ps1 >/dev/null 2>&1; then
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '
fi
## Add by Allex Wang, 2011/05/08
## http://bash.cyberciti.biz/guide/Changing_bash_prompt
__ps1_cwd() {
# How many characters of the $PWD should be kept
maxlen=15
dir=${PWD##*/}
p=${PWD/#$HOME/\~}
parts=$(echo "${p}" | tr "/" " ")
# Indicate that there has been dir truncation
trunc_symbol="${parts[0]}/..."
[ "${trunc_symbol:0:1}" = "~" ] || trunc_symbol="/${trunc_symbol}"
maxlen=$(( ( maxlen < ${#dir} ) ? ${#dir} : maxlen ))
local offset=$(( ${#p} - maxlen ))
if [ ${offset} -gt "0" ]; then
p=${p:$offset:$maxlen}
p=${trunc_symbol}/${p#*/}
fi
printf %s "$p"
}
__ps1_in_docker() {
(awk -F/ '$2 == "docker"' /proc/self/cgroup | read -r non_empty_input)
}
__ps1_init() {
# 27 = 033 = 0x1b = ^[ = \e
local c_reset="\[\e[0m\]" # unsets color to term's fg color
# regular colors
local k="\[\e[0;30m\]" # black
local r="\[\e[0;31m\]" # red
local g="\[\e[0;32m\]" # green
local y="\[\e[0;33m\]" # yellow
local b="\[\e[0;34m\]" # blue
local m="\[\e[0;35m\]" # magenta
local c="\[\e[0;36m\]" # cyan
local w="\[\e[0;37m\]" # white
# emphasized (bolded) colors
local em_k="\[\e[1;30m\]"
local em_r="\[\e[1;31m\]"
local em_g="\[\e[1;32m\]"
local em_y="\[\e[1;33m\]"
local em_b="\[\e[1;34m\]"
local em_m="\[\e[1;35m\]"
local em_c="\[\e[1;36m\]"
local em_w="\[\e[1;37m\]"
# background colors
local b_k="\[\e[40m\]"
local b_r="\[\e[41m\]"
local b_g="\[\e[42m\]"
local b_y="\[\e[43m\]"
local b_b="\[\e[44m\]"
local b_m="\[\e[45m\]"
local b_c="\[\e[46m\]"
local b_w="\[\e[47m\]"
local u_c=$c
if [ $UID -eq "0" ]; then
u_c=$r # root's color
fi
local _user="\u"
local _pwd="${em_g}\$(__ps1_cwd)" # \W
local _git_ps1=
local prompt_delimit='\\$'
if [ -n "${SSH_CLIENT:-$SSH_TTY}" ] || __ps1_in_docker; then
hostname=$(hostname)
# show current connection ip if hostname is 'localhost'
if [ "${hostname%%.*}" = "localhost" ]; then
ip=$(echo "${SSH_CONNECTION}" |cut -f3 -d ' ')
_user="\u@${ip:-\h}"
else
_user="\u@\h"
fi
fi
if type -t __git_ps1 >/dev/null 2>&1; then
_git_ps1="\$(__git_ps1 \" ${em_c}(%s)${c_reset}\" 2>/dev/null)"
fi
PS1="${m}${_user}${c_reset} ${_pwd}${_git_ps1} ${u_c}${prompt_delimit}${c_reset} "
}
{ [ "$color_prompt" = yes ] && __ps1_init; } || unset __ps1_cwd
unset __ps1_init color_prompt force_color_prompt
}
# END PS1 }}}
# vim: ft=sh:et:ts=2:sw=2:sts=2:ff=unix:fdm=marker:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment