From 2bc31bd7dc47a56dec6d6b4b473602c70b749ead Mon Sep 17 00:00:00 2001 From: terminalforlife Date: Tue, 17 Nov 2020 00:48:59 +0000 Subject: [PATCH 1/6] Address less-related bug regarding summary --- tests/lenchk | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/tests/lenchk b/tests/lenchk index 3f9223b..7a47b08 100755 --- a/tests/lenchk +++ b/tests/lenchk @@ -4,6 +4,14 @@ # Author E-Mail - terminalforlife@yahoo.com # Author GitHub - https://github.com/terminalforlife #------------------------------------------------------------------------------ +# Features: +# +#TODO: k +# +# Bugs: +# +# N/A +#------------------------------------------------------------------------------ Progrm=${0##*/} @@ -19,6 +27,18 @@ Usage(){ -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. + -w, --whitelist [FILE] - Whitelist files stored within. + + Whitelisting uses the following format, by way of example: + + file 'cheat.sheets/sheets/_perl/1line' + file 'cheat.sheets/sheets/find' + + The file paths to provide must begin with 'cheat.sheets/' to indicate + the root of the repository, otherwise this feature will fail. + + The location of the whitelisting file ('$Progrm-exclude') must remain + in the same directory in which $Progrm is stored. EOF } @@ -127,7 +147,7 @@ Main(){ } if [ "$DoLess" == 'True' ]; then - Main | less -r + Main 2>&1 | less -r else Main From 6ef4f6e9fc5584da24c9d5e09c691a8e7460ddf2 Mon Sep 17 00:00:00 2001 From: terminalforlife Date: Tue, 17 Nov 2020 00:56:16 +0000 Subject: [PATCH 2/6] Add option to ignore 'sheets/*/*' subdirectories --- tests/lenchk | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/lenchk b/tests/lenchk index 7a47b08..3591382 100755 --- a/tests/lenchk +++ b/tests/lenchk @@ -22,6 +22,7 @@ Usage(){ Usage: $Progrm [OPTS] -h, --help - Display this help information. + -D, --no-subdirs - Ignore the 'sheets/_*' subdirectories. -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. @@ -67,6 +68,8 @@ while [ "$1" ]; do DoColor='True' ;; --no-summary|-S) NoSummary='True' ;; + --no-subdirs|-D) + NoSubDirs='True' ;; *) Err 1 'Incorrect option(s) specified.' ;; esac @@ -87,7 +90,10 @@ case $PWD in esac Main(){ - for File in ../sheets/* ../sheets/*/*; { + Dirs=(../sheets/*) + [ "$NoSubDirs" == 'True' ] || Dirs+=(../sheets/*/*) + + for File in "${Dirs[@]}"; { [ -f "$File" ] || continue HaveBeenHit='False' @@ -147,6 +153,8 @@ Main(){ } if [ "$DoLess" == 'True' ]; then + # Redirecting STDERR to address less(1) bug causing summary to display + # where it shouldn't; only seems to happen when colorization is enabled. Main 2>&1 | less -r else Main From 17454dc8f4de86f372ccb31fec29a380906e8d97 Mon Sep 17 00:00:00 2001 From: terminalforlife Date: Tue, 17 Nov 2020 01:49:52 +0000 Subject: [PATCH 3/6] Add whitelisting functionality We can now specify our own whitelisting file, overriding the default of 'lenchk-excludes' which must stay in the same directory as lenchk, at least for now. The custom file can be wherever you want it, however. I'd like to use the `readarray`/`mapfile` BASH built-in for populating the array variable, but it didn't seem to want to work, so I just went for another `while read` loop. It's working very nicely as-is, though. I've changed the required whitelist format to one of simplicity: one filename per line. Why the previous format? Because of files which may have a newline character in them, but I realised in this environment that's just not going to happen; force of habit, I guess. I'd like to allow users to be able to whitelist entire directories, but I don't see that being so high on the priority list, for now. What do you think? --- tests/lenchk | 48 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/tests/lenchk b/tests/lenchk index 3591382..5bb3bb2 100755 --- a/tests/lenchk +++ b/tests/lenchk @@ -4,14 +4,6 @@ # Author E-Mail - terminalforlife@yahoo.com # Author GitHub - https://github.com/terminalforlife #------------------------------------------------------------------------------ -# Features: -# -#TODO: k -# -# Bugs: -# -# N/A -#------------------------------------------------------------------------------ Progrm=${0##*/} @@ -25,21 +17,23 @@ Usage(){ -D, --no-subdirs - Ignore the 'sheets/_*' subdirectories. -N, --no-preview - Omit the preview of each triggered line. -S, --no-summary - Omit the summary before $Progrm exits. + -W, --no-whilelist - Do not use the whitelist file. -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. - -w, --whitelist [FILE] - Whitelist files stored within. + -w, --wl-file [FILE] - Use an alternative whitelist file. - Whitelisting uses the following format, by way of example: + Files to whitelist must be placed line-by-line; one path per line. - file 'cheat.sheets/sheets/_perl/1line' - file 'cheat.sheets/sheets/find' + cheat.sheets/sheets/_perl/1line + cheat.sheets/sheets/find The file paths to provide must begin with 'cheat.sheets/' to indicate the root of the repository, otherwise this feature will fail. - The location of the whitelisting file ('$Progrm-exclude') must remain - in the same directory in which $Progrm is stored. + The location of the whitelisting file ('$Progrm-excludes') must + remain in the same directory in which $Progrm is stored, unless + otherwise specified. EOF } @@ -48,6 +42,7 @@ Err(){ [ $1 -gt 0 ] && exit $1 } +WLFile='lenchk-excludes' MaxCols=80 while [ "$1" ]; do @@ -70,6 +65,16 @@ while [ "$1" ]; do NoSummary='True' ;; --no-subdirs|-D) NoSubDirs='True' ;; + --no-whitelist|-W) + NoWhiteList='True' ;; + --wl-file|-w) + shift + + if [ -z "$1" ]; then + Err 1 'No alternative whitelist file provided.' + else + WLFile=$1 + fi ;; *) Err 1 'Incorrect option(s) specified.' ;; esac @@ -93,9 +98,24 @@ Main(){ Dirs=(../sheets/*) [ "$NoSubDirs" == 'True' ] || Dirs+=(../sheets/*/*) + # 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 + # Read the whitelist file, line-by-line, generating an array thereof. + Whitelisted=() + while read; do + Whitelisted+=("../${REPLY#cheat.sheets/}") + done < "$WLFile" + fi + for File in "${Dirs[@]}"; { [ -f "$File" ] || continue + # If the current file matches one which is whitelisted, skip it. + for CurWL in "${Whitelisted[@]}"; { + [ "$File" == "$CurWL" ] && continue 2 + } + HaveBeenHit='False' LineNum=0 From 66a38b71969e6955f99c130c7382601ac922880c Mon Sep 17 00:00:00 2001 From: terminalforlife Date: Tue, 17 Nov 2020 02:04:07 +0000 Subject: [PATCH 4/6] Tighten Main(), and use pager by default The use of `[ -t 1 ]` won't check for interactivity, rather, whether STDOUT is viable? If so, it might be best to check the `$-` variable (I think that's it) for the `i` character, like the standard default Debian `.bashrc` interactivity checks. Or, my preference, check the `$PS1` is not empty, which works well in my own `.bashrc` file. The `Main()` function was spread a bit too far, but is now more appropriate; has no real impact, just clarify when reading. Although the use of less(1) pager is enabled by default, it can be disabled now with the `--no-pager|-P` flags. --- tests/lenchk | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/tests/lenchk b/tests/lenchk index 5bb3bb2..6a0f16b 100755 --- a/tests/lenchk +++ b/tests/lenchk @@ -16,11 +16,11 @@ Usage(){ -h, --help - Display this help information. -D, --no-subdirs - Ignore the 'sheets/_*' subdirectories. -N, --no-preview - Omit the preview of each triggered line. + -P, --no-pager - Do not use less(1) to page the output. -S, --no-summary - Omit the summary before $Progrm exits. -W, --no-whilelist - Do not use the whitelist file. -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. -w, --wl-file [FILE] - Use an alternative whitelist file. Files to whitelist must be placed line-by-line; one path per line. @@ -55,8 +55,8 @@ while [ "$1" ]; do if ! [[ $MaxCols =~ ^[0-9]+$ ]]; then Err 1 'Invalid column maximum provided.' fi ;; - --pager|-p) - DoLess='True' ;; + --no-pager|-P) + NoPager='True' ;; --no-preview|-N) NoPreview='True' ;; --colorize|-c) @@ -94,20 +94,20 @@ case $PWD in Err 1 "Not within the 'cheat.sheets/tests' directory." ;; esac +Dirs=(../sheets/*) +[ "$NoSubDirs" == 'True' ] || Dirs+=(../sheets/*/*) + +# 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 + # Read the whitelist file, line-by-line, generating an array thereof. + Whitelisted=() + while read; do + Whitelisted+=("../${REPLY#cheat.sheets/}") + done < "$WLFile" +fi + Main(){ - Dirs=(../sheets/*) - [ "$NoSubDirs" == 'True' ] || Dirs+=(../sheets/*/*) - - # 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 - # Read the whitelist file, line-by-line, generating an array thereof. - Whitelisted=() - while read; do - Whitelisted+=("../${REPLY#cheat.sheets/}") - done < "$WLFile" - fi - for File in "${Dirs[@]}"; { [ -f "$File" ] || continue @@ -172,7 +172,7 @@ Main(){ fi } -if [ "$DoLess" == 'True' ]; then +if [ -t 1 -a "$NoPager" != 'True' ]; then # Redirecting STDERR to address less(1) bug causing summary to display # where it shouldn't; only seems to happen when colorization is enabled. Main 2>&1 | less -r From 1e7c9ae57ec2333b9243b3f2c1ee2869ec18253c Mon Sep 17 00:00:00 2001 From: terminalforlife Date: Tue, 17 Nov 2020 02:12:54 +0000 Subject: [PATCH 5/6] Use more(1) as a fallback for paging --- tests/lenchk | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/lenchk b/tests/lenchk index 6a0f16b..50303d7 100755 --- a/tests/lenchk +++ b/tests/lenchk @@ -16,7 +16,7 @@ Usage(){ -h, --help - Display this help information. -D, --no-subdirs - Ignore the 'sheets/_*' subdirectories. -N, --no-preview - Omit the preview of each triggered line. - -P, --no-pager - Do not use less(1) to page the output. + -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. -c, --colorize - Provide color via esscape sequences. @@ -173,9 +173,18 @@ Main(){ } if [ -t 1 -a "$NoPager" != 'True' ]; then + # Prefer less(1), but have more(1) as a fallback. + if type -fP less &> /dev/null; then + Pager='less' + elif type -fP more &> /dev/null; then + Pager='more' + else + Err 1 'Neither less(1) nor more(1) were found.' + fi + # Redirecting STDERR to address less(1) bug causing summary to display # where it shouldn't; only seems to happen when colorization is enabled. - Main 2>&1 | less -r + Main 2>&1 | $Pager -r else Main From a7dbcd06d73e1b64ee419ac46cd3da57caa8a6d4 Mon Sep 17 00:00:00 2001 From: terminalforlife Date: Tue, 17 Nov 2020 02:33:10 +0000 Subject: [PATCH 6/6] Handle user's own provided directories --- tests/lenchk | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/tests/lenchk b/tests/lenchk index 50303d7..2dd0ca5 100755 --- a/tests/lenchk +++ b/tests/lenchk @@ -11,7 +11,7 @@ Usage(){ while read; do printf '%s\n' "$REPLY" done <<-EOF - Usage: $Progrm [OPTS] + Usage: $Progrm [OPTS] [DIR_1 [DIR_2 ...]] -h, --help - Display this help information. -D, --no-subdirs - Ignore the 'sheets/_*' subdirectories. @@ -23,6 +23,10 @@ Usage(){ -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 + can use '--' to let $Progrm know when to stop looking for flags. + Files to whitelist must be placed line-by-line; one path per line. cheat.sheets/sheets/_perl/1line @@ -47,6 +51,8 @@ MaxCols=80 while [ "$1" ]; do case $1 in + --) + break ;; --help|-h|-\?) Usage; exit 0 ;; --limit|-l) @@ -75,8 +81,10 @@ while [ "$1" ]; do else WLFile=$1 fi ;; - *) + -*) Err 1 'Incorrect option(s) specified.' ;; + *) + break ;; esac shift done @@ -97,6 +105,15 @@ esac 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/*) + else + Err 1 "Directory '$ArgDir' not found." + fi +} + # 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 @@ -166,7 +183,7 @@ Main(){ done < "$File" } - if [ $Hits -gt 0 -a "$NoSummary" != 'True' ]; then + if [ ${Hits:-0} -gt 0 -a "$NoSummary" != 'True' ]; then printf '\nFound %d file(s) with comment length >%d.\n'\ $Hits $MaxCols 1>&2 fi