From 51bd0516d6a62aa19fb87f68949d3f3759193838 Mon Sep 17 00:00:00 2001 From: Andrea Mistrali Date: Fri, 17 Dec 2021 09:14:01 +0100 Subject: [PATCH] Fixes for OMP prompts and for default PATH --- omp/mypure.omp.yaml | 12 ++-- omp/realpure.omp.yaml | 115 ++++++++++++++++++++++++++++++++++ zsh.d/10-environment.zsh | 1 - zsh.d/30-prompt.zsh | 2 +- zsh.d/Darwin/50-darwin.zsh | 2 +- zsh.d/zshfunctions/awsprofile | 4 +- zsh.d/zshfunctions/gitfetch | 55 +++++++++++++++- zshrc | 7 +++ 8 files changed, 185 insertions(+), 13 deletions(-) create mode 100644 omp/realpure.omp.yaml diff --git a/omp/mypure.omp.yaml b/omp/mypure.omp.yaml index 4e32c79..bf51fee 100644 --- a/omp/mypure.omp.yaml +++ b/omp/mypure.omp.yaml @@ -47,13 +47,13 @@ blocks: - type: executiontime style: plain - foreground: lightBlue + foreground: darkGray properties: always_enabled: false postfix: "" prefix: "\uF017 " style: austin - threshold: 3000 + threshold: 30000 - type: prompt alignment: right @@ -63,9 +63,9 @@ blocks: style: plain foreground: yellow properties: - postfix: " " - # prefix:  - prefix: "\uF270" + postfix: "" + # prefix: " " + prefix: "\u2601 " template: '{{if .Profile}}{{.Profile}}{{end}}' - type: kubectl @@ -74,7 +74,7 @@ blocks: properties: parse_kubeconfig: true postfix: "" - prefix: "" + prefix: " " template: '{{if eq "on" .Env.KUBE_PS1_ENABLED}}ﴱ {{.Context}}/{{if .Namespace}}{{.Namespace}}{{else}}default{{end}}{{end}}' # - type: command diff --git a/omp/realpure.omp.yaml b/omp/realpure.omp.yaml new file mode 100644 index 0000000..71cdedf --- /dev/null +++ b/omp/realpure.omp.yaml @@ -0,0 +1,115 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json +console_title: true +console_title_style: template +console_title_template: '{{if .Env.SSH_CONNECTION}} {{ .Host }} {{end}}{{.Path}}' +final_space: false + +blocks: +- type: prompt + alignment: left + newline: false + segments: + + - type: session + style: plain + foreground: yellow + properties: + postfix: "" + prefix: "" + template: '{{if .SSHSession}}@{{ .ComputerName }} {{end}}' + + + - type: root + style: plain + foreground: yellow + properties: + postfix: "" + prefix: "" + root_icon: "\u26A1" + # root_icon: "# " + + - type: path + style: plain + foreground: lightBlue + properties: + prefix: "" + style: full + + - type: git + style: plain + foreground: yellow + properties: + branch_ahead_icon: <#88C0D0>⇡ + branch_behind_icon: <#88C0D0>⇣ + branch_icon: "" + display_status: true + fetch_status: true + prefix: "" + template: '{{ .HEAD }}{{.BranchStatus}}{{ if .Working.Changed }}<#FF0000>* {{ .Working.String }}{{ end }}' + + - type: executiontime + style: plain + foreground: yellow + properties: + always_enabled: false + postfix: "" + prefix: " " + style: austin + threshold: 30000 + +- type: prompt + alignment: right + segments: + + - type: aws + style: plain + foreground: "#ffA000" + properties: + postfix: "" + # prefix: " " + prefix: " " + template: '{{if .Profile}}{{.Profile}}{{end}}' + + - type: kubectl + style: plain + foreground: lightBlue + properties: + parse_kubeconfig: true + postfix: "" + prefix: " " + template: '{{if eq "on" .Env.KUBE_PS1_ENABLED}}|{{.Context}}/{{if .Namespace}}{{.Namespace}}{{else}}default{{end}}|{{end}}' + + # - type: command + # style: plain + # foreground: white + # prefix: "" + # postfix: "" + # properties: + # shell: /bin/sh + # # command: "ifconfig utun2 > /dev/null 2>&1 && echo '\uF023' || echo ''" + # command: "ifconfig utun2 > /dev/null 2>&1 && echo '\u2713' || echo '\u2717'" + + +- type: prompt + alignment: left + newline: true + segments: + + - type: python + style: plain + foreground: darkGray + properties: + display_version: false + postfix: "" + prefix: "" + + - type: exit + style: plain + foreground: lightYellow + foreground_templates: + - '{{ if gt .Code 0 }}#FF0000{{ end }}' + properties: + always_enabled: true + prefix: "" + # template: ❯ + template: "\u276F" diff --git a/zsh.d/10-environment.zsh b/zsh.d/10-environment.zsh index 35c31ab..9fbb1e8 100644 --- a/zsh.d/10-environment.zsh +++ b/zsh.d/10-environment.zsh @@ -2,7 +2,6 @@ # Variables ## Vars used always -PATH="$PATH:/usr/local/sbin:/usr/sbin:/sbin:$HOME/bin:." MIBS=all LOGD=/var/log diff --git a/zsh.d/30-prompt.zsh b/zsh.d/30-prompt.zsh index a65b4cb..d922fde 100644 --- a/zsh.d/30-prompt.zsh +++ b/zsh.d/30-prompt.zsh @@ -1,5 +1,5 @@ fpath=($0:h/prompts $fpath) -# fpath+=$0:h/prompts/mypure +fpath+=$0:h/prompts/mypure # fpath+=$0:h/prompts/pure autoload -U promptinit;promptinit diff --git a/zsh.d/Darwin/50-darwin.zsh b/zsh.d/Darwin/50-darwin.zsh index 4e49bce..bdd4d2c 100644 --- a/zsh.d/Darwin/50-darwin.zsh +++ b/zsh.d/Darwin/50-darwin.zsh @@ -5,7 +5,7 @@ # Setup brew # We need it here to fix the PATH # so we can setup pyenv and other stuff - +# for prefix in /usr/local /opt/homebrew; do brew=$(whence $prefix/bin/brew) if [ $brew ]; then diff --git a/zsh.d/zshfunctions/awsprofile b/zsh.d/zshfunctions/awsprofile index f7ef23f..15cce9b 100644 --- a/zsh.d/zshfunctions/awsprofile +++ b/zsh.d/zshfunctions/awsprofile @@ -21,8 +21,6 @@ if [ $profile ]; then # We got a profile pattern, look for a match print -P "$FX[bold]activating profile $FG[075]$match$FX[reset]" export AWS_DEFAULT_PROFILE=$match export AWS_PROFILE=$match - export AWS_DEFAULT_REGION=$(grep -A1 $match ~/.aws/config|grep region|cut -d" " -f3) - export AWS_REGION=${AWS_DEFAULT_REGION} return else print -P "$FX[bold]$FG[009]no match for $profile$FX[reset]" @@ -30,7 +28,7 @@ if [ $profile ]; then # We got a profile pattern, look for a match fi elif [ $AWS_PROFILE ]; then # no profile passed, clean up current one, logout - unset AWS_DEFAULT_PROFILE AWS_PROFILE AWS_DEFAULT_REGION AWS_REGION + unset AWS_DEFAULT_PROFILE AWS_PROFILE print -P "$FX[bold]profile cleared$FX[reset]." return fi diff --git a/zsh.d/zshfunctions/gitfetch b/zsh.d/zshfunctions/gitfetch index e4fc4a1..fc0517c 100644 --- a/zsh.d/zshfunctions/gitfetch +++ b/zsh.d/zshfunctions/gitfetch @@ -7,7 +7,60 @@ cd $1 gitdir=$(git rev-parse --git-dir 2> /dev/null) || return 0 last_fetch=$(zstat +mtime $gitdir/FETCH_HEAD 2> /dev/null || echo 0) let "diff = $(strftime %s) - $last_fetch" -[ $diff -gt ${GIT_FETCH_INTERVAL:-30} ] && git fetch --all -p +if [ $diff -gt ${GIT_FETCH_INTERVAL:-30} ]; then + setopt localoptions noshwordsplit + + # Sets `GIT_TERMINAL_PROMPT=0` to disable authentication prompt for Git fetch (Git 2.3+). + export GIT_TERMINAL_PROMPT=0 + # Set SSH `BachMode` to disable all interactive SSH password prompting. + export GIT_SSH_COMMAND="${GIT_SSH_COMMAND:-"ssh"} -o BatchMode=yes" + + local ref + ref=$(command git symbolic-ref -q HEAD) + local -a remote + remote=($(command git for-each-ref --format='%(upstream:remotename) %(refname)' $ref)) + + if [[ -z $remote[1] ]]; then + # No remote specified for this branch, skip fetch. + return 97 + fi + + # Default return code, which indicates Git fetch failure. + local fail_code=99 + + # Guard against all forms of password prompts. By setting the shell into + # MONITOR mode we can notice when a child process prompts for user input + # because it will be suspended. Since we are inside an async worker, we + # have no way of transmitting the password and the only option is to + # kill it. If we don't do it this way, the process will corrupt with the + # async worker. + setopt localtraps monitor + + # Make sure local HUP trap is unset to allow for signal propagation when + # the async worker is flushed. + trap - HUP + + trap ' + # Unset trap to prevent infinite loop + trap - CHLD + if [[ $jobstates = suspended* ]]; then + # Set fail code to password prompt and kill the fetch. + fail_code=98 + kill %% + fi + ' CHLD + + # Only fetch information for the current branch and avoid + # fetching tags or submodules to speed up the process. + command git -c gc.auto=0 fetch \ + --quiet \ + --no-tags \ + --recurse-submodules=no \ + $remote &>/dev/null & + wait $! || return $fail_code + + unsetopt monitor +fi return 0 diff --git a/zshrc b/zshrc index 6f97dda..db6c1d1 100644 --- a/zshrc +++ b/zshrc @@ -11,6 +11,7 @@ # at the bottom of ~/.zshrc add `zprof >! zsh_profile` to save # profiling data at startup # zmodload zsh/zprof + # Determine zsh conf file position. On Debian/Ubuntu is /etc/zsh, on other should be /etc # [ -d /etc/zsh ] && BASE="/etc/zsh" || BASE="/etc" @@ -38,6 +39,9 @@ function loadRC { done } +# Default PATH +PATH="/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin" + local KERNEL=`uname -s` local LIBRARY=${BASE}/zsh.d local KERNLIB=${LIBRARY}/${KERNEL} @@ -45,6 +49,9 @@ local KERNLIB=${LIBRARY}/${KERNEL} loadRC ${KERNLIB} loadRC ${LIBRARY} +# Add home bin to PATH +PATH="$PATH:$HOME/bin" + # Add local customization file if [ -w $LIBRARY ]; then [ -f $LIBRARY/99-local.zsh ] || echo "# Local customizations" > $LIBRARY/99-local.zsh