2
0
mirror of https://github.com/chubin/cheat.sheets synced 2024-11-11 01:10:31 +00:00

Add lenchk (max_length functionality, and some)

Hope this helps. I wish I'd known this is what you were after, but I
guess it didn't click! This is an area I love and excel at, so I
hoped to put that to use here.

* The colorization flag might help when there are a lot of results.

* By far, the indented line number and preview of the offending lines
  is the most useful aspect of lenchk, making it much easier to track
  down troublesome lines, especially for the inexperienced.

* I've accounted for `//` lines (C languages), although some like
  Javascript, HTML, and CSS I've yet to get around to.

* There's a summary, which is probably pointless, but it might help to
  keep track of our progress in getting through the files.

* It's ever-so-slightly more efficient and portable, but ultimately, -
  I'd say making it use just /bin/sh would be better, but Bash is
  pretty common-place these days, and has features which make lenchk
  much cleaner.

  Ideally, I'd have written this in Perl, due to the limited lifespan
  of this tester, because a shell can only handle so much data before
  it starts to chug. I figured, however, that not everyone will have
  Perl.

If this commit isn't accepted, no worries, as I'm sure I can repurpose
it for use elsewhere.

Want it, but have issues? Let me know, and I'll get on that ASAP.

Note that I've removed max_length in this commit, to avoid confusion.
This commit is contained in:
terminalforlife 2020-11-16 20:28:38 +00:00
parent 2abb75a33f
commit fa546de881
2 changed files with 137 additions and 23 deletions

137
tests/lenchk Executable file
View File

@ -0,0 +1,137 @@
#!/usr/bin/env bash
#------------------------------------------------------------------------------
# Author E-Mail - terminalforlife@yahoo.com
# Author GitHub - https://github.com/terminalforlife
#------------------------------------------------------------------------------
Progrm=${0##*/}
Usage(){
while read; do
printf '%s\n' "$REPLY"
done <<-EOF
Usage: $Progrm [OPTS]
-h, --help - Display this help information.
-N, --no-preview - Omit the preview of each triggered line.
-S, --no-summary - Omit the summary before $Progrm exits.
-c, --colorize - Provide color via esscape sequences.
-l, --limit [INT] - Override the limit of 80 columns.
-p, --pager - Use less(1) to page the output.
EOF
}
Err(){
printf 'ERROR: %s\n' "$2" 1>&2
[ $1 -gt 0 ] && exit $1
}
MaxCols=80
while [ "$1" ]; do
case $1 in
--help|-h|-\?)
Usage; exit 0 ;;
--limit|-l)
shift; MaxCols=$1
if ! [[ $MaxCols =~ ^[0-9]+$ ]]; then
Err 1 'Invalid column maximum provided.'
fi ;;
--pager|-p)
DoLess='True' ;;
--no-preview|-N)
NoPreview='True' ;;
--colorize|-c)
DoColor='True' ;;
--no-summary|-S)
NoSummary='True' ;;
*)
Err 1 'Incorrect option(s) specified.' ;;
esac
shift
done
# Confirm we are in the right place.
git rev-parse --is-inside-work-tree 1> /dev/null 2>&1 ||
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.sh/tests' directory." ;;
esac
Main(){
for File in ../sheets/* ../sheets/*/*; {
[ -f "$File" ] || continue
HaveBeenHit='False'
LineNum=0
while read; do
let LineNum++
# Ignore non-comment lines for '#' and '//'.
case $REPLY in
'#'*|'//'*)
;;
*)
continue ;;
esac
if [ ${#REPLY} -gt 80 ]; then
# We only need to be informed of a hit just the once, per file.
if [ "$HaveBeenHit" == 'False' ]; then
# The leading newline character, if needed.
[ "$NoPreview" == 'True' ] || printf '\n'
# The filename containing problematic line lengths.
[ "$DoColor" == 'True' ] && printf '\e[1;31m'
printf '%s\n' "${File#../}"
[ "$DoColor" == 'True' ] && printf '\e[0m'
HaveBeenHit='True'
let Hits++
fi
if ! [ "$NoPreview" == 'True' ]; then
# The line number of the problematic length.
[ "$DoColor" == 'True' ] && printf '\e[1;32m'
printf ' %7d ' $LineNum # <-- allows for 9,999,999 lines.
[ "$DoColor" == 'True' ] && printf '\e[0m'
# Cannot make this 80 columns long, due to the indentation
# and padding, but if you need to test this, for the sake
# of checking lenchk is doing its job, set the 70 to 80, -
# then make sure the terminal window is wide enough.
printf '%s' "${REPLY:0:70}"
# Cut-off elipses.
[ "$DoColor" == 'True' ] && printf '\e[2;1m'
printf '...\n'
[ "$DoColor" == 'True' ] && printf '\e[0m'
fi
fi
done < "$File"
}
if [ $Hits -gt 0 -a "$NoSummary" != 'True' ]; then
printf '\nFound %d file(s) with comment length >%d.\n'\
$Hits $MaxCols 1>&2
fi
}
if [ "$DoLess" == 'True' ]; then
Main | less -r
else
Main
# Testing line for use when checking the summary lines up with wc(1). When
# using this, be sure previewing is disbaled and the summary is enabled.
#printf '%d\n' $((`Main |& wc -l` - 2))
fi

View File

@ -1,23 +0,0 @@
#!/usr/bin/env bash
# Check comment lines (lines starting with #) in cheat sheets
# and list files with such lines, and return exit code 1.
# Return exit code 0, if no cheat sheets with long lines found
MAX_STRING_LENGTH=80
MY_DIR=$(dirname "$(dirname "$(realpath "${BASH_SOURCE[0]}")")")
SHEETS_DIR="$MY_DIR/sheets"
long_lines_sheets=$(
cd "$SHEETS_DIR" &&
grep -rPl "#.{${MAX_STRING_LENGTH}}"
)
if [[ $(printf "%s\n" "$long_lines_sheets" | wc -l) -gt 1 ]]; then
echo "Files with long (> 80) lines:"
echo
printf "%s\n" "$long_lines_sheets"
exit 1
fi