From 622c0af6f88f2b4bde4868f7542cd0220b6c0f24 Mon Sep 17 00:00:00 2001 From: oli10 <oli10@pcfeia406n.vsb.cz> Date: Sun, 13 Oct 2024 14:01:47 +0200 Subject: [PATCH] script added --- bashrc_prompt | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 bashrc_prompt diff --git a/bashrc_prompt b/bashrc_prompt new file mode 100644 index 0000000..a81e917 --- /dev/null +++ b/bashrc_prompt @@ -0,0 +1,103 @@ +#!/bin/bash + +# binary tput output is converted into string +function tput_to_str() { + TPUT=$(tput $@) + OUT="" + for (( i = 0; i < ${#TPUT}; i++ )); do + local CH=${TPUT:$i:1} + if [[ "${CH}" < " " ]]; then + printf -v CH "\\%03o" "'${CH}" + fi + OUT+=$CH + done + echo $OUT +} + +function prompt() { + #see man terminfo + PS_COLOR_BLACK=0 + PS_COLOR_RED=1 + PS_COLOR_GREEN=2 + PS_COLOR_BROWN=3 + PS_COLOR_BLUE=4 + PS_COLOR_MAGENTA=5 + PS_COLOR_CYAN=6 + PS_COLOR_LGRAY=7 + PS_COLOR_GRAY=8 + PS_COLOR_LRED=9 + PS_COLOR_LGREEN=10 + PS_COLOR_YELLOW=11 + PS_COLOR_LBLUE=12 + PS_COLOR_LMAGENTA=13 + PS_COLOR_LCYAN=14 + PS_COLOR_WHITE=15 + + # Line Graphisc, see terminfo + PS_ACS_HLINE=q + PS_ACS_VLINE=x + PS_ACS_LRCORNER=j + PS_ACS_URCORNER=k + PS_ACS_ULCORNER=l + PS_ACS_LLCORNER=m + + # Default colors (maybe later if[] for white background) + DEF_PS_COLOR_DEF=$PS_COLOR_WHITE + DEF_PS_COLOR_USER=$PS_COLOR_LGREEN + DEF_PS_COLOR_ROOT=$PS_COLOR_LRED + DEF_PS_COLOR_DIR=$PS_COLOR_LCYAN + + # Supported command setf/setaf? + SETF=setf + tput $SETF 0 || SETF=setaf + + PS_COLOR_DEF= + PS_COLOR_USER= + PS_COLOR_ROOT= + PS_COLOR_DIR= + + # colors? + if [[ "${color_prompt}" == yes ]]; then + PS_COLOR_DEF=$(tput_to_str $SETF $PS_COLOR_WHITE) + PS_COLOR_USER=$(tput_to_str $SETF $PS_COLOR_LGREEN) + PS_COLOR_ROOT=$(tput_to_str $SETF $PS_COLOR_LRED) + PS_COLOR_DIR=$(tput_to_str $SETF $PS_COLOR_LCYAN) + fi + + # RED color for root + if [[ "${UID}" == "0" ]]; then + PS_COLOR_USER=${PS_COLOR_ROOT} + fi + + # turn off all atributes + PS_ALL_ATTR_OFF=$(tput_to_str sgr0) + + # Alternate character set + PS_ACS_ON=$(tput_to_str smacs) + PS_ACS_OFF=$(tput_to_str rmacs) + + PS_LINE1="" + PS_LINE2="" + + PS_LINE1=${SSH_TTY:+"\[${PS_ACS_ON}\]${PS_ACS_ULCORNER}${PS_ACS_HLINE}\[${PS_ACS_OFF}\]"} + PS_LINE2=${SSH_TTY:+"\n\[${PS_ACS_ON}\]${PS_ACS_LLCORNER}${PS_ACS_HLINE}${PS_ACS_HLINE}\[${PS_ACS_OFF}\]{"} + + PS_LINE1+="{\[${PS_COLOR_USER}\]\u@\h\[${PS_COLOR_DEF}\]}" + PS_LINE1+="{\[${PS_COLOR_DIR}\]\w\[${PS_COLOR_DEF}\]}" + + PS_LINE2+="\[${PS_ALL_ATTR_OFF}\]\\$" + + #echo "${PS_LINE1}\$(prompt_command)${PS_LINE2} " + echo "${PS_LINE1}\${PS_EXCODE}${PS_LINE2} " +} + +function prompt_command() { + EC=$? + PS_EXCODE="" + if [ $EC -ne 0 ]; then PS_EXCODE="[$EC]"; fi +} + +PROMPT_COMMAND=prompt_command +PS1=$(prompt) + + -- GitLab