Skip to content

Instantly share code, notes, and snippets.

@subnut
Last active November 10, 2024 14:22
Show Gist options
  • Save subnut/3af65306fbecd35fe2dda81f59acf2b2 to your computer and use it in GitHub Desktop.
Save subnut/3af65306fbecd35fe2dda81f59acf2b2 to your computer and use it in GitHub Desktop.
Transient prompt for zsh (insipred by romkatv's powerlevel10k)
### Simple transient prompt
[[ -c /dev/null ]] || return
zmodload zsh/system || return
## Set the transient prompt PROMPT here -
TRANSIENT_PROMPT='%# ' # Sample value
function set_prompt {
## Set the values of PROMPT and RPROMPT here
# Sample values given below
PROMPT='%~'$'\n''%# '
RPROMPT='%(?..%B%F{1}%?%f%b)'
}
zle -N send-break _transient_prompt_widget-send-break
function _transient_prompt_widget-send-break {
_transient_prompt_widget-zle-line-finish
zle .send-break
}
zle -N zle-line-finish _transient_prompt_widget-zle-line-finish
function _transient_prompt_widget-zle-line-finish {
(( ! _transient_prompt_fd )) && {
sysopen -r -o cloexec -u _transient_prompt_fd /dev/null
zle -F $_transient_prompt_fd _transient_prompt_restore_prompt
}
zle && PROMPT=$TRANSIENT_PROMPT RPROMPT= zle reset-prompt && zle -R
}
function _transient_prompt_restore_prompt {
exec {1}>&-
(( ${+1} )) && zle -F $1
_transient_prompt_fd=0
set_prompt
zle reset-prompt
zle -R
}
(( ${+precmd_functions} )) || typeset -ga precmd_functions
(( ${#precmd_functions} )) || {
do_nothing() {true}
precmd_functions=(do_nothing)
}
precmd_functions+=_transient_prompt_precmd
function _transient_prompt_precmd {
TRAPINT() { zle && _transient_prompt_widget-zle-line-finish; return $(( 128 + $1 )) }
}
# vim: sw=0 ts=4 sts=4 et
#### Transient prompt with transient newline before prompt
[[ -c /dev/null ]] || return
zmodload zsh/system || return
## Set the transient prompt PROMPT here -
TRANSIENT_PROMPT='%# ' # Sample value
function set_prompt {
## Set the values of PROMPT and RPROMPT here
# Sample values given below
PROMPT='%~'$'\n''%# '
RPROMPT='%(?..%B%F{1}%?%f%b)'
}
typeset -g _transient_prompt_newline=
function _transient_prompt_set_prompt {
set_prompt
PROMPT='$_transient_prompt_newline'$PROMPT
}; _transient_prompt_set_prompt
zle -N clear-screen _transient_prompt_widget-clear-screen
function _transient_prompt_widget-clear-screen {
_transient_prompt_newline=
zle .clear-screen
}
zle -N send-break _transient_prompt_widget-send-break
function _transient_prompt_widget-send-break {
_transient_prompt_widget-zle-line-finish
zle .send-break
}
zle -N zle-line-finish _transient_prompt_widget-zle-line-finish
function _transient_prompt_widget-zle-line-finish {
(( ! _transient_prompt_fd )) && {
sysopen -r -o cloexec -u _transient_prompt_fd /dev/null
zle -F $_transient_prompt_fd _transient_prompt_restore_prompt
}
zle && PROMPT=$TRANSIENT_PROMPT RPROMPT= zle reset-prompt && zle -R
}
function _transient_prompt_restore_prompt {
exec {1}>&-
(( ${+1} )) && zle -F $1
_transient_prompt_fd=0
_transient_prompt_set_prompt
zle reset-prompt
zle -R
}
(( ${+precmd_functions} )) || typeset -ga precmd_functions
(( ${#precmd_functions} )) || {
do_nothing() {true}
precmd_functions=(do_nothing)
}
precmd_functions+=_transient_prompt_precmd
function _transient_prompt_precmd {
# We define _transient_prompt_precmd in this way because we don't want
# _transient_prompt_newline to be defined on the very first precmd.
TRAPINT() {zle && _transient_prompt_widget-zle-line-finish; return $(( 128 + $1 ))}
function _transient_prompt_precmd {
TRAPINT() {zle && _transient_prompt_widget-zle-line-finish; return $(( 128 + $1 ))}
_transient_prompt_newline=$'\n'
}
}
# vim: sw=0 ts=4 sts=4 et
@gmr458
Copy link

gmr458 commented Apr 7, 2022

when I activate a virtual environment of python it does not appear in the prompt, how could it be solved?

@subnut
Copy link
Author

subnut commented Apr 7, 2022

@gmr458 Add ${VIRTUAL_ENV##*/} to PROMPT or RPROMPT or TRANSIENT_PROMPT, depending on where you want to show it.

@gmr458
Copy link

gmr458 commented Apr 8, 2022

I solved it with this:

function set_prompt {
    if [[ -n "$VIRTUAL_ENV" ]]; then
        PROMPT="(`basename \"$VIRTUAL_ENV\"`) "
    else
        PROMPT=''
    fi

    ## Set the values of PROMPT and RPROMPT here
    # Sample values given below
    PROMPT='%~'$'\n''%# '
    RPROMPT='%(?..%B%F{1}%?%f%b)'
}

@johansetia
Copy link

how i can integrated it with spaceship theme ?

@johansetia
Copy link

i solve it with change to this

## Set the transient prompt PROMPT here -
TRANSIENT_PROMPT=''   # Sample value

function set_prompt {
    ## Set the values of PROMPT and RPROMPT here
    # Sample values given below
    PROMPT='$(spaceship_prompt)'
    RPROMPT='$(spaceship_rprompt)'
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment