Skip to content

Instantly share code, notes, and snippets.

@rpetit3
Created June 7, 2022 18:01
Show Gist options
  • Select an option

  • Save rpetit3/8ea027fc3eadd936ae688164f2066607 to your computer and use it in GitHub Desktop.

Select an option

Save rpetit3/8ea027fc3eadd936ae688164f2066607 to your computer and use it in GitHub Desktop.
bash-full-of-colors with conda support
#!/usr/bin/env bash
# ~/.bashrc: executed by bash(1) for non-login shells.
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
# don't put duplicate lines in the history. See bash(1) for more options
# ... or force ignoredups and ignorespace
HISTCONTROL=ignoredups:ignorespace
# append to the history file, don't overwrite it
shopt -s histappend
# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=10000
HISTFILESIZE=20000
# check the window size after each command and, if necessary,
# update the values of LINES and COLUMNS.
shopt -s checkwinsize
# make less more friendly for non-text input files, see lesspipe(1)
[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"
# 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
# Reset
Color_Off='\e[0m' # Text Reset
# Regular Colors
Black='\e[0;30m' # Black
Grey='\e[0;37m' # Black
Red='\e[0;31m' # Red
Green='\e[0;32m' # Green
Yellow='\e[0;33m' # Yellow
Blue='\e[0;34m' # Blue
Purple='\e[0;35m' # Purple
Cyan='\e[0;36m' # Cyan
White='\e[0;37m' # White
# Bold
BBlack='\e[1;30m' # Black
BRed='\e[1;31m' # Red
BGreen='\e[1;32m' # Green
BYellow='\e[1;33m' # Yellow
BBlue='\e[1;34m' # Blue
BPurple='\e[1;35m' # Purple
BCyan='\e[1;36m' # Cyan
BWhite='\e[1;37m' # White
# Underline
UBlack='\e[4;30m' # Black
UGrey='\e[4;37m' # Black
URed='\e[4;31m' # Red
UGreen='\e[4;32m' # Green
UYellow='\e[4;33m' # Yellow
UBlue='\e[4;34m' # Blue
UPurple='\e[4;35m' # Purple
UCyan='\e[4;36m' # Cyan
UWhite='\e[4;37m' # White
# Background
On_Black='\e[40m' # Black
On_Red='\e[41m' # Red
On_Green='\e[42m' # Green
On_Yellow='\e[43m' # Yellow
On_Blue='\e[44m' # Blue
On_Purple='\e[45m' # Purple
On_Cyan='\e[46m' # Cyan
On_White='\e[47m' # White
# High Intensity
IBlack='\e[0;90m' # Black
IRed='\e[0;91m' # Red
IGreen='\e[0;92m' # Green
IYellow='\e[0;93m' # Yellow
IBlue='\e[0;94m' # Blue
IPurple='\e[0;95m' # Purple
ICyan='\e[0;96m' # Cyan
IWhite='\e[0;97m' # White
# Bold High Intensity
BIBlack='\e[1;90m' # Black
BIRed='\e[1;91m' # Red
BIGreen='\e[1;92m' # Green
BIYellow='\e[1;93m' # Yellow
BIBlue='\e[1;94m' # Blue
BIPurple='\e[1;95m' # Purple
BICyan='\e[1;96m' # Cyan
BIWhite='\e[1;97m' # White
# High Intensity backgrounds
On_IBlack='\e[0;100m' # Black
On_IRed='\e[0;101m' # Red
On_IGreen='\e[0;102m' # Green
On_IYellow='\e[0;103m' # Yellow
On_IBlue='\e[0;104m' # Blue
On_IPurple='\e[0;105m' # Purple
On_ICyan='\e[0;106m' # Cyan
On_IWhite='\e[0;107m' # White
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
HOST_COLOR=${White}
FQDN=`hostname -f`
function __makeTerminalTitle() {
local title=''
local CURRENT_DIR="${PWD/#$HOME/\~}"
if [ -n "${SSH_CONNECTION}" ]; then
title+="`hostname`:${CURRENT_DIR} [`whoami`@${FQDN}]"
else
title+="${CURRENT_DIR} [`whoami`]"
fi
echo -en '\033]2;'${title}'\007'
}
function __getMachineId() {
if [ -f /etc/machine-id ]; then
echo $((0x$(cat /etc/machine-id | head -c 15)))
else
echo $(( (${#HOSTNAME}+0x$(hostid))))
fi
}
function __makePS1() {
local EXIT="$?"
if [ ! -n "${HOST_COLOR}" ]; then
local H=$(__getMachineId)
HOST_COLOR=$(tput setaf $((H%5 + 2))) # foreground
fi
PS1=''
PS1+="${debian_chroot:+($debian_chroot)}"
if [ -n "${VIRTUAL_ENV}" ]; then
local VENV=`basename $VIRTUAL_ENV`
PS1+="\[${BWhite}\](${VENV})\[${Color_Off}\]" # show virtualenv if in it
fi
if [ -n "${CONDA_PROMPT_MODIFIER}" ]; then
PS1+="\[${BWhite}\]${CONDA_PROMPT_MODIFIER}\[${Color_Off}\]" # show conda env if in it
fi
# git branch
if [ -x "`which git 2>&1`" ]; then
local branch="$(git name-rev --name-only HEAD 2>/dev/null)"
if [ -n "${branch}" ]; then
local git_status="$(git status --porcelain -b 2>/dev/null)"
local letters="$( echo "${git_status}" | grep --regexp=' \w ' | sed -e 's/^\s\?\(\w\)\s.*$/\1/' )"
local untracked="$( echo "${git_status}" | grep -F '?? ' | sed -e 's/^\?\(\?\)\s.*$/\1/' )"
local status_line="$( echo -e "${letters}\n${untracked}" | sort | uniq | tr -d '[:space:]' )"
PS1+="\[${BGreen}\]{${branch}"
if [ -n "${status_line}" ]; then
PS1+=" ${status_line}"
fi
PS1+="}\[${Color_Off}\] "
fi
fi
if [ ${USER} == root ]; then
PS1+="\[${Red}\]" # root
elif [ ${USER} != ${LOGNAME} ]; then
PS1+="\[${Grey}\]" # normal user
else
PS1+="\[${Grey}\]" # normal user
fi
PS1+="\u\[${Color_Off}\]"
if [ -n "${SSH_CONNECTION}" ]; then
PS1+="\[${White}\]@"
PS1+="\[${UWhite}${HOST_COLOR}\]\h\[${Color_Off}\]" # host displayed only if ssh connection
fi
PS1+=":\[${Cyan}\]\w" # working directory
PS1+="\[${BCyan}\]\\$\[${Color_Off}\] " # prompt
__makeTerminalTitle
}
if [ "$color_prompt" = yes ]; then
PROMPT_COMMAND=__makePS1
PS2="\[${BPurple}\]>\[${Color_Off}\] " # continuation prompt
else
PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt
# enable color support of ls and also add handy aliases
if [ -x /usr/bin/dircolors ]; then
test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
alias dir='dir --color=auto'
alias vdir='vdir --color=auto'
alias grep='grep --color=auto'
alias fgrep='fgrep --color=auto'
alias egrep='egrep --color=auto'
fi
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
if [ -f ~/.bash_local ]; then
. ~/.bash_local
fi
umask 022
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
# On first login, setup conda
CONDA_SETUP=1
if [ $CONDA_SETUP -eq 0 ]; then
# Try to setup conda
if [ -d "$HOME/miniconda3" ]; then
echo "$HOME/miniconda3 exists, is it working? If so set the value of CONDA_SETUP to 1 in your ~/.bashrc file."
else
# Install miniconda3
wget -O $HOME/miniconda3.sh https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash $HOME/miniconda3.sh -b -u -p $HOME/miniconda3
$HOME/miniconda3/bin/conda upgrade conda -y
rm $HOME/miniconda3.sh
sed -i "s/CONDA_SETUP=1/CONDA_SETUP=1/" $HOME/.bashrc
fi
fi
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$("$HOME/miniconda3/bin/conda" 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "$HOME/miniconda3/etc/profile.d/conda.sh" ]; then
. "$HOME/miniconda3/etc/profile.d/conda.sh"
else
export PATH="$HOME/miniconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment