From ebcd6fcea1517b00153214e3284d6307809175f5 Mon Sep 17 00:00:00 2001 From: Robin Jarry Date: Sun, 16 Oct 2022 12:05:25 +0200 Subject: [PATCH] lint: check for bad white space habits A little coding hygiene cannot hurt. Add a simple awk script to check all source files for bad white space habits: - trailing white space - trailing new lines at the end of files - missing new line at the end of files - spaces followed by tabs The script outputs color when the terminal supports it. It exits with a non-zero code when there was at least one white space issue found. Call the script in the lint step. Example output of the awk script: config/default_styleset:1:# <-- trailing whitespace config/default_styleset:3:# <-- trailing whitespace doc/aerc.1.scd:78: Executes an arbitrary command in the background. Aerc will set the <-- trailing whitespace doc/aerc.1.scd:234: <-- trailing whitespace doc/aerc.1.scd:237: <-- trailing whitespace worker/types/thread_test.go:74: // return ErrSkipThread<-- space(s) followed by tab(s) worker/lib/testdata/message/invalid/hexa: trailing new line(s) Fix issues reported by the script. NB: The ENDFILE match is a GNU extension. It will be ignored on BSD-awk and trailing new lines will not be detected. The lint make target is only invoked on alpine linux which has GNU awk anyway. NB: Empty cells in scdoc tables require trailing white space... Avoid this by setting content in these cells. I don't really see a use for empty cells. Signed-off-by: Robin Jarry Tested-by: Moritz Poldrack --- Makefile | 1 + config/default_styleset | 4 +- contrib/check-whitespace | 51 ++++++++++++++++++++++++ doc/aerc-config.5.scd | 38 +++++++++--------- doc/aerc-stylesets.7.scd | 8 ++-- doc/aerc.1.scd | 4 +- worker/lib/testdata/message/invalid/hexa | 2 - worker/notmuch/worker.go | 5 --- worker/types/thread_test.go | 3 -- 9 files changed, 77 insertions(+), 39 deletions(-) create mode 100755 contrib/check-whitespace diff --git a/Makefile b/Makefile index 04ee08e..7f1837a 100644 --- a/Makefile +++ b/Makefile @@ -61,6 +61,7 @@ fmt: .PHONY: lint lint: + @contrib/check-whitespace `git ls-files` && echo white space ok. @$(GO) run mvdan.cc/gofumpt -l . | grep ^ \ && echo The above files need to be formatted, please run make fmt && exit 1 \ || echo all files formatted. diff --git a/config/default_styleset b/config/default_styleset index ae3b9e8..30baf97 100644 --- a/config/default_styleset +++ b/config/default_styleset @@ -1,6 +1,6 @@ -# +# # aerc default styleset -# +# # This styleset uses the terminal defaults as its base. # More information on how to configure the styleset can be found in # the aerc-stylesets(7) manpage. Please read the manual before diff --git a/contrib/check-whitespace b/contrib/check-whitespace new file mode 100755 index 0000000..5afd2f4 --- /dev/null +++ b/contrib/check-whitespace @@ -0,0 +1,51 @@ +#!/usr/bin/awk -f + +BEGIN { + isatty = system("test -t 1") == "0" + retcode = 0 +} + +function color(code, s) { + if (isatty) { + return "\033[" code "m" s "\033[0m" + } + return s +} +function red(s) { return color("31", s) } +function green(s) { return color("32", s) } +function magenta(s) { return color("35", s) } +function cyan(s) { return color("36", s) } +function bg_red(s) { return color("41", s) } +function hl_ws(s, pattern) { + gsub(pattern, bg_red("&"), s) + # convert tab characters to 8 spaces to allow coloring + gsub(/\t/, " ", s) + return s +} + +/ +\t+/ { + retcode = 1 + print magenta(FILENAME) cyan(":") green(FNR) cyan(":") \ + hl_ws($0, " +\\t+") red("<-- space(s) followed by tab(s)") +} + +/[ \t]+$/ { + retcode = 1 + print magenta(FILENAME) cyan(":") green(FNR) cyan(":") \ + hl_ws($0, "[ \\t]+$") red("<-- trailing whitespace") +} + +ENDFILE { + # will only match on GNU awk, ignored on non-GNU versions + if ($0 ~ /^[ \t]*$/) { + retcode = 1 + print magenta(FILENAME) cyan(": ") red("trailing new line(s)") + } else if (RT != "\n") { + retcode = 1 + print magenta(FILENAME) cyan(": ") red("no new line at end of file") + } +} + +END { + exit retcode +} diff --git a/doc/aerc-config.5.scd b/doc/aerc-config.5.scd index 21472e6..a09c5ab 100644 --- a/doc/aerc-config.5.scd +++ b/doc/aerc-config.5.scd @@ -390,7 +390,7 @@ These options are configured in the *[statusline]* section of aerc.conf. | %T : general on/off information (e.g. passthrough, threading, sorting) | %> -: does not print anything but all format specifier that follow will be right justified. +: does not print anything but all format specifier that follow will be right justified. *separator* Specifies the separator between grouped statusline elements (e.g. for @@ -854,7 +854,7 @@ each binding context: *$ex* This can be set to a keystroke which will bring up the command input in this context. - + Default: ':' In addition to letters, special keys may be specified in . The @@ -867,63 +867,63 @@ following special keys are supported: | semicolon : ; | tab -: +: Tab | enter -: +: Enter | up -: +: Up arrow | c-up : Ctrl+Up | a-up : Alt+Up | down -: +: Down arrow | c-down : Ctrl+Down | a-down : Alt+Down | right -: +: Right arrow | c-right : Ctrl+Right | a-right : Alt+Right | left -: +: Left arrow | c-left : Ctrl+Left | a-left : Alt+Left | pgup -: +: Page Up | c-pgup : Ctrl+PageUp | a-pgup : Alt+PageUp | pgdn -: +: Page Down | c-pgdn : Ctrl+PageDn | a-pgdn : Alt+PageDn | home -: +: Home | end -: +: End | insert -: +: Insert | delete -: +: Delete | exit -: +: Exit | cancel -: +: Cancel | print -: +: Print screen | pause -: +: Pause | backtab -: +: Shift+Tab | c-space : Ctrl+Space | a-space diff --git a/doc/aerc-stylesets.7.scd b/doc/aerc-stylesets.7.scd index 3b5dc23..0cb5a32 100644 --- a/doc/aerc-stylesets.7.scd +++ b/doc/aerc-stylesets.7.scd @@ -83,11 +83,10 @@ styling. [[ *Style Object* :[ *Description* | default -: The default style object used for normal ui elements while not - using specialized configuration. +: The default style object used for normal ui elements while not using specialized configuration. | error : The style used to show errors. -| warning +| warning : The style used when showing warnings. | success : The style used for success messages. @@ -158,8 +157,7 @@ For example, the following wildcards can be made using this syntax. | \*.fg=blue : Set the foreground color of all style objects to blue. | \*list.bg=hotpink -: Set the background color of all style objects that end in list - to hotpink. +: Set the background color of all style objects that end in list to hotpink. ## Selected modifier Selected modifier can be applied to any style object. The style provided for diff --git a/doc/aerc.1.scd b/doc/aerc.1.scd index dbf782c..ea4be17 100644 --- a/doc/aerc.1.scd +++ b/doc/aerc.1.scd @@ -76,7 +76,7 @@ These commands work in any context. as a delta from the selected tab. *exec* - Executes an arbitrary command in the background. Aerc will set the + Executes an arbitrary command in the background. Aerc will set the environment variables *$account* and *$folder* when the command is executed from an Account tab or an opened message. @@ -237,10 +237,8 @@ message list, the message in the message viewer, etc). Seen Message has been read - Answered Message has been answered - Flagged Message is flagged for urgent/special attention diff --git a/worker/lib/testdata/message/invalid/hexa b/worker/lib/testdata/message/invalid/hexa index 2967a19..56b352f 100644 --- a/worker/lib/testdata/message/invalid/hexa +++ b/worker/lib/testdata/message/invalid/hexa @@ -24,5 +24,3 @@ Content-Type: text/html; charset=utf-8 --Nextpart_1Q2YJhd197991794467076Pgfa-- - - diff --git a/worker/notmuch/worker.go b/worker/notmuch/worker.go index f1533e1..38539df 100644 --- a/worker/notmuch/worker.go +++ b/worker/notmuch/worker.go @@ -135,18 +135,13 @@ func (w *worker) handleMessage(msg types.WorkerMessage) error { case *types.CheckMail: go w.handleCheckMail(msg) return nil - // not implemented, they are generally not used // in a notmuch based workflow // case *types.DeleteMessages: // case *types.CopyMessages: - // return w.handleCopyMessages(msg) // case *types.AppendMessage: - // return w.handleAppendMessage(msg) // case *types.CreateDirectory: - // return w.handleCreateDirectory(msg) // case *types.RemoveDirectory: - // return w.handleRemoveDirectory(msg) } return errUnsupported } diff --git a/worker/types/thread_test.go b/worker/types/thread_test.go index 803f7ba..b3c3932 100644 --- a/worker/types/thread_test.go +++ b/worker/types/thread_test.go @@ -70,9 +70,6 @@ func TestNewWalk(t *testing.T) { var prefix []string lastLevel := 0 tree.Walk(func(t *Thread, lvl int, e error) error { - // if t.Uid%2 != 0 { - // return ErrSkipThread - // } if e != nil { fmt.Printf("ERROR: %v\n", e) }