Move prompt display to oh-my-posh

and reduce shell-integrated fallback-prompt to minimum

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2022-09-20 15:04:55 +02:00
parent 86dc403cf6
commit 0b789b5a8a
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
2 changed files with 152 additions and 105 deletions

View file

@ -18,104 +18,6 @@ function box_name() {
[ -f ~/.box-name ] && cat ~/.box-name || echo ${SHORT_HOST:-$HOST} [ -f ~/.box-name ] && cat ~/.box-name || echo ${SHORT_HOST:-$HOST}
} }
function git_describe() {
git describe --tags 2>/dev/null || git rev-parse --short HEAD 2>/dev/null || printf '\ue701'
}
function awsenv_prompt() {
local pr=$(awsenv prompt 2>/dev/null)
[ -z "${pr}" ] || echo "${bracket_open} ${pr} ${bracket_close}"
}
function config-git_prompt() {
if [ -f ~/bin/config-git-status.sh ]; then
if ! (~/bin/config-git-status.sh); then
echo "${bracket_open} %{$fg[red]%} ${bracket_close}"
fi
fi
}
function shortened_branch() {
local branch=$(git_current_branch)
[ $(echo -n "${branch}" | wc -c) -gt 15 ] && branch="${branch:0:15}%{$fg[red]%}$(printf '\uf141')%{$reset_color%}"
echo "${branch}"
}
function build_git_prompt() {
# Allow hiding the right side of the prompt
(test "${NO_RIGHT}" = "true") && return
# When there is no git, don't show a git prompt
git branch >/dev/null 2>/dev/null || return
# Fetch status of the current repo
local INDEX=$(command git status --porcelain -b 2>/dev/null)
local REMOTE=$(command git remote -v | grep fetch)
echo -n "${bracket_open} %{$fg[blue]%}"
# Mark specific remotes
case "${REMOTE}" in
*bitbucket.org*)
printf '\ue703 '
;;
*github.com*)
printf '\ue709 '
;;
*gitlab.com*)
printf '\uf296 '
;;
*)
printf '\uf1d3 '
;;
esac
# Show current branch and commit / tag
echo -n "%{$reset_color%}"
echo -n "$(shortened_branch) "
echo -n "($(git_describe)) "
# Print repository status information
[ $(echo "$INDEX" | wc -l) -gt 1 ] && echo -n "%{$fg[red]%}$(printf '\uf0f6')%{$FG[236]%} "
(git rev-parse --verify refs/stash >/dev/null 2>&1) && echo -n "%{$fg[blue]%}$(printf '\uf64c')%{$FG[236]%} "
# Show difference to remote
if (echo "$INDEX" | grep '^## .*ahead.*behind' &>/dev/null); then
echo -n "%{$fg[red]%}$(printf '\uf047')"
elif (echo "$INDEX" | grep '^## .*ahead' &>/dev/null); then
echo -n "%{$fg[green]%}$(printf '\uf061')"
elif (echo "$INDEX" | grep '^## .*behind' &>/dev/null); then
echo -n "%{$fg[yellow]%}$(printf '\uf060')"
else
echo -n "%{$fg[green]%}="
fi
echo -n "%{$reset_color%} ${bracket_close}"
}
function prompt_gomod() {
local mod_default="auto"
local mod=${GO111MODULE:-${mod_default}}
[[ ${mod} == ${mod_default} ]] || echo "${bracket_open} %{$fg[yellow]%}${mod} ${bracket_close}"
}
function prompt_kubectx() {
command -v kubectl >/dev/null || return 0
local ctx="$(kubectl config current-context 2>/dev/null || echo "")"
local color="yellow"
[[ -n $ctx ]] || return 0
case "${ctx}" in
minikube) color="green" ;;
prod) color="red" ;;
esac
echo "${bracket_open} %{$fg[${color}]%}${ctx} ${bracket_close}"
}
local current_dir='$(short_path)' local current_dir='$(short_path)'
local git_info='$(build_git_prompt)' local git_info='$(build_git_prompt)'
@ -126,16 +28,12 @@ local prompt_part_time="${bracket_open} %T ${bracket_close}"
local prompt_part_user="${bracket_open} %{$FG[040]%}%n%{$reset_color%}%{$FG[239]%}@%{$reset_color%}%{$(box_color)%}$(box_name)%{$reset_color%} ${bracket_close}" local prompt_part_user="${bracket_open} %{$FG[040]%}%n%{$reset_color%}%{$FG[239]%}@%{$reset_color%}%{$(box_color)%}$(box_name)%{$reset_color%} ${bracket_close}"
local prompt_part_path="%{$terminfo[bold]$FG[226]%}${current_dir}%{$reset_color%}" local prompt_part_path="%{$terminfo[bold]$FG[226]%}${current_dir}%{$reset_color%}"
local prompt_part_exit="%(?..${bracket_open} %{$fg[red]%}%?%{${reset_color}%} ${bracket_close})" local prompt_part_exit="%(?..${bracket_open} %{$fg[red]%}%?%{${reset_color}%} ${bracket_close})"
local prompt_part_char='$(prompt_char)'
local prompt_part_configgit='$(config-git_prompt)'
local prompt_part_gomod='$(prompt_gomod)'
local prompt_part_kubectx='$(prompt_kubectx)'
PROMPT=" PROMPT="
╭─ ${prompt_part_time}${prompt_part_user}${prompt_part_configgit}${prompt_part_gomod}${prompt_part_kubectx}${prompt_part_exit} ${prompt_part_path} ${prompt_part_time}${prompt_part_user}${prompt_part_exit} ${prompt_part_path}
╰─ " $(printf '\u279c') "
RPROMPT="${git_info}" RPROMPT=""
ZSH_THEME_GIT_PROMPT_PREFIX="" ZSH_THEME_GIT_PROMPT_PREFIX=""
ZSH_THEME_GIT_PROMPT_SUFFIX="" ZSH_THEME_GIT_PROMPT_SUFFIX=""
@ -144,4 +42,8 @@ ZSH_THEME_GIT_PROMPT_SUFFIX=""
ZSH_THEME_GIT_PROMPT_SHA_BEFORE="" ZSH_THEME_GIT_PROMPT_SHA_BEFORE=""
ZSH_THEME_GIT_PROMPT_SHA_AFTER="" ZSH_THEME_GIT_PROMPT_SHA_AFTER=""
if command -v oh-my-posh >/dev/null; then
eval "$(oh-my-posh init zsh --config ~/.zsh/oh-my-custom/oh-my-posh.yaml)"
fi
# vim: set ft=zsh: # vim: set ft=zsh:

View file

@ -0,0 +1,145 @@
blocks:
- type: prompt
alignment: left
newline: true
segments:
- type: time
style: diamond
leading_diamond: &ld '<#666>[</> '
trailing_diamond: &td ' <#666>]</>'
foreground: p:white
template: '{{ .CurrentDate | date "15:04:05" }}'
- type: session
style: diamond
leading_diamond: *ld
trailing_diamond: *td
foreground: p:green
template: '{{ .UserName }}{{ if .SSHSession }}@{{ .HostName }}{{ end }}'
- type: command
style: diamond
leading_diamond: *ld
trailing_diamond: *td
foreground: p:red
properties:
command: '~/bin/config-git-status.sh || echo "mod"'
template: '{{ if eq .Output "mod" }}{{ end }}'
- type: kubectl
style: diamond
leading_diamond: *ld
trailing_diamond: *td
foreground: p:white
foreground_templates:
- '{{ if eq .Context "int" }}p:yellow{{ end }}'
- '{{ if eq .Context "minikube" }}p:green{{ end }}'
- '{{ if eq .Context "prod" }}p:red{{ end }}'
template: '{{ .Context }}'
- type: command
style: diamond
leading_diamond: *ld
trailing_diamond: *td
foreground: p:yellow
properties:
command: short_path
template: '{{ .Output }}'
- type: exit
style: diamond
leading_diamond: *ld
trailing_diamond: *td
foreground: p:white
foreground_templates:
- '{{ if gt .Code 0 }}p:red{{ end }}'
properties:
always_enabled: false
template: '{{ if gt .Code 0 }}{{ .Code }}{{ else }}{{ end }}'
- type: prompt
alignment: right
overflow: hide
segments:
- type: executiontime
style: diamond
leading_diamond: *ld
trailing_diamond: *td
properties:
style: austin
template: '{{ .FormattedMs }}'
- type: git
style: diamond
leading_diamond: *ld
trailing_diamond: *td
foreground: p:green
foreground_templates:
- '{{ if or (.Working.Changed) (.Staging.Changed) }}p:yellow{{ end }}'
- '{{ if and (gt .Ahead 0) (gt .Behind 0) }}p:red{{ end }}'
- '{{ if gt .Ahead 0 }}#49416D{{ end }}'
- '{{ if gt .Behind 0 }}#7A306C{{ end }}'
properties:
bitbucket_icon: "\ue703"
branch_icon: ''
branch_max_length: 25
fetch_status: true
fetch_upstream_icon: true
github_icon: "\ue709"
git_icon: "\uf1d3"
gitlab_icon: "\uf296"
templates:
- '{{ if .UpstreamURL }}{{ url .UpstreamIcon .UpstreamURL }} {{ end }}'
- '{{ .HEAD }} ({{ .ShortHash }})'
- '{{ if .BranchStatus }} {{ .BranchStatus }}{{ end }}'
- '{{ if .Working.Changed }}  {{ .Working.String }}{{ end }}'
- '{{ if .Staging.Changed }}  {{ .Staging.String }}{{ end }}'
- type: prompt
alignment: left
newline: true
segments:
- type: text
style: plain
foreground: p:yellow
template: "\u279c"
console_title_template: "{{ .UserName }}@{{ .HostName }} \u279c {{ .Folder }}"
final_space: true
palette:
black: '#262B44'
blue: '#4B95E9'
gray: '#333333'
green: '#00bc00'
orange: '#F07623'
red: '#c91b00'
white: '#E0DEF4'
yellow: '#f4f400'
darkblue: '#0b1052'
tooltips:
- type: aws
style: diamond
leading_diamond: &ttld
trailing_diamond: &tttd
background: p:orange
foreground: p:white
properties:
display_default: true
template: '  {{ .Profile }}{{ if .Region }}@{{ .Region }}{{ end }} '
tips:
- aws
- type: az
style: diamond
leading_diamond: *ttld
trailing_diamond: *tttd
background: p:blue
foreground: p:white
template: ' ﴃ {{ .Name }} '
tips:
- az
version: 2