Skip to content

Instantly share code, notes, and snippets.

@zazaulola
Last active July 20, 2024 05:25
Show Gist options
  • Save zazaulola/4513d14c2f39b3dd4f549eaac8313aa7 to your computer and use it in GitHub Desktop.
Save zazaulola/4513d14c2f39b3dd4f549eaac8313aa7 to your computer and use it in GitHub Desktop.
#!/bin/bash
# __ __
# ____ ____ _________ ____ ___ ____ / /_ _____/ /_
# /_ / / __ \/ ___/ __ \/ __ `__ \/ __ \/ __/ / ___/ __ \
# / /_/ /_/ / / / /_/ / / / / / / /_/ / /__ (__ ) / / /
# /___/ .___/_/ \____/_/ /_/ /_/ .___/\__(_)____/_/ /_/
# /_/ /_/
PS1='\u@\h \W\$ '
PROMPT_COMMAND=__UPDATE_PROMPT
__UPDATE_PROMPT(){
#
local RETURNED_CODE=$? OK= ER= OF= RL= TL= UL= HL= PL= CS=
local SPLITTER=$(printf "\xee\x82\xb0") #
# OK_MARK - OK mark for result of previous command
# "\xe2\x9c\x93" ✓
# "\xe2\x9c\x94" ✔
# "\xe2\x9c\x85" ✅
local OK_MARK=$(printf "\xe2\x9c\x94") #
# ER_MARK - Error mark for result of previous command
# "\xe2\x9c\x95" ✕
# "\xe2\x9c\x96" ✖
# "\xe2\x9c\x97" ✗
# "\xe2\x9c\x98" ✘
# "\xe2\x9d\x8c" ❌
local ERR_MARK=$(printf "\xe2\x9c\x98") #
local THREE_DOTS="\xe2\x80\xa6" #
# Date format
local DATE_FORMAT='\D{%Y-%m-%dT%H:%M:%S%z}'
# USER_FG - user foreground color
# USER_BG - user background color
local USER_FG=255 USER_BG=33
# ROOT_FG - root foreground color
# ROOT_BG - root background color
local ROOT_FG=255 ROOT_BG=196
# HOSTNAME_FG - hostname foreground color
# HOSTNAME_BG - hostname background color
local HOSTNAME_FG=232 HOSTNAME_BG=46
# CWD_FG - current working directory foreground color
# CWD_BG - current working directory background color
local CWD_FG=232 CWD_BG=228
# OK_FG - ok mark foreground color
local OK_FG=34
# ERR_FG - error mark foreground color
local ERR_FG=196
# DATE_FG - date foreground color
local DATE_FG=255
# IS - invisible start
# IE - invisible end
local IS='\[' IE='\]'
# FG - foreground color
# BG - background color
local FG='\e[38;5;' BG='\e[48;5;' BOLD='\e[1m' ITALIC='\e[3m' RESET='\e[0m'
# MOVE_1_ROW_UP - move cursor up 1 row
# MOVE_TO_RIGHT_BORDER - move cursor to the right border
local MOVE_1_ROW_UP='\e[1A' MOVE_TO_RIGHT_BORDER='\e[$(($COLUMNS-1))G'
{ [[ -v MC_SID ]] || [[ $TERM == linux ]];} && return
# OK result label
OK=$IS$FG$OK_FG'm'$ITALIC$IE$OK_MARK$IS$RESET$IE'\n'
# ERROR result label
ER=$IS'\e[4D'$FG$ERR_FG'm'$IE$(printf %3d\ $RETURNED_CODE)$ERR_MARK$IS$RESET$IE'\n'
# Result of the previous command prefix
OF='\n'$IS$MOVE_1_ROW_UP$MOVE_TO_RIGHT_BORDER$BOLD$IE
# Current time label is finish of the previous command execution
TL=$IS$MOVE_TO_RIGHT_BORDER'\e[30D'$FG$DATE_FG'm\e[2m'$IE'Finish:'$DATE_FORMAT$IS$RESET$IE'\n'
# if previous command was successful then add OK label else add ERROR label
[[ $RETURNED_CODE -eq 0 ]] && RL=$OF$OK || RL=$OF$ER
# CWD - current working directory
# USER - user
# HOSTNAME - hostname
local CWD=`pwd` USER=`whoami` HOSTNAME=`hostname`
[[ $UID -eq 0 ]] && USER_FG=$ROOT_FG && USER_BG=$ROOT_BG
# User label
UL=$IS$FG$USER_FG'm'$BG$USER_BG'm'$IE" $USER "$IS$FG$USER_BG'm'$IE
# Hostname label
HL=$IS$BG$HOSTNAME_BG'm'$IE$SPLITTER$IS$FG$HOSTNAME_FG'm'$IE" $HOSTNAME "$IS$FG$HOSTNAME_BG'm'$IE
# Closure of the prompt
CS=$SPLITTER$IS$RESET$IE' '
# If current working directory is in home directory, replace it with ~
[[ $HOME == ${CWD:0:${#HOME}} ]] && CWD=\~${CWD:${#HOME}}
# Path of CWD label
PL=$IS$BG$CWD_BG'm'$IE$SPLITTER$IS$FG$CWD_FG'm'$BOLD$IE" $CWD "$IS$RESET$FG$CWD_BG'm'$IE
# If COLUMNS is not set, do only left side of the prompt and exit
[[ -z $COLUMNS ]] && PS1=$UL$HL$PL$CS && PS0= && return
# If COLUMNS is set, do right side of the prompt
PS0=$IS$MOVE_TO_RIGHT_BORDER'\e[29D'$FG$DATE_FG'm\e[2m'$IE'Start:'$DATE_FORMAT$IS$RESET$IE'\n'
# 1/3 of the terminal width
THIRD=$(echo "$COLUMNS/3" | bc)
# If current prompt is too long then cut hostname label from prompt
[[ $(echo -n " $USER $HOSTNAME $CWD " | wc -c) -gt $THIRD ]] && HL='' &&
# If current prompt is too long again then cut user label from prompt
[[ $(echo -n " $USER $CWD " | wc -c) -gt $THIRD ]] && UL=$IS$BG$USER_BG'm'$IE' '$IS$FG$USER_BG'm'$IE &&
# If current working directory is too long
[[ $(echo -n " $CWD " | wc -c) -gt $THIRD ]] &&
# Replace some parts of path with three dots
CWD=$(echo $CWD | perl -pe 's/([^\/]{2})[^\/]{2,9}[\/]/\1'$THREE_DOTS'\//g') &&
# And replace the path label
PL=$IS$BG$CWD_BG'm'$IE$SPLITTER$IS$FG$CWD_FG'm'$BOLD$IE" $CWD "$IS$RESET$FG$CWD_BG'm'$IE
# Replace the prompt
PS1=$RL$TL$UL$HL$PL$CS
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment