From 4aa1cfaef2437ffb1ed17a40de945dc050be42fd Mon Sep 17 00:00:00 2001 From: Russell Cloran Date: Tue, 10 Oct 2023 11:03:33 -0700 Subject: [PATCH] Add auto-update functionality Fixes #102 --- README.md | 5 +++++ functions/.znap.auto-update | 25 +++++++++++++++++++++++++ scripts/init.zsh | 4 ++++ 3 files changed, 34 insertions(+) create mode 100644 functions/.znap.auto-update diff --git a/README.md b/README.md index ee11811..6b9cdb8 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,11 @@ To run `znap pull` on specific repos only, including ones you have set to be exc % znap pull ... ``` +To be prompted to automatically update with `znap pull` every 30 days, add the following to your `.zshrc` file: +```sh +zstyle ':znap:pull' auto-update 30 +``` + ## `.zshrc` optimization Using Znap to optimize your Zsh config can be as simple as this: ```sh diff --git a/functions/.znap.auto-update b/functions/.znap.auto-update new file mode 100644 index 0000000..ef9fd1b --- /dev/null +++ b/functions/.znap.auto-update @@ -0,0 +1,25 @@ +#!/bin/zsh + +zmodload zsh/datetime + +.znap.autoupdate() { + local update_file=${XDG_STATE_HOME}/znap/last-update + if [[ ! -f "${update_file}" ]] ; then + echo $EPOCHSECONDS > "${update_file}" + return 0 + fi + + local interval + zstyle -s ':znap:pull' auto-update interval + + local last_update=$(< "${update_file}") + (( last_update + (interval * 86400) < EPOCHSECONDS )) || return 0 + + read -q "?Update znap? [y/N] " _ || return 0 + echo + + .znap.pull + echo $EPOCHSECONDS > "${update_file}" +} + +.znap.autoupdate "$@" diff --git a/scripts/init.zsh b/scripts/init.zsh index 3b76676..88de948 100644 --- a/scripts/init.zsh +++ b/scripts/init.zsh @@ -58,6 +58,10 @@ zstyle -T :znap: auto-compile && add-zsh-hook preexec ..znap.auto-compile add-zsh-hook zsh_directory_name ..znap.dirname +if zstyle -s ':znap:pull' auto-update _ ; then + .znap.auto-update +fi + typeset -gH _comp_dumpfile=${_comp_dumpfile:-$XDG_CACHE_HOME/zsh/compdump} [[ -f $_comp_dumpfile && ${${:-${ZDOTDIR:-$HOME}/.zshrc}:P} -nt $_comp_dumpfile ]] && zf_rm -f $_comp_dumpfile