Skip to content

Commit

Permalink
Make znap prompt work without arguments
Browse files Browse the repository at this point in the history
Implements issue #37.
  • Loading branch information
marlonrichert committed May 11, 2021
1 parent e3972b3 commit 6b8792d
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 74 deletions.
70 changes: 45 additions & 25 deletions .zshrc
Original file line number Diff line number Diff line change
@@ -1,40 +1,52 @@
#
# ⚠️ WARNING: Never manually `source` your .zshrc file! It can have unexpected side effects.
# Instead, to safely apply changes, use `znap restart`.
# ⚠️ WARNING: Never manually `source` your .zshrc file! It can have unexpected
# side effects. Instead, to safely apply changes, use `znap restart`.
#

# Source Znap at the start of your .zshrc file.
source ~/git/zsh-snap/znap.zsh


# `znap prompt` makes your prompt appear in ~40ms. You can start typing right away!
# `znap prompt` makes your prompt appear in ~40ms. You can start typing right
# away!
znap prompt agnoster/agnoster-zsh-theme

# `znap prompt` also supports Oh-My-Zsh themes. Just make sure you load the required libs first:
# `znap prompt` also supports Oh-My-Zsh themes. Just make sure you load the
# required libs first:
znap source ohmyzsh/ohmyzsh lib/{git,theme-and-appearance}
znap prompt ohmyzsh/ohmyzsh robbyrussell

# Now your prompt is visible and you can type, even though your .zshrc file hasn't finished loading
# yet! In the background, the rest of your `.zshrc` file continues to be executed.
# Using your own custom prompt? Just call `znap prompt` without arguments:
PS1=$'%(?,%F{g},%F{r})%#%f '
znap prompt

# ℹ️ NOTE: For the best experience, the theme you pass to `znap prompt` should implement Zsh's
# `promptinit` API. For more info, see

# Now your prompt is visible and you can type, even though your .zshrc file
# hasn't finished loading yet! In the background, the rest of your `.zshrc`
# file continues to be executed.

# ℹ️ NOTE: For the best experience, the theme you pass to `znap prompt` should
# implement Zsh's `promptinit` API. For more info, see
# http://zsh.sourceforge.net/Doc/Release/User-Contributions.html#Writing-Themes


# Use `znap source` to load only those parts of Oh-My-Zsh or Prezto that you really need:
znap source ohmyzsh/ohmyzsh 'lib/(*~(git|theme-and-appearance).zsh)' plugins/git
# Use `znap source` to load only those parts of Oh-My-Zsh or Prezto that you
# really need:
znap source sorin-ionescu/prezto modules/{environment,history}
znap source ohmyzsh/ohmyzsh \
'lib/(*~(git|theme-and-appearance).zsh)' plugins/git

# Use `znap source` to load your plugins:
znap source marlonrichert/zsh-autocomplete
znap source marlonrichert/zsh-edit

# `znap source` finds the right file automatically, but you can also specify one explicitly:
# `znap source` finds the right file automatically, but you can also specify
# one explicitly:
znap source asdf-vm/asdf asdf.sh


# No special syntax is needed for configuring plugins. Just use normal Zsh statements:
# No special syntax is needed for configuring plugins. Just use normal Zsh
# statements:

znap source marlonrichert/zsh-hist
bindkey '^[q' push-line-or-edit
Expand All @@ -49,45 +61,53 @@ znap source zsh-users/zsh-syntax-highlighting

# Use `znap eval` to cache the output of slow commands:

# If the first arg is a repo, then the command will run inside it. Plus, whenever you update a
# repo with `znap pull`, its eval cache gets regenerated automatically.
# If the first arg is a repo, then the command will run inside it. Plus,
# whenever you update a repo with `znap pull`, its eval cache gets regenerated
# automatically.
znap eval trapd00r/LS_COLORS 'dircolors -b LS_COLORS'
znap source marlonrichert/zcolors
znap eval zcolors "zcolors ${(q)LS_COLORS}"

# The cache gets regenerated, too, when the eval command has changed. So, for example, since we
# include the full path to `direnv` in the command string, the cache will be regenerated whenever
# the version of `direnv` changes.
# The cache gets regenerated, too, when the eval command has changed. So, for
# example, since we include the full path to `direnv` in the command string,
# the cache will be regenerated whenever the version of `direnv` changes.
znap eval asdf-community/asdf-direnv "asdf exec $( asdf which direnv ) hook zsh"

# These don't belong to any repo, but the first arg will be used to name the cache file.
# These don't belong to any repo, but the first arg will be used to name the
# cache file.
znap eval brew-shellenv 'brew shellenv'
znap eval pyenv-init ${${:-=pyenv}:A}' init -' # Absolute path contains version number.
znap eval pip-completion 'pip completion --zsh'
znap eval pipx-completion 'register-python-argcomplete pipx'
znap eval pipenv-completion 'pipenv --completion'

# Combine `znap eval` with `curl` or `wget` to download, cache and source
# individual files:
znap eval omz-git \
'curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/plugins/git/git.plugin.zsh'


# Use `znap compdef` to add generated completion functions:
znap compdef _kubectl 'kubectl completion zsh'
znap compdef _rustup 'rustup completions zsh'
znap compdef _cargo 'rustup completions zsh cargo'
# These functions are automatically regenerated when any of the commands for which they complete is
# newer than the function.
# These functions are automatically regenerated when any of the commands for
# which they complete is newer than the function.


# All repos managed by Znap are automatically available as dynamically-named dirs.
# All repos managed by Znap are automatically available as dynamically-named
# dirs.

# This makes it easier to add commands to your `$path`...
path+=(
~[aureliojargas/clitest]
~[ekalinin/github-markdown-toc]
~[aureliojargas/clitest]
~[ekalinin/github-markdown-toc]
)

# ...or (completion) functions to your `$fpath`.
fpath+=(
~[asdf-community/asdf-direnv]/completions
~[zsh-users/zsh-completions]/src
~[asdf-community/asdf-direnv]/completions
~[zsh-users/zsh-completions]/src
)

# Likewise, you can also do
Expand Down
17 changes: 8 additions & 9 deletions functions/.znap.eval
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,29 @@ zmodload -Fa zsh/files b:zf_mkdir

local repo=~znap/$1 pwd=$PWD
[[ $1 == */* ]] &&
repo=~[$1]
repo=~[$1]
shift
{
local cmd="$@"
local line header="#${(q)cmd}"
local line header="#${(q)@}"
local cache_dir=$XDG_CACHE_HOME/zsh-snap/eval
[[ -d $cache_dir ]] ||
zf_mkdir -pm 0700 $cache_dir
zf_mkdir -pm 0700 $cache_dir
local cache_file=$cache_dir/${repo:t}.zsh
[[ -r $cache_file ]] &&
IFS='' read -r line < $cache_file
if [[ $line != $header ||
( -d $repo && -f $repo/${GIT_DIR:-.git}/index
&& $repo/${GIT_DIR:-.git}/index -nt $cache_file ) ]]; then
( -d $repo && -f $repo/${GIT_DIR:-.git}/index
&& $repo/${GIT_DIR:-.git}/index -nt $cache_file ) ]]; then
print -r "$header" >! $cache_file
[[ -d $repo ]] &&
cd -q -- $repo
print -r "$(eval $cmd)" >>! $cache_file
cd -q -- $repo
eval "$@" >>| $cache_file
fi
. $cache_file
return
} always {
[[ -x $pwd ]] &&
cd -q -- $pwd
cd -q -- $pwd
}
}

Expand Down
78 changes: 38 additions & 40 deletions functions/.znap.prompt
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@ zmodload -F zsh/parameter p:functions
autoload -Uz add-zsh-hook

.znap.prompt() {
if (( $# < 1 )); then
print -u2 'znap prompt: not enough arguments'
.znap.help prompt
return $(( sysexits[(i)USAGE] + 63 ))
fi

local repo=~znap/$1; [[ $1 == */* ]] && repo=~[$1]
local theme=${2:-${${repo##*/((oh-my-|)zsh-|)}%%((-zsh|)-(prompt|theme)|.zsh)}}
if ! .znap.prompt.load $repo $theme; then
print -u2 'znap prompt: no such theme:' $@
return $(( sysexits[(i)NOINPUT] + 63 ))
if (( # )); then
local repo=~znap/$1; [[ $1 == */* ]] && repo=~[$1]
local theme=${2:-${${repo##*/((oh-my-|)zsh-|)}%%((-zsh|)-(prompt|theme)|.zsh)}}
if ! .znap.prompt.load $repo $theme; then
print -u2 'znap prompt: no such theme:' $@
return $(( sysexits[(i)NOINPUT] + 63 ))
fi
fi

..znap.tput civis
Expand All @@ -25,37 +21,39 @@ autoload -Uz add-zsh-hook

autoload -Uz promptinit
promptinit
prompt_themes+=( $theme )

{
# Prevent `zsh-syntax-highlighting` from sending us into an infinite loop.
typeset -gHi _znap__hook_widgets=0
add-zle-hook-widget() {
eval ":znap:add-zle-hook-widget:$(( ++_znap__hook_widgets ))() {
add-zle-hook-widget ${(@q+)@}
}"
setopt NO_promptsp

if [[ -n $theme ]]; then
prompt_themes+=( $theme )
{
# Prevent `zsh-syntax-highlighting` from sending us into an infinite loop.
typeset -gHi _znap__hook_widgets=0
add-zle-hook-widget() {
eval ":znap:add-zle-hook-widget:$(( ++_znap__hook_widgets ))() {
add-zle-hook-widget ${(@q+)@}
}"
}

prompt $theme

local precmd=prompt_${theme}_precmd
if [[ -n $functions[$precmd] ]]; then
$precmd
functions[:znap:$precmd]=$functions[$precmd]
eval "$precmd() {
functions[$precmd]=\$functions[:znap:$precmd]
unfunction :znap:$precmd
$precmd \"\$@\"
}"
fi
} always {
unfunction add-zle-hook-widget
autoload -Uz add-zle-hook-widget
}

setopt NO_promptsp
prompt $theme

local precmd=prompt_${theme}_precmd
if [[ -n $functions[$precmd] ]]; then
$precmd
functions[:znap:$precmd]=$functions[$precmd]
eval "$precmd() {
functions[$precmd]=\$functions[:znap:$precmd]
unfunction :znap:$precmd
$precmd \"\$@\"
}"
fi
} always {
unfunction add-zle-hook-widget
autoload -Uz add-zle-hook-widget
}
fi

[[ -o promptcr ]] &&
print -n $'\r'
print -n $'\r'
:
print -nPr -- "$PS1"
..znap.tput cnorm
Expand Down Expand Up @@ -83,7 +81,7 @@ autoload -Uz add-zsh-hook
..znap.tput cnorm

[[ -v prompt_opts ]] ||
setopt promptsp
setopt promptsp

return 0
}
Expand Down

0 comments on commit 6b8792d

Please sign in to comment.