From e006f9836755e04d80b22e1460d724cd31024b1a Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 20 Jun 2020 13:11:09 +0200 Subject: [PATCH] Autoload functions on call and tweak gpg stuff around --- completions.zsh | 1 - custom.zsh | 8 +- custom/dirstack.zsh | 1 - custom/docker.zsh | 5 - custom/gpg.zsh | 8 +- custom/segments.zsh | 118 ------------------- custom/segments/cprompts:cache:async:covid | 2 + custom/segments/cprompts:cache:async:weather | 8 ++ custom/segments/prompt_c_covid_19 | 14 +++ custom/segments/prompt_c_docker_host | 2 + custom/segments/prompt_c_mpv_mpris | 6 + custom/segments/prompt_c_new_mail | 4 + custom/segments/prompt_c_todo | 17 +++ custom/segments/prompt_c_weather | 37 ++++++ custom/segments/prompt_c_youtube | 6 + powerlevel-config/prompt-elements.zsh | 2 +- 16 files changed, 105 insertions(+), 134 deletions(-) delete mode 100644 custom/dirstack.zsh delete mode 100644 custom/docker.zsh delete mode 100644 custom/segments.zsh create mode 100644 custom/segments/cprompts:cache:async:covid create mode 100644 custom/segments/cprompts:cache:async:weather create mode 100644 custom/segments/prompt_c_covid_19 create mode 100644 custom/segments/prompt_c_docker_host create mode 100644 custom/segments/prompt_c_mpv_mpris create mode 100644 custom/segments/prompt_c_new_mail create mode 100644 custom/segments/prompt_c_todo create mode 100644 custom/segments/prompt_c_weather create mode 100644 custom/segments/prompt_c_youtube diff --git a/completions.zsh b/completions.zsh index ab781ab..4484168 100644 --- a/completions.zsh +++ b/completions.zsh @@ -16,4 +16,3 @@ zstyle ':completion:*:functions' ignored-patterns '_*' zstyle ':completion:*:descriptions' format '%U%B%d%b%u' zstyle ':completion:*:warnings' format '%BSorry, no matches for: %d%b' autoload -Uz +X compinit && compinit - diff --git a/custom.zsh b/custom.zsh index 6b554cb..e253729 100644 --- a/custom.zsh +++ b/custom.zsh @@ -1,7 +1,7 @@ -source "${ZDOTDIR}/custom/segments.zsh" source "${ZDOTDIR}/custom/prependsudo.zsh" source "${ZDOTDIR}/custom/aliases.zsh" -source "${ZDOTDIR}/custom/dirstack.zsh" -source "${ZDOTDIR}/custom/docker.zsh" source "${ZDOTDIR}/custom/gpg.zsh" -fpath+="${ZDOTDIR}/custom/functions" +fpath+=("${ZDOTDIR}/custom/functions" "${ZDOTDIR}/custom/segments") +function {prompt_c_{covid_19,docker_host,mpv_mpris,new_mail,todo,weather,youtube},cprompts:cache:async:{covid,weather},get{mangareader,nhentai},gitsubrepo,osutab,ffmpeg-{compress,gif}}() { + autoload -X +} diff --git a/custom/dirstack.zsh b/custom/dirstack.zsh deleted file mode 100644 index 8d1c8b6..0000000 --- a/custom/dirstack.zsh +++ /dev/null @@ -1 +0,0 @@ - diff --git a/custom/docker.zsh b/custom/docker.zsh deleted file mode 100644 index 6e9beaf..0000000 --- a/custom/docker.zsh +++ /dev/null @@ -1,5 +0,0 @@ -export DOCKER_HOST=ssh://nbg-0 - -function build-n-push { - docker build ${1} --tag=localhost:5000/${2} --force-rm --compress && docker push localhost:5000/${2} # --squash can eat disk space quickly due to diffs not being taken in calculation -} diff --git a/custom/gpg.zsh b/custom/gpg.zsh index f617493..e5e18b5 100644 --- a/custom/gpg.zsh +++ b/custom/gpg.zsh @@ -1,7 +1,7 @@ # GPG SSH -unset SSH_AGENT_PID -if [ "${gnupg_SSH_AUTH_SOCK_by:-0}" -ne $$ ]; then +if [ ! -v SSH_AUTH_SOCK ]; then + unset SSH_AGENT_PID export SSH_AUTH_SOCK="$(gpgconf --list-dirs agent-ssh-socket)" + export GPG_TTY=$TTY + gpg-connect-agent updatestartuptty /bye >/dev/null fi -export GPG_TTY=$TTY -gpg-connect-agent updatestartuptty /bye >/dev/null diff --git a/custom/segments.zsh b/custom/segments.zsh deleted file mode 100644 index e45bb18..0000000 --- a/custom/segments.zsh +++ /dev/null @@ -1,118 +0,0 @@ -function prompt_c_docker_host() { - p10k segment -b blue -i '' -t "${DOCKER_HOST#*://}" -c "${DOCKER_HOST#*://}" -} -typeset -g POWERLEVEL9K_DOCKER_HOST_SHOW_ON_COMMAND='docker' -function prompt_c_new_mail() { - if [ ! -v _cprompts_mail_count ]; then - local new_mails=(~/Mail/*/Inbox/new/*(.N)) - typeset -g _cprompts_mail_count=${#new_mails} - fi - [[ ${_cprompts_mail_count} -gt 0 ]] || return - p10k segment -b yellow -i '' -t "${_cprompts_mail_count:-unknown}" -} -function prompt_c_youtube() { - [ -v commands\[sqlite3\] ] || return - local newsboatdb=~/.newsboat/cache.db - if [ ! -v _cprompts_youtube ]; then - typeset -g _cprompts_youtube="$(sqlite3 ${newsboatdb} --ascii 'SELECT COUNT(id) FROM rss_item WHERE unread == 1 AND guid GLOB "yt:video:*"')" - fi - p10k segment -b red -i '' -t "${_cprompts_youtube}" -} -function prompt_c_covid_19() { - ([ -v commands\[curl\] ] && [ -v commands\[jq\] ]) || return - local tmpfile=${CACHEPATH}/covid - local REPLY - zstyle -s ':custom:prompts:covid' country REPLY - local -a stats - if [ ! -v _cprompts_covid ]; then - zstat -A stats +mtime -- $tmpfile 2>/dev/null - if [[ $stats[1] < $(($epochtime[1]-86400)) ]]; then - cprompts:cache:async:covid &! - else - typeset -g _cprompts_covid=$(< $tmpfile) - fi - fi - [ -v _cprompts_covid ] && p10k segment -b red -i '' -t "${_cprompts_covid:-unknown}" -} -function cprompts:cache:async:covid() { - typeset -g _cprompts_covid=$(curl -sL https://api.covid19api.com/country/${REPLY:-germany} | jq -e '. |= sort_by (.Date) | .[-1].Active') - [ ! -z $_cprompts_covid ] && print ${_cprompts_covid} > $tmpfile -} -function prompt_c_weather() { - ([ -v commands\[curl\] ] && [ -v commands\[jq\] ]) || return - local tmpfile=${CACHEPATH}/weather - local -a stats - - # Fancy stuff - local -A icons - local -A colors - icons[c]='' - colors[c]="yellow" - icons[lc]='' - colors[lc]="yellow" - icons[hc]='' - colors[hc]="cyan" - icons[s]='🌢' - colors[s]="yellow" - icons[lr]='🌢' - colors[lr]="cyan" - icons[hr]='' - colors[hr]="blue" - icons[t]='' - colors[t]="white" - icons[h]='' - colors[h]="white" - icons[sl]='' - colors[sl]="blue" - icons[sn]='' - colors[sn]="white" - - if [ ! -v _cprompts_weather ]; then - zstat -A stats +mtime -- $tmpfile 2>/dev/null - if [[ $stats[1] < $(($epochtime[1]-21600)) ]]; then - cprompts:cache:async:weather &! - else - typeset -g _cprompts_weather=($(< $tmpfile)) - fi - fi - [ -v _cprompts_weather ] && p10k segment -b ${_cprompts_weather[2]:-white} -i ${_cprompts_weather[3]:-\?} -t "${_cprompts_weather[1]:-unknown}" - -} -function cprompts:cache:async:weather() { - local REPLY - zstyle -s ':custom:prompts:weather' location REPLY - local result=$(curl -sL https://www.metaweather.com/api/location/${REPLY:-1339615}/ | jq -e '.consolidated_weather[0] | {"temp": .the_temp, "icon": .weather_state_abbr}') - if [ ! -z $result ]; then - local ico=$(print $result | jq -r '.icon') - typeset -g _cprompts_weather=($(print $result | jq -r '.temp') ${colors[$ico]} ${icons[$ico]:-?}) - print ${_cprompts_weather%%.*} > $tmpfile - fi -} - -function prompt_c_mpv_mpris() { - ([ -v commands\[busctl\] ] && [ -v commands\[jq\] ]) || return - typeset -g _cprompts_mpv - local busout="$(busctl get-property -j --no-pager --timeout=300ms --user org.mpris.MediaPlayer2.mpv /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player Metadata 2>/dev/null)" - [ -z $busout ] && return - _cprompts_mpv="$(print $busout | jq -er '(.data."xesam:title".data // empty)')" - [ -z $_cprompts_mpv ] || p10k segment -b magenta -i '' -t "${_cprompts_mpv:0:20}$([ ${#_cprompts_mpv} -gt 20 ] && print ...)" -} -function prompt_c_todo() { - [ -r todo.txt ] || return - local -a stats - zstat -A stats +mtime -- todo.txt 2>/dev/null - if [ ! -v _cprompts_todo ] || [ $_cprompts_todo[4] -ne $stats[1] ]; then - local todofi=(${(f@)${mapfile[todo.txt]}}) - local t_nf=0 - local t_f=0 - local colors=(red yellow green) - for i in $todofi; do - [ ${i:0:1} = "-" ] && ((t_nf++)) - [ ${i:0:1} = "+" ] && ((t_f++)) - done - local t_tot=$(($t_f+$t_nf)) - [ $t_tot -gt 0 ] || return - typeset -g _cprompts_todo=($t_f $t_tot ${colors[$(( 1+int( $t_f / $t_tot.0 * 2 ) ))]} $stats[1]) - fi - p10k segment -b ${_cprompts_todo[3]:-white} -i '' -t "${_cprompts_todo[1]:-unknown} / ${_cprompts_todo[2]:-unknown}" -} diff --git a/custom/segments/cprompts:cache:async:covid b/custom/segments/cprompts:cache:async:covid new file mode 100644 index 0000000..1d92725 --- /dev/null +++ b/custom/segments/cprompts:cache:async:covid @@ -0,0 +1,2 @@ +typeset -g _cprompts_covid=$(curl -sL https://api.covid19api.com/country/${REPLY:-germany} | jq -e '. |= sort_by (.Date) | .[-1].Active') +[ ! -z $_cprompts_covid ] && print ${_cprompts_covid} > $tmpfile diff --git a/custom/segments/cprompts:cache:async:weather b/custom/segments/cprompts:cache:async:weather new file mode 100644 index 0000000..27f40eb --- /dev/null +++ b/custom/segments/cprompts:cache:async:weather @@ -0,0 +1,8 @@ +local REPLY +zstyle -s ':custom:prompts:weather' location REPLY +local result=$(curl -sL https://www.metaweather.com/api/location/${REPLY:-1339615}/ | jq -e '.consolidated_weather[0] | {"temp": .the_temp, "icon": .weather_state_abbr}') +if [ ! -z $result ]; then + local ico=$(print $result | jq -r '.icon') + typeset -g _cprompts_weather=($(print $result | jq -r '.temp') ${colors[$ico]} ${icons[$ico]:-?}) + print ${_cprompts_weather%%.*} > $tmpfile +fi diff --git a/custom/segments/prompt_c_covid_19 b/custom/segments/prompt_c_covid_19 new file mode 100644 index 0000000..21b2cb0 --- /dev/null +++ b/custom/segments/prompt_c_covid_19 @@ -0,0 +1,14 @@ +([ -v commands\[curl\] ] && [ -v commands\[jq\] ]) || return +local tmpfile=${CACHEPATH}/covid +local REPLY +zstyle -s ':custom:prompts:covid' country REPLY +local -a stats +if [ ! -v _cprompts_covid ]; then + zstat -A stats +mtime -- $tmpfile 2>/dev/null + if [[ $stats[1] < $(($epochtime[1]-86400)) ]]; then + cprompts:cache:async:covid &! + else + typeset -g _cprompts_covid=$(< $tmpfile) + fi +fi +[ -v _cprompts_covid ] && p10k segment -b red -i '' -t "${_cprompts_covid:-unknown}" diff --git a/custom/segments/prompt_c_docker_host b/custom/segments/prompt_c_docker_host new file mode 100644 index 0000000..56ec17d --- /dev/null +++ b/custom/segments/prompt_c_docker_host @@ -0,0 +1,2 @@ +p10k segment -b blue -i '' -t "${DOCKER_HOST#*://}" -c "${DOCKER_HOST#*://}" +typeset -g POWERLEVEL9K_DOCKER_HOST_SHOW_ON_COMMAND='docker' diff --git a/custom/segments/prompt_c_mpv_mpris b/custom/segments/prompt_c_mpv_mpris new file mode 100644 index 0000000..b58c0a1 --- /dev/null +++ b/custom/segments/prompt_c_mpv_mpris @@ -0,0 +1,6 @@ +([ -v commands\[busctl\] ] && [ -v commands\[jq\] ]) || return +typeset -g _cprompts_mpv +local busout="$(busctl get-property -j --no-pager --timeout=300ms --user org.mpris.MediaPlayer2.mpv /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player Metadata 2>/dev/null)" +[ -z $busout ] && return +_cprompts_mpv="$(print $busout | jq -er '(.data."xesam:title".data // empty)')" +[ -z $_cprompts_mpv ] || p10k segment -b magenta -i '' -t "${_cprompts_mpv:0:20}$([ ${#_cprompts_mpv} -gt 20 ] && print ...)" diff --git a/custom/segments/prompt_c_new_mail b/custom/segments/prompt_c_new_mail new file mode 100644 index 0000000..00d5314 --- /dev/null +++ b/custom/segments/prompt_c_new_mail @@ -0,0 +1,4 @@ +local new_mails=(~/Mail/*/Inbox/new/*(.N)) +typeset -g _cprompts_mail_count=${#new_mails} +[[ ${_cprompts_mail_count} -gt 0 ]] || return +p10k segment -b yellow -i '' -t "${_cprompts_mail_count:-unknown}" diff --git a/custom/segments/prompt_c_todo b/custom/segments/prompt_c_todo new file mode 100644 index 0000000..7bc38d9 --- /dev/null +++ b/custom/segments/prompt_c_todo @@ -0,0 +1,17 @@ +[ -r todo.txt ] || return +local -a stats +zstat -A stats +mtime -- todo.txt 2>/dev/null +if [ ! -v _cprompts_todo ] || [ $_cprompts_todo[4] -ne $stats[1] ]; then + local todofi=(${(f@)${mapfile[todo.txt]}}) + local t_nf=0 + local t_f=0 + local colors=(red yellow green) + for i in $todofi; do + [ ${i:0:1} = "-" ] && ((t_nf++)) + [ ${i:0:1} = "+" ] && ((t_f++)) + done + local t_tot=$(($t_f+$t_nf)) + [ $t_tot -gt 0 ] || return + typeset -g _cprompts_todo=($t_f $t_tot ${colors[$(( 1+int( $t_f / $t_tot.0 * 2 ) ))]} $stats[1]) +fi +p10k segment -b ${_cprompts_todo[3]:-white} -i '' -t "${_cprompts_todo[1]:-unknown} / ${_cprompts_todo[2]:-unknown}" diff --git a/custom/segments/prompt_c_weather b/custom/segments/prompt_c_weather new file mode 100644 index 0000000..e2e4aef --- /dev/null +++ b/custom/segments/prompt_c_weather @@ -0,0 +1,37 @@ +([ -v commands\[curl\] ] && [ -v commands\[jq\] ]) || return +local tmpfile=${CACHEPATH}/weather +local -a stats + +# Fancy stuff +local -A icons +local -A colors +icons[c]='' +colors[c]="yellow" +icons[lc]='' +colors[lc]="yellow" +icons[hc]='' +colors[hc]="cyan" +icons[s]='🌢' +colors[s]="yellow" +icons[lr]='🌢' +colors[lr]="cyan" +icons[hr]='' +colors[hr]="blue" +icons[t]='' +colors[t]="white" +icons[h]='' +colors[h]="white" +icons[sl]='' +colors[sl]="blue" +icons[sn]='' +colors[sn]="white" + +if [ ! -v _cprompts_weather ]; then + zstat -A stats +mtime -- $tmpfile 2>/dev/null + if [[ $stats[1] < $(($epochtime[1]-21600)) ]]; then + cprompts:cache:async:weather &! + else + typeset -g _cprompts_weather=($(< $tmpfile)) + fi +fi +[ -v _cprompts_weather ] && p10k segment -b ${_cprompts_weather[2]:-white} -i ${_cprompts_weather[3]:-\?} -t "${_cprompts_weather[1]:-unknown}" diff --git a/custom/segments/prompt_c_youtube b/custom/segments/prompt_c_youtube new file mode 100644 index 0000000..e9d7530 --- /dev/null +++ b/custom/segments/prompt_c_youtube @@ -0,0 +1,6 @@ +[ -v commands\[sqlite3\] ] || return +local newsboatdb=~/.newsboat/cache.db +if [ ! -v _cprompts_youtube ]; then + typeset -g _cprompts_youtube="$(sqlite3 ${newsboatdb} --ascii 'SELECT COUNT(id) FROM rss_item WHERE unread == 1 AND guid GLOB "yt:video:*"')" +fi +p10k segment -b red -i '' -t "${_cprompts_youtube}" diff --git a/powerlevel-config/prompt-elements.zsh b/powerlevel-config/prompt-elements.zsh index bf45ca9..192160d 100644 --- a/powerlevel-config/prompt-elements.zsh +++ b/powerlevel-config/prompt-elements.zsh @@ -14,6 +14,7 @@ typeset -g POWERLEVEL9K_LEFT_PROMPT_ELEMENTS=( ) typeset -g POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=() if [ ! -v SSH_CONNECTION ]; then +zmodload zsh/mapfile # Required by the todo segment, is way faster POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS+=( c_mpv_mpris c_todo @@ -22,7 +23,6 @@ POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS+=( c_weather c_covid_19 ) -zmodload zsh/mapfile # Required by the todo segment, is way faster fi POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS+=( newline