Last active
January 15, 2016 17:24
-
-
Save markusfisch/94ae54b8d1911622c7df to your computer and use it in GitHub Desktop.
Count indent levels and echo lines that are indented over a given limit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Read lines from stdin and echo them if the indent is too deep | |
# | |
# @param 1 - levels of indent to consider too much (optional) | |
# @param 2 - indent character (optional) | |
check_indents() | |
{ | |
local THRESHOLD=${1:-3} | |
local INDENT=${2:-$'\t'} | |
local LINE=0 | |
local HITS=0 | |
while read -r | |
do | |
local COUNT=0 | |
while [[ $REPLY == $INDENT* ]] | |
do | |
REPLY=${REPLY:1} | |
(( ++COUNT )) | |
done | |
if (( COUNT > THRESHOLD )) | |
then | |
printf "% 4d (%dx) %s\n" $LINE $COUNT "$REPLY" | |
(( ++HITS )) | |
fi | |
(( ++LINE )) | |
done | |
return $HITS | |
} | |
if [ "${BASH_SOURCE[0]}" == "$0" ] | |
then | |
check_indents "$@" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment