From 3c63b5d2d695dea40288cccf96b886a34a818a03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=9F=E9=85=8C=20=E9=B5=AC=E5=85=84?= Date: Mon, 15 Aug 2022 21:21:35 +0800 Subject: [PATCH] Simplify PS1 logics --- bash/rbashrc | 42 +++++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/bash/rbashrc b/bash/rbashrc index 7a761a9..b9b9a95 100644 --- a/bash/rbashrc +++ b/bash/rbashrc @@ -14,15 +14,23 @@ RBASH_ENV="$RBASH_HOME/env" RBASH_REMOTE="https://git.k8s.astropenguin.net" RBASH_REPO="penguin/utils" RBASH_PATH="bash" +RBASH_MERGED="$RBASH_SOURCES/merged" + +RHOSTNAME="" +RDOMAIN="" +RCOLOR="" + +if [ $RHOSTNAME == "" ]; then + RHOSTNAME=$( hostname -s ) + RDOMAIN="@"$( hostname -d ) + RCOLOR=$(( $RANDOM * 6 / 32767 + 1 )) +fi # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi -# The merged sources -RBASH_MERGED="$RBASH_SOURCES/merged" - # don't put duplicate lines or lines starting with space in the history. # See bash(1) for more options HISTCONTROL=ignoreboth @@ -156,28 +164,24 @@ function rbash-reload { function rbash-upgrade { echo "Updating $__SCRIPT" - local TMPFILE + local TMPFILE _P_HOSTNAME _P_DOMAIN _P_COLOR TMPFILE=$( mktemp ) __download "$RBASH_REMOTE/$RBASH_REPO/raw/branch/master/$RBASH_PATH/rbashrc" > $TMPFILE - if [ -z "$1" ]; then - MC_NAME="" - MCC_NAME="" - else - MC_NAME=$1 - MCC_NAME=$2 + if [ -n "$1" ]; then + RHOSTNAME=$1 + RDOMAIN=$2 fi - PMC_NAME=$( echo "#_MACHINE_NAME_#" | sed -e "s/#_//g" ) - PMCC_NAME=$( echo "#_MACHINE_COLORED_NAME_#" | sed -e "s/#_//g" ) - COLOR_CODE_TOK=$( echo "#_COLOR_CODE_#" | sed -e "s/#_//g" ) - COLOR_CODE=$(( $RANDOM * 6 / 32767 + 1 )) + _P_HOSTNAME="" + _P_DOMAIN="" + _P_COLOR_CODE="" sed -i \ - -e "s/$PMC_NAME/$MC_NAME/g" \ - -e "s/$PMCC_NAME/$MCC_NAME/g" \ - -e "s/$COLOR_CODE_TOK/$COLOR_CODE/g" \ + -e "s/$_P_HOSTNAME/$RHOSTNAME/g" \ + -e "s/$_P_DOMAIN/$RDOMAIN/g" \ + -e "s/$_P_COLOR_CODE/$RCOLOR/g" \ $TMPFILE mv $TMPFILE $__SCRIPT @@ -285,7 +289,7 @@ function r2dissource { rm -r "$_dis" "$RBASH_SOURCES" } -export PS1='This is \e[1;3m\e[0m: \w\n\$ ' +export PS1="This is ${RHOSTNAME}\e[1;3${RCOLOR}m${RDOMAIN}\e[0m: \w\n\$ " export EDITOR=vim # Source for custom bash env, allows overriding @@ -305,5 +309,5 @@ ___DEFAULT___ chmod 600 "$RBASH_CONFIG" fi -source "$RBASH_CONFIG" +ource "$RBASH_CONFIG" unset _RBASH_MERGE_START