| 1 |
#!/usr/bin/awk -f |
| 2 |
|
| 3 |
BEGIN { |
| 4 |
isatty = system("test -t 1") == "0" |
| 5 |
retcode = 0 |
| 6 |
} |
| 7 |
|
| 8 |
function color(code, s) { |
| 9 |
if (isatty) { |
| 10 |
return "\033[" code "m" s "\033[0m" |
| 11 |
} |
| 12 |
return s |
| 13 |
} |
| 14 |
function red(s) { return color("31", s) } |
| 15 |
function green(s) { return color("32", s) } |
| 16 |
function magenta(s) { return color("35", s) } |
| 17 |
function cyan(s) { return color("36", s) } |
| 18 |
function bg_red(s) { return color("41", s) } |
| 19 |
function hl_ws(s, pattern) { |
| 20 |
gsub(pattern, bg_red("&"), s) |
| 21 |
# convert tab characters to 8 spaces to allow coloring |
| 22 |
gsub(/\t/, " ", s) |
| 23 |
return s |
| 24 |
} |
| 25 |
|
| 26 |
/ +\t+/ { |
| 27 |
retcode = 1 |
| 28 |
print magenta(FILENAME) cyan(":") green(FNR) cyan(":") \ |
| 29 |
hl_ws($0, " +\\t+") red("<-- space(s) followed by tab(s)") |
| 30 |
} |
| 31 |
|
| 32 |
/[ \t]+$/ { |
| 33 |
retcode = 1 |
| 34 |
print magenta(FILENAME) cyan(":") green(FNR) cyan(":") \ |
| 35 |
hl_ws($0, "[ \\t]+$") red("<-- trailing whitespace") |
| 36 |
} |
| 37 |
|
| 38 |
ENDFILE { |
| 39 |
# will only match on GNU awk, ignored on non-GNU versions |
| 40 |
if ($0 ~ /^[ \t]*$/) { |
| 41 |
retcode = 1 |
| 42 |
print magenta(FILENAME) cyan(": ") red("trailing new line(s)") |
| 43 |
} else if (RT != "\n") { |
| 44 |
retcode = 1 |
| 45 |
print magenta(FILENAME) cyan(": ") red("no new line at end of file") |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
END { |
| 50 |
exit retcode |
| 51 |
} |