Merge branch 'prompt'

This commit is contained in:
Stefan Gaiselmann 2021-02-13 10:46:37 +01:00
commit d2df48fa32
4 changed files with 169 additions and 105 deletions

View File

@ -1,8 +1,10 @@
{{- $name := promptString "name" -}} {{- $name := promptString "name" -}}
{{- $email := promptString "email" -}} {{- $email := promptString "email" -}}
{{- $category := promptString "Server category" -}} {{- $category := promptString "Server category" -}}
{{- $color := promptString "Prompt color (see https://unix.stackexchange.com/questions/269077/tput-setaf-color-table-how-to-determine-color-codes)" -}}
[data] [data]
name = "{{ $name }}" name = "{{ $name }}"
email = "{{ $email }}" email = "{{ $email }}"
bash.prompt.category = "{{ $category }}" bash.prompt.category = "{{ $category }}"
bash.prompt.color = "{{ $color }}"

View File

@ -0,0 +1,135 @@
#
# System Name and type detectedn
#
mySystem=$(uname -n)
myFQDN="$(uname -n).$(dnsdomainname)"
myCategory="{{.bash.prompt.category}}"
myColor="{{.bash.prompt.color}}"
myType="{{.chezmoi.hostname}}"
{{ if eq .chezmoi.os "windows" -}}
myPrettyName="{{.chezmoi.os}}"
{{ else -}}
myPrettyName="{{.chezmoi.osRelease.prettyName}}"
{{ end -}}
myNum=0
# Helper functions go set colors with tput
function colf() {
local color=$1
tput setaf "$color"
}
function colb() {
local color=$1
tput setab "$color"
}
function res() {
tput sgr0
}
# last exit code
function lastExitCode {
local exit="$?"
if [ $exit != 0 ];then
# printf '\e[38;5;160m%-5s' "✕ ❗ ($exit)"
echo "$(colf 1)❗($exit)"
else
echo '✔'
fi
}
# Color sequences must be escaped with '\[ ... \]
# See https://unix.stackexchange.com/questions/105958/terminal-prompt-not-wrapping-correctly
function hostPrompt() {
local system=$1
echo "\[$(colf "$PROMPT_COLOR")\]░▒▓\[$(colb "$PROMPT_COLOR")\]\[$(colf 7)$(tput bold)\] $system \[$(res)$(colf "$PROMPT_COLOR")$(colb 7)\]▓▒░ \h"
}
function statusPrompt() {
local type=$1
echo "\[$(colf "$PROMPT_COLOR")\]  \$(lastExitCode)\[$(colf "$PROMPT_COLOR")\]  $type \[$(res)\]\[$(colf 7)\]▓▒░"
}
function userPrompt() {
echo "\[$(res)\]\[$(colf 2)\]\u@\h \[$(colf 1)\]☆ \[$(colf 2)\]\w \[$(colf 1)\]☆ \[$(colf "$PROMPT_COLOR")\]\$(parse_git_branch) "
}
function datePrompt() {
echo "\[$(colf 2)\]\D{%Y-%m-%d %H:%M:%S}"
#echo "\[$(colf 2) \]"
}
function setPrompt() {
local category=$1
local type=$2
PROMPT_COLOR=$3
SYSTEM_PROMPT="$(hostPrompt "$category")"
TYPE_PROMPT="$(statusPrompt "$type")"
USER_PROMPT="$(userPrompt) "
DATE="$(datePrompt)"
PROMPT="${SYSTEM_PROMPT} ${TYPE_PROMPT}\n${USER_PROMPT}\n${DATE}"
PS1="${PROMPT} → \[$(res)\]"
}
# example from http://stackoverflow.com/questions/4133904/ps1-line-with-git-current-branch-and-colors
function color_my_prompt {
local __user_and_host="\[\033[01;32m\]\u@\h"
local __cur_location="\[\033[01;34m\]\w"
local __git_branch_color="\[\033[31m\]"
#local __git_branch="\`ruby -e \"print (%x{git branch 2> /dev/null}.grep(/^\*/).first || '').gsub(/^\* (.+)$/, '(\1) ')\"\`"
local __git_branch='`git branch 2> /dev/null | grep -e ^* | sed -E s/^\\\\\*\ \(.+\)$/\(\\\\\1\)\ /`'
local __prompt_tail="\[\033[35m\]$"
local __last_color="\[\033[00m\]"
export PS1="$__user_and_host $__cur_location $__git_branch_color$__git_branch$__prompt_tail$__last_color "
}
function parse_git_branch {
# echo -n $(git branch --no-color 2>/dev/null | awk -v out=$1 '/^*/ { if(out=="") print $2; else print out}')
git_branch="$(git branch 2> /dev/null | grep -e ^* | sed -E s/^\\\*\ \(.+\)$/\(\ \\\1\)\ /)"
if [ -z "$git_branch" ];
then
git_branch="(no git branch) "
else
local git_dir="$(git rev-parse --git-dir)"
if [ -f $git_dir/logs/refs/stash ];
then
local git_stash="$(wc -l $git_dir/logs/refs/stash | sed -E 's/^([0-9]+).*/\1/')"
else
local git_stash="0"
fi
if [ $git_stash -gt 0 ];
then
git_branch=$(echo $git_branch | sed -E "s/^(.*)\)$/\1\|stash:${git_stash}\)/")
fi
fi
echo -n -e "$git_branch"
}
# fast version form https://stackoverflow.com/questions/4485059/git-bash-is-extremely-slow-on-windows-7-x64
fast_git_ps1 ()
{
printf -- "$(git branch 2>/dev/null | sed -ne '/^\* / s/^\* \(.*\)/ [\1] / p')"
}
# Set backgroud color of prompt, default to magenta
# Color #define Value RGB
# black COLOR_BLACK 0 0, 0, 0
# red COLOR_RED 1 max,0,0
# green COLOR_GREEN 2 0,max,0
# yellow COLOR_YELLOW 3 max,max,0
# blue COLOR_BLUE 4 0,0,max
# magenta COLOR_MAGENTA 5 max,0,max
# cyan COLOR_CYAN 6 0,max,max
# white COLOR_WHITE 7 max,max,max
setPrompt "${myCategory}" "${myPrettyName}" "${myColor:-5}"
if [[ -z "${myType// }" ]]; then
TYPE=$(uname -n)
else
TYPE=" $myType "
fi
setPrompt "${myCategory}" "${myPrettyName}" ${1:-6}
# color_my_prompt
# TODO: background colors...
# TODO: private history file for shared accounts
# TODO: vim

View File

@ -1,91 +0,0 @@
#
# System Name and type detectedn
#
mySystem=$(uname -n)
myFQDN="$(uname -n).$(dnsdomainname)"
myCategory="{{.bash.prompt.category}}"
myType="{{.chezmoi.hostname}}"
{{ if eq .chezmoi.os "windows" -}}
myPrettyName="{{.chezmoi.os}}"
{{ else -}}
myPrettyName="{{.chezmoi.osRelease.prettyName}}"
{{ end -}}
myNum=0
# last exit code
function lastExitCode {
local exit="$?"
if [ $exit != 0 ];then
printf '\e[38;5;160m%-5s' "✕ ❗ ($exit)"
else
printf '✔'
fi
}
# sourcing Solarized Theme is already done in .bashrc.d
echo "mySystem: $mySystem"
echo "myFQDN: $myFQDN"
bg_type="\[\e[48;5;254m\]"
bg_reset="\[\e[49m\]"
fg_reset="\[\e[39m\]"
fg_system="${sol_base3}"
{{- if eq .bash.prompt.category "Materna" }}
# Materna cyan
bg_system="${sol_bg_cyan}"
fg_type="${sol_cyan}"
{{- else if eq .bash.prompt.category "AWS" }}
# AWS specific color
bg_system="${sol_bg_orange}"
fg_type="${sol_orange}"
{{- else }}
# Default color
bg_system="${sol_bg_magenta}"
fg_type="${sol_magenta}"
{{- end }}
bg_type="${sol_bg_base3}"
fg_arrow1="${fg_type}"
fg_arrow2="${sol_base3}"
if [[ -z "${myType// }" ]]; then
TYPE=$(uname -n)
else
TYPE=" $myType "
fi
if [ "$myNum" != "0" ]; then
TYPE="$TYPE ($myNum)"
fi
echo "System Prompt: $SYSTEM_PROMPT"
SYSTEM_PROMPT="\n${fg_arrow1}░▒▓${bg_system} ${fg_system}${myCategory} ${bg_type}${fg_arrow1}▓▒░"
TYPE_PROMPT="$TYPE  \$(lastExitCode) ${fg_arrow1}$myPrettyName ${sol_blue}\${STY#[0-9]*.} ${bg_reset}${fg_arrow2}▓▒░"
PROMPT="${SYSTEM_PROMPT} ${TYPE_PROMPT}"
DATE="\[\e[0;32m\]\d \t"
if [ "$(whoami)" = "root" ]; then
DIR="\[\e[97;49m\]\[\e[0;91;107m\] \u@\h \[\e[97;49m\]\[\e[39;49m\] in \[\e[0;32m\]\w"
else
DIR="\[\e[0;32m\]\u@\h \[\e[31m\]☆ \[\e[0;32m\]\w"
fi
# PROMPT_COMMAND=promptcmd
PS1="${PROMPT}\[\e[0m\] \n${DIR}\n${DATE} → \[\e[0m\]"
if [ $(whoami | grep root | wc -l) == 1 ]; then
echo -e "\e[31;107m┌─────────────────────────────┐\e[0m"
echo -e "\e[31;107m│ YOU ARE ROOT, BE CAREFUL! │\e[0m"
echo -e "\e[31;107m└─────────────────────────────┘\e[0m"
# Test mintty
echo -ne '\e]11;44,10,10\a'
fi
# TODO: background colors...
# TODO: private history file for shared accounts
# TODO: vim

View File

@ -14,17 +14,17 @@
ll = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]%Creset%Cgreen\\ (%cd)" --decorate --numstat --date=relative ll = log --pretty=format:"%C(yellow)%h%Cred%d\\ %Creset%s%Cblue\\ [%cn]%Creset%Cgreen\\ (%cd)" --decorate --numstat --date=relative
ld = log --pretty=format:"%C(yellow)%h\\ %C(green)%ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short --graph ld = log --pretty=format:"%C(yellow)%h\\ %C(green)%ad%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=short --graph
ls = log --pretty=format:"%C(green)%h\\ %C(yellow)[%ad]%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=relative ls = log --pretty=format:"%C(green)%h\\ %C(yellow)[%ad]%Cred%d\\ %Creset%s%Cblue\\ [%cn]" --decorate --date=relative
# Short summary of commiters # Short summary of commiters
summary = shortlog --summary --numbered --email summary = shortlog --summary --numbered --email
# https://stackoverflow.com/questions/5188320/how-can-i-get-a-list-of-git-branches-ordered-by-most-recent-commit # https://stackoverflow.com/questions/5188320/how-can-i-get-a-list-of-git-branches-ordered-by-most-recent-commit
# list remote branches ordered by date # list remote branches ordered by date
rr = for-each-ref --sort=-committerdate refs/remotes/ --count=20 --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))' rr = for-each-ref --sort=-committerdate refs/remotes/ --count=20 --format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'
# show tracked files # show tracked files
ls-tracked = ls-tree -r --name-only HEAD ls-tracked = ls-tree -r --name-only HEAD
# show number of commits per author # show number of commits per author
# https://stackoverflow.com/questions/9839083/git-number-of-commits-per-author-on-all-branches # https://stackoverflow.com/questions/9839083/git-number-of-commits-per-author-on-all-branches
stats = shortlog -s -n --all --no-merges stats = shortlog -s -n --all --no-merges
@ -45,26 +45,44 @@
ss = status -s --branch -uno ss = status -s --branch -uno
co = checkout co = checkout
pr = remote prune origin pr = remote prune origin
# from pixelbeat # from pixelbeat
brdate = for-each-ref --sort=-committerdate --format='%(committerdate:iso8601) %(refname:short)' refs/heads/ brdate = for-each-ref --sort=-committerdate --format='%(committerdate:iso8601) %(refname:short)' refs/heads/
st = stash list --date=local st = stash list --date=local
alias = ! git config --get-regexp ^alias\\. | sed -e s/^alias\\.// -e s/\\ /\\ =\\ / alias = ! git config --get-regexp ^alias\\. | sed -e s/^alias\\.// -e s/\\ /\\ =\\ /
d = diff -w d = diff -w
dc = diff -w --cached dc = diff -w --cached
# from https://www.toptal.com/developers/gitignore # from https://www.toptal.com/developers/gitignore
# create .gitignore templates based on operationg systems, IDE, languages etc. # create .gitignore templates based on operationg systems, IDE, languages etc.
ignore = "!gi() { curl -sL https://www.toptal.com/developers/gitignore/api/$@ ;}; gi" ignore = "!gi() { curl -sL https://www.toptal.com/developers/gitignore/api/$@ ;}; gi"
[core] [core]
editor = vim editor = vim
[push] [push]
# default = matching # default = matching
default = simple # safer option, push only current branch default = simple # safer option, push only current branch
[pull]
ff = only
{{ if eq .chezmoi.os "windows" -}}
[merge] [merge]
tool = p4merge tool = p4merge
ff = false ff = false
[pull]
ff = only [mergetool "p4merge"]
path = "C:\\Program Files\\Perforce\\p4merge.exe"
cmd = p4merge $BASE $LOCAL $REMOTE $MERGED
keepBackup = false
[difftool "p4merge"]
external = p4merge $LOCAL $REMOTE
[difftool "winmerge"]
cmd = "'c:/PortableApps/PortableApps/WinMergePortable/App/WinMerge/WinMergeU'" -e "$LOCAL" "$REMOTE"
{{ end -}}
[http]
# proxy = http://host:port