pull/175/merge
terminalforlife 1 year ago committed by GitHub
commit d471d23d90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,14 +4,14 @@
# Author E-Mail - terminalforlife@yahoo.com
# Author GitHub - https://github.com/terminalforlife
#------------------------------------------------------------------------------
# Requires BASH >= 3.1, and optionally makes use of less(1) or more(1).
#------------------------------------------------------------------------------
Progrm=${0##*/}
Usage(){
while read; do
printf '%s\n' "$REPLY"
done <<-EOF
Usage: $Progrm [OPTS] [DIR_1 [DIR_2 ...]]
Usage() {
read -d '' <<-EOF
Usage: $Progrm [OPTS] [DIR [DIR] ...]
-h, --help - Display this help information.
-C, --no-color - Do not use ANSI color escape sequences.
@ -21,8 +21,8 @@ Usage(){
-P, --no-pager - Do not use less(1) or more(1) for paging.
-S, --no-summary - Omit the summary before $Progrm exits.
-W, --no-whilelist - Do not use the whitelist file.
-l, --limit [INT] - Override the limit of 80 columns.
-w, --wl-file [FILE] - Use an alternative whitelist file.
-l, --limit INT - Override the limit of 80 columns.
-w, --wl-file FILE - Use an alternative whitelist file.
Additional directories can be appended to the argument string in
order to process directories otherwise not handled by $Progrm. You
@ -41,19 +41,21 @@ Usage(){
otherwise specified.
Alternatively, a file can be whitelisted by ensuring the very first
line contains only 'lenchk=disable', akin to a vim(1) modeline.
line contains only '$Progrm=disable', akin to a vim(1) modeline.
EOF
printf '%s' "$REPLY"
}
Err(){
printf 'ERROR: %s\n' "$2" 1>&2
[ $1 -gt 0 ] && exit $1
Err() {
printf 'Err: %s\n' "$2" 1>&2
(( $1 > 0 )) && exit $1
}
WLFile='lenchk-excludes'
MaxCols=80
while [ "$1" ]; do
while [[ -n $1 ]]; do
case $1 in
--)
break ;;
@ -62,7 +64,7 @@ while [ "$1" ]; do
--limit|-l)
shift; MaxCols=$1
if ! [[ $MaxCols =~ ^[0-9]+$ ]]; then
if ! [[ $MaxCols =~ ^[[:digit:]]+$ ]]; then
Err 1 'Invalid column maximum provided.'
fi ;;
--no-pager|-P)
@ -82,7 +84,7 @@ while [ "$1" ]; do
--wl-file|-w)
shift
if [ -z "$1" ]; then
if [[ -z $1 ]]; then
Err 1 'No alternative whitelist file provided.'
else
WLFile=$1
@ -95,30 +97,28 @@ while [ "$1" ]; do
shift
done
# Thank you, Chubin. Feature apparently added in version 3.0 alpha, so should
# be OK, but if anyone is having issues, just `cd` to the 'tests' directory.
[ ${BASH_VERSINFO[0]:-0} -eq 3 ] || cd "${BASH_SOURCE[0]%/*}" &> /dev/null
# Thank you, Chubin. Being in the 'tests' directory is expected. This handles
# the edge case in which LenChk is instead sourced.
cd "${BASH_SOURCE[0]%/*}" &> /dev/null
# Confirm we are in the right place.
git rev-parse --is-inside-work-tree 1> /dev/null 2>&1 ||
git rev-parse --is-inside-work-tree &> /dev/null ||
Err 0 'Not inside a Git repository.'
case $PWD in
*/cheat.sheets/tests)
;;
'')
Err 1 'Unable to determine the CWD.' ;;
*)
Err 1 "Not within the 'cheat.sheets/tests' directory." ;;
esac
Dirs=(../sheets/*)
[ "$NoSubDirs" == 'True' ] || Dirs+=(../sheets/*/*)
[[ $NoSubDirs == True ]] || Dirs+=(../sheets/*/*)
# Add user's own directories to check, if they exist.
for ArgDir in "$@"; {
if [ -d "$ArgDir" ]; then
Dirs+=($ArgDir/*)
if [[ -d $ArgDir ]]; then
Dirs+=("$ArgDir"/*)
else
Err 1 "Directory '$ArgDir' not found."
fi
@ -126,7 +126,7 @@ for ArgDir in "$@"; {
# If the whitelist file exists, use it, unless whitelisting is disabled.
# Keeping this test outside of the loop to avoid unnecessary processing.
if [ "$NoWhiteList" != 'True' ] && [ -f "$WLFile" ]; then
if [[ $NoWhiteList != True && -f $WLFile ]]; then
# Read the whitelist file, line-by-line, generating an array thereof.
Whitelisted=()
while read; do
@ -134,47 +134,49 @@ if [ "$NoWhiteList" != 'True' ] && [ -f "$WLFile" ]; then
done < "$WLFile"
fi
# Using tput(1) for portability.
if type -fP tput &> /dev/null; then
# Prefer tput(1) for portability.
if type -P tput &> /dev/null; then
C_Ellipses=`tput dim; tput setaf 7`
C_LineNums=`tput bold; tput setaf 2`
C_FileNames=`tput bold; tput setaf 1`
C_Reset=`tput sgr0`
else
Err 1 'Unable to colorize output -- tput(1) not found.'
C_Ellipses='\e[2;37'
C_LineNums='\e[1;92m'
C_FileNames='\e[1;91m'
C_Reset='\e[0m'
fi
Main(){
Main() {
for File in "${Dirs[@]}"; {
[ -f "$File" ] || continue
[[ -f $File ]] || continue
# Per chubin's desire to have an "in-bound flag"; see #134.
if [ "$NoLenChkLine" != 'True' ]; then
# Per chubin's desire to have an "in-bound flag"; see #134. This also
# makes way for additional override parameters, if added in the future.
if [[ $NoLenChkLine != True ]]; then
SkipFile='False'
readarray -t Buffer < "$File"
for BufferLine in "${Buffer[@]}"; {
if [[ $BufferLine == '# cheat.sh: '* ]]; then
CheatLine=${BufferLine#'# cheat.sh: '}
while read; do
if [[ $REPLY == '# cheat.sh: '* ]]; then
CheatLine=${REPLY#'# cheat.sh: '}
IFS=',' read -a Fields <<< "$CheatLine"
for Field in "${Fields[@]}"; {
[ "$Field" == "$Progrm=disable" ] && SkipFile='True'
[[ $Field == "$Progrm"=disable ]] && SkipFile='True'
}
fi
}
done < "$File"
[ "$SkipFile" == 'True' ] && continue
[[ $SkipFile == True ]] && continue
fi
# If the current file matches one which is whitelisted, skip it.
for CurWL in "${Whitelisted[@]}"; {
[ "$File" == "$CurWL" ] && continue 2
[[ $File == "$CurWL" ]] && continue 2
}
LineNum=0
HaveBeenHit='False'
while read; do
let LineNum++
(( LineNum++ ))
# Ignore non-comment lines for '#' and '//'.
case $REPLY in
@ -184,26 +186,26 @@ Main(){
continue ;;
esac
if [ ${#REPLY} -gt 80 ]; then
if (( ${#REPLY} > 80 )); then
# We only need to be informed of a hit just the once, per file.
if [ "$HaveBeenHit" == 'False' ]; then
if [[ $HaveBeenHit == False ]]; then
# The leading newline character, if needed.
[ "$NoPreview" == 'True' ] || printf '\n'
[[ $NoPreview == True ]] || printf '\n'
# The filename containing problematic line lengths.
[ "$NoColor" == 'True' ] || printf "$C_FileNames"
[[ $NoColor == True ]] || printf "$C_FileNames"
printf '%s\n' "${File#../}"
[ "$NoColor" == 'True' ] || printf "$C_Reset"
[[ $NoColor == True ]] || printf "$C_Reset"
HaveBeenHit='True'
let Hits++
(( Hits++ ))
fi
if ! [ "$NoPreview" == 'True' ]; then
if [[ $NoPreview != True ]]; then
# The line number of the problematic length.
[ "$NoColor" == 'True' ] || printf "$C_LineNums"
[[ $NoColor == True ]] || printf "$C_LineNums"
printf ' %7d ' $LineNum # <-- allows for 9,999,999 lines.
[ "$NoColor" == 'True' ] || printf "$C_Reset"
[[ $NoColor == True ]] || printf "$C_Reset"
# Cannot make this 80 columns long, due to the indentation
# and padding, but if you need to test this, for the sake
@ -212,32 +214,32 @@ Main(){
printf '%s' "${REPLY:0:70}"
# Cut-off ellipses.
[ "$NoColor" == 'True' ] || printf "$C_Ellipses"
[[ $NoColor == True ]] || printf "$C_Ellipses"
printf '...\n'
[ "$NoColor" == 'True' ] || printf "$C_Reset"
[[ $NoColor == True ]] || printf "$C_Reset"
fi
fi
done < "$File"
}
if [ ${Hits:-0} -gt 0 -a "$NoSummary" != 'True' ]; then
if (( Hits > 0 )) && [[ $NoSummary != True ]]; then
printf '\nFound %d file(s) with comment length >%d.\n'\
$Hits $MaxCols 1>&2
fi
}
if [ -t 1 -a "$NoPager" != 'True' ]; then
if [[ -t 1 && $NoPager != True ]]; then
# Prefer less(1), but have more(1) as a fallback.
if type -fP less &> /dev/null; then
if type -P less &> /dev/null; then
Pager='less -R'
elif type -fP more &> /dev/null; then
elif type -P more &> /dev/null; then
Pager='more'
if [ "$NoColor" != 'True' ]; then
if [[ $NoColor != True ]]; then
Err 1 'Only more(1) is available -- colors unsupported.'
fi
else
Err 1 'Neither less(1) nor more(1) were found.'
Err 1 'Neither less(1) nor more(1) found.'
fi
# Redirecting STDERR to address less(1) bug causing summary to display

Loading…
Cancel
Save