Skip to content

Instantly share code, notes, and snippets.

@hiway
Last active May 12, 2022 21:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiway/1dfeeb9b8474d59afb7c80ea1e96435c to your computer and use it in GitHub Desktop.
Save hiway/1dfeeb9b8474d59afb7c80ea1e96435c to your computer and use it in GitHub Desktop.
My sshrc dofile to carry a custom bash prompt to remote *nix with ssh/mosh access.
#!/usr/bin/env bash
##
## hiwaysshrc.sh
## --------------------------------------------------
##
## Install:
##
## $ ln -s $(pwd)/hiwaysshrc.sh ~/.sshrc
## $ echo "source ~/.sshrc" >> ~/.bash_profile
##
## --------------------------------------------------
## Requires:
## https://github.com/Russell91/sshrc
##
## Uncomment the next line to enable debug mode.
# set -x
## User settings
## --------------------------------------------------
# Set your preferred timezone here.
export TZ="Asia/Kolkata"
## Aliases
## --------------------------------------------------
# Original commands are accessible
# by prefixing the alias with a backslash (ex: $ \ssh ).
alias ssh="sshrc"
alias mosh="moshrc"
alias sudp="sudo_with_prompt" # think "sudo -s"
# Navigation
alias cds="cd ~/source/"
alias cdsc="cd ~/source/community"
# Development
alias ggf="git clone" # Git clone.
alias ggs="git clone --depth 1" # Shallow git clone.
## Shortcuts
## --------------------------------------------------
alias l="ls -cAGt"
alias ll="ls -cAGtl"
alias llg="ls -cAGtl|grep"
## Defaults and automatic settings
## --------------------------------------------------
SSHRC="$BASH_SOURCE"
USER_ID=$( id -ur )
## Text / Strings Helper Functions
## --------------------------------------------------
function text_case_lower { tr '[A-Z]' '[a-z]'; }
function text_case_upper { tr '[a-z]' '[A-Z]'; }
## ANSI formatting codes
## --------------------------------------------------
# ANSI support is limited in tput on FreeBSD.
# Prefer raw codes.
ansi_reset="\033[0;0m"
ansi_fmt_superuser="\033[7;8m\033[31;47m"
ansi_fmt_hostname="\033[0;34m"
ansi_fmt_icons="\033[2;30m"
ansi_fmt_clock="\033[0;34m"
ansi_fmt_workdir="\033[3;3m\033[26;47m"
ansi_fmt_prompt_char="\033[0;38m"
ansi_fmt_prompt_char_root="\033[0;31m"
## Shell Helper Functions
## --------------------------------------------------
function previous_command_exit_status {
local _exit_status="$?"
if [ $_exit_status -eq 0 ]; then
echo -n "";
else
echo -n "$ansi_fmt_superuser$_exit_status$ansi_reset";
fi
}
function sudo_with_prompt {
\sudo bash --rcfile $SSHRC
}
function mkurl-scp {
# expects a single filename as argument.
echo "scp $( whoami )@$( hostname ):$( pwd )/$1 ./$1"
}
## Prompt
## --------------------------------------------------
function format_identity {
if [ $1 -eq 0 ]; then
echo -n "$ansi_fmt_superuser$2$ansi_reset@$ansi_fmt_hostname$3$ansi_reset";
else
echo -n "$2$ansi_reset@$ansi_fmt_hostname$3$ansi_reset"
fi
}
function format_clock {
# Latest Unicode emoji may not work with mosh and hence moshrc.
# See: https://github.com/mobile-shell/mosh/issues/234
# echo -n "🇮🇳 $ansi_fmt_clock$_time_tz$ansi_reset"
# Using codepoints that work through mosh:
echo -n "$ansi_fmt_icons"
echo -n "$ansi_fmt_clock\D{%H:%M}$ansi_reset"
}
function format_utc_clock {
# Latest Unicode emoji may not work with mosh and hence moshrc.
# See: https://github.com/mobile-shell/mosh/issues/234
# echo -n "🌍 $ansi_fmt_clock$( date -u +%H:%M )$ansi_reset"
# Using codepoints that work through mosh:
echo -n "$ansi_fmt_icons"
echo -n "$ansi_fmt_clock$( date -u +%H:%M )$ansi_reset"
}
function format_pwd {
local _pwd="\w"
echo -n "$ansi_fmt_workdir$_pwd$ansi_reset"
}
function format_prompt_char {
if [ $1 -eq 0 ]; then
echo -n "$ansi_fmt_prompt_char_root# $ansi_reset";
else
echo -n "$ansi_fmt_prompt_char$ $ansi_reset";
fi
}
function format_prompt {
# We first store previous command's exit status,
# then run other commands, since executing any command
# before capturing this variable will overwrite it!
local _prev_exit_status="$( previous_command_exit_status )"
local _username="\u"
local _hostname="$( hostname -s | text_case_lower )"
local _clock="$( format_clock ) $( format_utc_clock )"
local _identity="$( format_identity $USER_ID $_username $_hostname )"
local _working_directory="$( format_pwd )"
# Compose the prompt
local _prompt_line_1="$_prev_exit_status"
local _prompt_line_2="$_clock $_identity:$_working_directory"
local _prompt_char="$(format_prompt_char $USER_ID)"
# Echo prompt to allow `prompt_command` to grab the output
# and set as PS1.
echo "$_prompt_line_1\r\n$_prompt_line_2\r\n$_prompt_char"
}
function prompt_command {
PS1="$(format_prompt)"
}
# `prompt_command` will be called when a bash prompt
# is about to be displayed. This works for user shell,
# however doen not work if you initiate a sudo shell.
# See `sudo_with_prompt` under aliases for workaround.
PROMPT_COMMAND=prompt_command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment