Skip to content

Commit

Permalink
using new implentation if -z4h decode and zcat
Browse files Browse the repository at this point in the history
  • Loading branch information
mawwwrk committed Jun 16, 2022
1 parent 05cc36c commit 42476d2
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 50 deletions.
38 changes: 38 additions & 0 deletions fn/-z4h-base64-decode
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env zsh

eval "$_z4h_opt"

setopt cbases

typeset -g REPLY=

[[ -z $1 ]] && return
(( $#1 % 4 == 0 )) || return 1

local table='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'

local c1 c2 c3 c4 res
local -i16 b1 b2 b3 b4

for c1 c2 c3 c4 in ${(s::)1//=/A}; do
((
b1 = $table[(ie)$c1] - 1,
b2 = $table[(ie)$c2] - 1,
b3 = $table[(ie)$c3] - 1,
b4 = $table[(ie)$c4] - 1,
b1 = b1 << 2 | b2 >> 4,
b2 = (b2 << 4) & 255 | b3 >> 2,
b3 = (b3 << 6) & 255 | b4
))
res+=($b1 $b2 $b3)
done

if [[ $1 == *= ]]; then
if [[ $1 == *== ]]; then
res[-2,-1]=()
else
res[-1]=()
fi
fi

printf -v REPLY ${(j:\:)res#0}
75 changes: 25 additions & 50 deletions fn/-z4h-direnv-hook
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,14 @@ else
fi
fi

# { print -n '\x1f\x8b\x08\x00\x00\x00\x00\x00'; base64 -d <<<${${DIRENV_WATCHES//-/+}//_//} } | zcat 2>/dev/null

# zcat # gzcat # uncompress -c # gunzip -c # gzip -cd

local sig
local -a deps
local envrc=(./(../)#.envrc(NY1:a))

if (( $#envrc )); then
local -a deps=(
deps+=(
${XDG_DATA_HOME:-~/.local/share}/direnv/allow
${XDG_CONFIG_HOME:-~/.config}/{,direnv/}{direnv.toml,config.toml,direnvrc}
)
if [[ -v DIRENV_WATCHES ]]; then
local watches=$DIRENV_WATCHES
local -a watched
(){
typeset -A b64_map
local -i i=0
local -a b64chars=({A..Z} {a..z} {0..9} + /)

for char in $b64chars[@]; do b64_map+=( ${char} $( printf %06d $(( [##2] $i )) ) ); i+=1; done
b64_map+=( [-]=$b64_map[+] [_]=$b64_map[/] [=]=$b64_map[A] )

local bits
local hexstring='\x1f\x8b\x08\x00\x00\x00\x00\x00'
local -i bitlen=8
for j in {0..${#watches}}; do
bits+=${b64_map[${watches:$j:1}]}
if [[ ${#bits} -ge $bitlen ]]; then
hexstring+="\x${(Ll:2::0:)$(( [##16] 2#${bits:0:$bitlen} ))}"; bits=${bits:$bitlen}
fi
done

local -a apps=( 'zcat' 'gzcat' 'gzip' '-cd' 'gunzip' '-c' 'uncompress' '-c' )
local pat='[\[]*[\]]'
for app in $apps; do
(( $+commands[$app] )) || continue;
local unpacked=$( echo "${hexstring}" | "$app" ${${apps[((${apps[(i)$app]}+1))]}:#[^-]*} -- 2>/dev/null )
[[ -n $unpacked ]] && [[ -z ${unpacked:#${~pat}} ]] && break
done
${XDG_CONFIG_HOME:-~/.config}/{,direnv/}{direnv.toml,config.toml,direnvrc})
fi

setopt localoptions extendedglob
local -a elements
Expand Down Expand Up @@ -95,23 +64,30 @@ if (( $#envrc )); then
}
done
unsetopt localoptions extendedglob
if [[ -v DIRENV_WATCHES ]]; then
() {
local REPLY json
-z4h-base64-decode ${${DIRENV_WATCHES//-/+}//_//} || return
json=$(printf '\x1f\x8b\x08\x00\x00\x00\x00\x00%s' $REPLY | -z4h-zcat 2>/dev/null) || return
[[ $json == '['*']' ]] || return
# TODO: extract $(jq '.[] | .Path') from $json and append it to deps.
# TODO: cache those elements with $DIRENV_WATCHES as the key.
}
fi
[[ $envrc == "${(@)watched[1]}" ]]
deps=( ${(@)watched} ${(@)deps} )
local files=($^deps(N))
local non_files=(${deps:|files})
local -a stat
if zstat -A stat +mtime -- $files 2>/dev/null; then
local sig=$envrc$'\0'${(pj:\0:)stat}
else
local sig=stat-error
fi
elif [[ ! -v DIRENV_WATCHES ]]; then
# TODO: handle errors from the above intelligently.
fi

if (( $#envrc + $#deps == 0 )); then
typeset -g _z4h_direnv_sig=none
return
fi

local -a stat
local files=($^deps(N))
local non_files=(${deps:|files})
if zstat -A stat +mtime -- $envrc $files 2>/dev/null; then
local sig=$envrc$'\0'${(pj:\0:)stat}
else
local sig=none
local sig=stat-error
fi

[[ $sig == ${_z4h_direnv_sig-} ]] && return
Expand Down Expand Up @@ -148,6 +124,5 @@ if [[ ! -v __p9k_trapped ]]; then
builtin trap : INT
builtin trap "builtin trap ${(q)__p9k_trapint:--} INT" EXIT
fi
builtin eval -- $out
typeset -g _z4h_direnv_sig=$sig
11 changes: 11 additions & 0 deletions fn/-z4h-zcat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env zsh

eval "$_z4h_opt"

local cmd
for cmd in zcat gzcat 'uncompress -c' 'gunzip -c' 'gzip -cd'; do
local words=($=cmd)
[[ -v commands[$words[1]] ]] || continue
$words || return
break
done

0 comments on commit 42476d2

Please sign in to comment.