Skip to content

Commit

Permalink
add zshfmt experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
docwhat committed Oct 29, 2020
1 parent 04ade3d commit 7e24174
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
46 changes: 46 additions & 0 deletions tag-experiments/bin/zshfmt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env zsh

declare -r INDENTSIZE=2

printsymbol() {
local -ri indent=$(( $1 * INDENTSIZE ))
local -r symbol=$2

printf "% ${indent}s" ""
printf "%s " "$symbol"
}

fmtone() {
set -x
local -r content=$(cat "$1")
local -i indent=0
local -i newline=0
local line
for line in "${(Z+c+@)content}"; do
case "$line" in
';' )
printf "\n"
newline=1
continue
;;
'do' | '{' )
indent=$(( indent + 1 ))
printsymbol $(( indent * newline )) $line
;;
'done' | '}' )
indent=$(( indent - 1 ))
printsymbol $(( indent * newline )) $line
;;
*)
printsymbol $(( indent * newline )) $line
;;
esac
newline=0
done
printf "\n"
set +x
}

for file in "$@"; do
fmtone "$file"
done
2 changes: 1 addition & 1 deletion test-files/test.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

set -x

if (($+commands[zsh])); then
if (( $+commands[zsh] )); then
echo found zsh
fi

0 comments on commit 7e24174

Please sign in to comment.