mblaze/mless

77 lines
1.6 KiB
Plaintext
Raw Normal View History

2016-07-20 11:47:18 +00:00
#!/bin/sh
2016-07-20 12:07:20 +00:00
# mless RANGES... - less(1)-wrapper around mshow
2016-07-20 11:47:18 +00:00
PATH="${0%/*}:$PATH"
2016-07-24 21:38:00 +00:00
colormsg() {
awk '
2016-07-20 11:47:18 +00:00
function fg(c, s) { return sprintf("\033[38;5;%03dm%s\033[0m", c, s) }
function so(s) { return sprintf("\033[1m%s\033[0m", s) }
BEGIN { hdr = 1 }
/^$/ { hdr = 0 }
2016-07-28 11:57:18 +00:00
/^-- $/ { ftr = 1 }
/^--- .* ---/ { print fg(242, $0); ftr = 0; sig = 0; next }
/^-----BEGIN .* SIGNATURE-----/ { sig = 1 }
2016-07-20 11:47:18 +00:00
hdr && /^From:/ { print so(fg(119, $0)); next }
hdr { print fg(120, $0); next }
2016-07-28 11:57:18 +00:00
ftr { print fg(244, $0); next }
/^-----BEGIN .* MESSAGE-----/ ||
/^-----END .* SIGNATURE-----/ { print fg(244, $0); sig = 0; next }
sig { print fg(244, $0); next }
2016-07-20 11:47:18 +00:00
/^>/ { print fg(151, $0); next }
{ print }'
2016-07-24 21:38:00 +00:00
}
colorscan() {
awk '
function fg(c, s) { return sprintf("\033[38;5;%03dm%s\033[0m", c, s) }
function so(s) { return sprintf("\033[1m%s\033[0m", s) }
/^>/ { print so(fg(119, $0)); next }
/^ *\\_/ { print fg(242, $0); next }
{ print }'
}
2016-07-24 21:55:13 +00:00
mnums() {
2016-07-24 22:07:58 +00:00
mscan "$@" 2>/dev/null | awk '$2 ~ /^[0-9]*$/ { print $2 }'
2016-07-24 21:55:13 +00:00
}
2016-07-24 21:38:00 +00:00
if [ "$1" = --filter ]; then
if [ "$2" = //scan ]; then
mscan : 2>/dev/null | colorscan
exit $?
fi
2016-07-26 15:21:39 +00:00
mseq -C "$2"
2016-07-24 21:38:00 +00:00
mscan .-2:.+3 2>/dev/null | colorscan
echo
if ! [ -f $(mseq "$2") ]; then
mseq "$2"
exit
fi
mshow "$2" | colormsg
2016-07-20 11:47:18 +00:00
exit $?
fi
2016-07-26 11:44:07 +00:00
scantab=
if [ "$#" -eq 0 ] && ! [ -t 0 ]; then
2016-07-26 14:39:10 +00:00
mseq -S >/dev/null
2016-07-26 11:44:07 +00:00
set -- :
scantab=//scan
fi
if ! [ -t 1 ]; then
exec mseq "$@"
fi
case "$0" in
*next*) d=1;;
*prev*) d=-1;;
*) d=0;;
esac
2016-07-24 21:55:13 +00:00
curcmd=$(mnums "$@" |
awk -v cur=$(mseq -n .) -v d=$d '$1 == cur {print "+" FNR+d ":x"; quit}')
2016-07-20 11:47:18 +00:00
2016-07-26 11:44:07 +00:00
LESSOPEN="|$0 --filter %s" exec less -Ps"mless %f?m (message %i of %m).." -R $curcmd $scantab $(mnums "$@")