Last active
July 26, 2022 20:56
-
-
Save budRich/b1b0c4e8d882b78426ab720af5b1726b to your computer and use it in GitHub Desktop.
Display a colorful compact prompt with runtime of last command and a compact working directory.
This file contains 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
# PS1 = time compact_pwd > | |
# curated by budRich since 2017 | |
# ============================= | |
# | |
# display runtime of last command and a compat PWD | |
# default time output is ms, if time is more then | |
# 1000ms, seconds is displayed instead. | |
# color of time is changed depending on the value. | |
# the compact PWD, displays the first character of | |
# each folder in PWD. If first character is '.', | |
# display the second character instead. | |
# examples | |
# -------- | |
# `cd /usr/share` | |
# 009 /u/s > | |
# `cd ~/.config` | |
# 012 ~/c > | |
# screenshot | |
# ---------- | |
# https://i.imgur.com/6yTX79Y.png | |
# inspiration | |
# ----------- | |
# [Ville Laurikari](https://stackoverflow.com/a/1862762) | |
# [Henrik Nyh](https://github.com/henrik/dotfiles/blob/master/bash/prompt.sh) | |
# source | |
# ------ | |
# https://gist.github.com/budRich/b1b0c4e8d882b78426ab720af5b1726b | |
timer_start() { | |
timer=${timer:-$(date +%s%N | cut -b1-13)} | |
} | |
prompt_generator() { | |
# regular colors | |
local K="\[\033[0;30m\]" # black | |
local R="\[\033[0;31m\]" # red | |
local G="\[\033[0;32m\]" # green | |
local Y="\[\033[0;33m\]" # yellow | |
local B="\[\033[0;34m\]" # blue | |
local M="\[\033[0;35m\]" # magenta | |
local C="\[\033[0;36m\]" # cyan | |
local W="\[\033[0;37m\]" # white | |
# emphasized (bolded) colors | |
local BK="\[\033[1;30m\]" | |
local BR="\[\033[1;31m\]" | |
local BG="\[\033[1;32m\]" | |
local BY="\[\033[1;33m\]" | |
local BB="\[\033[1;34m\]" | |
local BM="\[\033[1;35m\]" | |
local BC="\[\033[1;36m\]" | |
local BW="\[\033[1;37m\]" | |
# reset | |
local RESET="\[\033[0;37m\]" | |
# get time | |
local milli=$(($(date +%s%N | cut -b1-13) - $timer)) | |
case $(( | |
milli >= 0 && milli <= 20 ? 1 : | |
milli > 20 && milli <= 100 ? 2 : | |
milli > 100 && milli <= 250 ? 3 : | |
milli > 250 && milli <= 500 ? 4 : | |
milli > 500 && milli <= 999 ? 5 : | |
milli > 999 && milli <= 2000 ? 6 : 7)) in | |
(1) psc="${G}" ;; | |
(2) psc="${Y}" ;; | |
(3) psc="${C}" ;; | |
(4) psc="${B}" ;; | |
(5) psc="${M}" ;; | |
(6) psc="${R}" milli=$((milli/1000));; | |
(7) psc="${R}" milli=$((milli/1000));; | |
esac | |
# pad ms or seconds with zeroes, so string length always is 3. | |
local timmy=$(printf "%03d" $milli) | |
# create a string with the first charcter of each folder in path | |
local pth="$(pwd)" | |
pth=${pth/${HOME}/'~'} | |
local newpth="" | |
apa=( ${pth//\//' '} ) | |
for d in ${apa[@]}; do | |
[[ $d = '~' ]] && newpth='~' && continue | |
# if first char is '.', grab the second | |
[[ ${d:0:1} = '.' ]] \ | |
&& newpth+="/${d:1:1}" || newpth+="/${d:0:1}" | |
done | |
PS1="${psc}${timmy} ${W}${newpth} ${B}>${RESET} " | |
unset timer | |
} | |
# https://unix.stackexchange.com/a/65312 | |
trap 'timer_start' DEBUG | |
PROMPT_COMMAND=prompt_generator | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment