Last active
December 24, 2015 23:09
-
-
Save nyteshade/6877454 to your computer and use it in GitHub Desktop.
This is a tool to create a nice UNIX style prompt in full color. Designed for dark backgrounds and a tango color scheme.#!/bin/sh
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
#!/bin/bash | |
# Function to determine if this script is being sourced | |
# or not. | |
function isSourced() { | |
if [ "${FUNCNAME[1]}" = source ]; then | |
echo "true" | |
else | |
echo "false" | |
fi | |
} | |
sourced=$( isSourced ) | |
# We cannot determine any command line parameters if we | |
# are sourced from another script. | |
if [ "${sourced}" = "true" ]; then | |
this='<sourced>' | |
else | |
this="$( cd `dirname ${0}` >/dev/null; pwd )/$(basename ${0})" | |
fi | |
# Detect whether or not we have node.js in the path | |
set hasNode="" | |
[[ "${hasNode}N" = "N" ]] && hasNode=false || hasNode=true | |
function getGitBranch() { | |
local cur_branch="$(git branch 2>/dev/null | sed -n 's|\*\{1\}[ ]\(.*\)|\1|p')" | |
if [ "${cur_branch}n" != "n" ]; then | |
printf "\x1b[0m \x1b[1m(\x1b[90m${cur_branch}\x1b[0m\x1b[1m)\x1b[0m" | |
fi | |
} | |
function getHgBranch() { | |
local cur_branch="$(hg bookmark 2>/dev/null | sed -n 's|[ ]*\*\{1\}[ ]\(\w*\).*|\1|p')" | |
if [ "${cur_branch}n" != "n" ]; then | |
printf "\x1b[0m \x1b[1m(\x1b[90m${cur_branch}\x1b[0m\x1b[1m)\x1b[0m" | |
fi | |
} | |
function getIPPython () { | |
if [ -e /tmp/ip.txt ]; then | |
cat /tmp/ip.txt | |
else | |
python -c 'import socket;s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM);s.connect(("google.com",80));print s.getsockname()[0]' > /tmp/ip.txt | |
cat /tmp/ip.txt | |
fi | |
} | |
function getIPNode () { | |
CLI="\ | |
function getIPAddress() {\ | |
var interfaces = require('os').networkInterfaces();\ | |
for (var devName in interfaces) {\ | |
var iface = interfaces[devName];\ | |
for (var i = 0; i < iface.length; i++) {\ | |
var alias = iface[i];\ | |
if (alias.family === 'IPv4' && alias.address !== '127.0.0.1' \ | |
&& !alias.internal)\ | |
return alias.address;\ | |
}\ | |
}\ | |
return '0.0.0.0';\ | |
}\ | |
console.log(getIPAddress());\ | |
" | |
node -e "${CLI}" | |
} | |
function getIP () { | |
if [ "$(which node 2>/dev/null)z" = "z" ]; then | |
getIPPython | |
else | |
getIPNode | |
fi | |
} | |
function getPrompt () { | |
if [ $(id -u) = "0" ]; then | |
#printf "#" | |
printf "\xf0\x9f\x8c\x80 " | |
else | |
#printf "$" | |
printf "\xe2\x9c\xa8 " | |
fi | |
} | |
# If the file is being sourced from another shell script, then set the PS1 | |
# prompt and exit. Otherwise, act as a command line tool. The first parameter | |
# determines the functionality when executed in that mode. | |
if [ "$(isSourced)" = "true" ]; then | |
export PS1='\r\n\[\e[1m\e[33m\]\u\[\e[0m@\e[1m\e[31m\]\H\[\e[0m\]/\[\e[1m\e[34m\]`getIP``getGitBranch``getHgBranch`\[\e[0m\]\r\n@\T \[\e[1m\e[32m\]\w\[\e[0m\]\r\n`getPrompt` ' | |
return 2>/dev/null || exit | |
elif [ "${#}" -lt 1 ]; then | |
TOOL=$( basename ${0} ) | |
printf "\nUsage: ${TOOL} <getIP|getPrompt|getGitBranch|install>\n" | |
printf "\tgetIP - returns the ip address of this machine\n" | |
printf "\tgetPrompt - returns \$ for normal and # for root users\n" | |
printf "\tgetGitBranch - returns the current branch if in a git repo dir\n" | |
printf "\tgetHgBranch - returns the current bookmark if in a hg repo dir\n" | |
printf "\tinstall - installs command line tools for the above tools\n\n" | |
printf "NOTE: Tools are installed to /usr/local/bin by default. In order\n" | |
printf " install them into another directory, make the call with\n" | |
printf " \x1b[1mINSTALL_DIR=/path/to/bin ${TOOL} install\x1b[0m\n" | |
printf " in order to change that path.\n\n" | |
return 2>/dev/null || exit | |
elif [ "${1}" = "getIP" ]; then | |
getIP | |
elif [ "${1}" = "getPrompt" ]; then | |
getPrompt | |
elif [ "${1}" = "getGitBranch" ]; then | |
getGitBranch | |
elif [ "${1}" = "install" ]; then | |
export INSTALL_DIR=/usr/local/bin | |
printf >${INSTALL_DIR}/getIP << EOF "#!/bin/sh | |
${this} getIP" | |
EOF | |
chmod +x ${INSTALL_DIR}/getIP | |
printf >${INSTALL_DIR}/getPrompt << EOF "#!/bin/sh | |
${this} getPrompt" | |
EOF | |
chmod +x ${INSTALL_DIR}/getPrompt | |
printf >${INSTALL_DIR}/getGitBranch << EOF "#!/bin/sh | |
${this} getGitBranch" | |
EOF | |
chmod +x ${INSTALL_DIR}/getGitBranch | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment