feat: html rendering
This commit is contained in:
@@ -1,49 +0,0 @@
|
|||||||
BEGIN {
|
|
||||||
RS = "\x0c"
|
|
||||||
FS = "\n"
|
|
||||||
}
|
|
||||||
|
|
||||||
/^message}/ {
|
|
||||||
mheader = header
|
|
||||||
mpart = part
|
|
||||||
mparts = parts
|
|
||||||
level = 0
|
|
||||||
multipart = ""
|
|
||||||
header = ""
|
|
||||||
part = ""
|
|
||||||
parts = ""
|
|
||||||
}
|
|
||||||
/^header{/ {
|
|
||||||
header = $0
|
|
||||||
}
|
|
||||||
/^(part|attachment)}/ {
|
|
||||||
level--
|
|
||||||
}
|
|
||||||
/^(part|attachment){/ {
|
|
||||||
parts = sprintf("%s\n%"(2*level)"s%s", parts, "", substr($1, index($1, "ID")))
|
|
||||||
level++
|
|
||||||
}
|
|
||||||
# /^part{ ID: [[:digit:]]+, Content-type: multipart\/alternative/ {
|
|
||||||
# multipart = "alternative"
|
|
||||||
# }
|
|
||||||
# /^part{ ID: [[:digit:]]+, Content-type: multipart\/mixed/ {
|
|
||||||
# multipart = "mixed"
|
|
||||||
# }
|
|
||||||
/^part{ ID: [[:digit:]]+, Content-type: text\/plain/ {
|
|
||||||
part = $0
|
|
||||||
}
|
|
||||||
|
|
||||||
END {
|
|
||||||
split(mheader, a, "\n")
|
|
||||||
delete a[1]
|
|
||||||
# delete a[2]
|
|
||||||
for (line in a) {
|
|
||||||
print a[line]
|
|
||||||
}
|
|
||||||
split(mpart, a, "\n")
|
|
||||||
delete a[1]
|
|
||||||
for (line in a) {
|
|
||||||
print a[line]
|
|
||||||
}
|
|
||||||
print "Parts:"mparts
|
|
||||||
}
|
|
||||||
13
src/awk/messageheader.awk
Normal file
13
src/awk/messageheader.awk
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
BEGIN {
|
||||||
|
RS = "\x0c"
|
||||||
|
FS = "\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
/^header{/ {
|
||||||
|
split($0, a, "\n")
|
||||||
|
delete a[1]
|
||||||
|
delete a[2]
|
||||||
|
for (line in a)
|
||||||
|
print a[line]
|
||||||
|
exit
|
||||||
|
}
|
||||||
12
src/awk/messageparts.awk
Normal file
12
src/awk/messageparts.awk
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
BEGIN {
|
||||||
|
RS = "\x0c"
|
||||||
|
FS = "\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
/^(part|attachment)}/ {
|
||||||
|
level--
|
||||||
|
}
|
||||||
|
/^(part|attachment){/ {
|
||||||
|
printf "%"(2*level)"s%s\n", "", substr($1, index($1, "ID"))
|
||||||
|
level++
|
||||||
|
}
|
||||||
14
src/awk/partnr.awk
Normal file
14
src/awk/partnr.awk
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
# Return the part number of the first text/plain part, or, if the variable
|
||||||
|
# `html` is set, of the first text/html part.
|
||||||
|
BEGIN {
|
||||||
|
RS = "\x0c"
|
||||||
|
FS = "\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
/^part{.*Content-type: text\// {
|
||||||
|
match($1, "ID: ([[:digit:]]+).* Content-type: text/([[:alpha:]]+)", a)
|
||||||
|
if ((html && a[2] == "html") || (!html && a[2] == "plain")) {
|
||||||
|
print a[1]
|
||||||
|
exit
|
||||||
|
}
|
||||||
|
}
|
||||||
68
src/main.sh
68
src/main.sh
@@ -17,47 +17,43 @@ set -eu
|
|||||||
# tools
|
# tools
|
||||||
. "sh/tools.sh"
|
. "sh/tools.sh"
|
||||||
|
|
||||||
# history
|
# query history
|
||||||
. "sh/history.sh"
|
. "sh/history.sh"
|
||||||
|
|
||||||
__notmuch_search() {
|
# notmuch functions
|
||||||
$NOTMUCH search "$*" \
|
. "sh/notmuch.sh"
|
||||||
| sed "s/^thread:\([[:xdigit:]]\+\)\s\+\([^[]\+\)\s\+\[\([^]]\+\)\]\([^;]*\); \(.*\) (\([^(]*\))$/${COLDATE}\2${COLRESET}\t${COLCNTS}\3${COLRESET}\t${COLFROM}\4${COLRESET}\t${COLSUBJ}\5${COLRESET}\t${COLTAGS}\6${COLRESET}\t\1/" \
|
|
||||||
| column -s "$(printf '\t')" -t -l 3 -R 1,2
|
|
||||||
}
|
|
||||||
|
|
||||||
__notmuch_thread() {
|
# list generators
|
||||||
$NOTMUCH show thread:"$1" \
|
. "sh/lists.sh"
|
||||||
| awk "$AWK_THREADOVERVIEW" \
|
|
||||||
| sed "s/^\(\S*\)\t\([^\t]*\)\t\(.*\) (\([^()]*\)) (\([^()]*\))$/${COLFROM}\3${COLRESET}\t${COLSUBJ}\2${COLRESET}\t${COLDATE}\4${COLRESET}\t${COLTAGS}\5${COLRESET}\t\1/"
|
|
||||||
}
|
|
||||||
|
|
||||||
__notmuch_list_tags() {
|
|
||||||
$NOTMUCH search --output=tags tag:/./
|
|
||||||
}
|
|
||||||
|
|
||||||
__notmuch_list_address() {
|
|
||||||
$NOTMUCH address '*'
|
|
||||||
}
|
|
||||||
|
|
||||||
|
# preview functions
|
||||||
|
. "sh/preview.sh"
|
||||||
|
|
||||||
if [ "${1:-}" = "--preview-thread" ]; then
|
if [ "${1:-}" = "--preview-thread" ]; then
|
||||||
shift
|
shift
|
||||||
thread="$1"
|
threadid="$1"
|
||||||
$NOTMUCH show thread:$thread \
|
preview_message "thread" "$threadid"
|
||||||
| awk "$AWK_LASTMESSAGEINTHREAD" \
|
exit
|
||||||
| tail -n +2 \
|
fi
|
||||||
| $CAT
|
|
||||||
|
if [ "${1:-}" = "--preview-html-thread" ]; then
|
||||||
|
shift
|
||||||
|
threadid="$1"
|
||||||
|
preview_message "thread" "$threadid" "html"
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "${1:-}" = "--preview-message" ]; then
|
if [ "${1:-}" = "--preview-message" ]; then
|
||||||
shift
|
shift
|
||||||
messageid="$1"
|
messageid="$1"
|
||||||
$NOTMUCH show id:$messageid \
|
preview_message "id" "$messageid"
|
||||||
| awk "$AWK_LASTMESSAGEINTHREAD" \
|
exit
|
||||||
| tail -n +2 \
|
fi
|
||||||
| $CAT
|
|
||||||
|
if [ "${1:-}" = "--preview-html-message" ]; then
|
||||||
|
shift
|
||||||
|
messageid="$1"
|
||||||
|
preview_message "id" "$messageid" "html"
|
||||||
exit
|
exit
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -76,7 +72,7 @@ FZF_DEFAULT_THREAD_LINE="{1} {3} ({4})"
|
|||||||
if [ "${1:-}" = "--show-thread" ]; then
|
if [ "${1:-}" = "--show-thread" ]; then
|
||||||
shift
|
shift
|
||||||
thread="$1"
|
thread="$1"
|
||||||
res=$(__notmuch_thread "$thread" | $FZF \
|
res=$(list_messages_in_thread "$thread" | $FZF \
|
||||||
--raw \
|
--raw \
|
||||||
--reverse \
|
--reverse \
|
||||||
--multi \
|
--multi \
|
||||||
@@ -91,6 +87,8 @@ if [ "${1:-}" = "--show-thread" ]; then
|
|||||||
--bind="ctrl-h,backward-eof:abort" \
|
--bind="ctrl-h,backward-eof:abort" \
|
||||||
--bind="alt-/:change-preview-window($FZF_ALTERNATE_PREVIEW_WINDOW|$FZF_DEFAULT_PREVIEW_WINDOW)" \
|
--bind="alt-/:change-preview-window($FZF_ALTERNATE_PREVIEW_WINDOW|$FZF_DEFAULT_PREVIEW_WINDOW)" \
|
||||||
--bind="alt-v:execute:$0 --view-source {5}" \
|
--bind="alt-v:execute:$0 --view-source {5}" \
|
||||||
|
--bind="alt-h:change-preview:$0 --preview-html-message {5}" \
|
||||||
|
--bind="focus:change-preview:$0 --preview-message {5}" \
|
||||||
--preview="$0 --preview-message {5}" \
|
--preview="$0 --preview-message {5}" \
|
||||||
--preview-window="$FZF_DEFAULT_PREVIEW_WINDOW" || true)
|
--preview-window="$FZF_DEFAULT_PREVIEW_WINDOW" || true)
|
||||||
exit
|
exit
|
||||||
@@ -108,7 +106,7 @@ CMD_EDIT_QUERY="edit-query"
|
|||||||
while true; do
|
while true; do
|
||||||
nmquery="${nmquery:-tag:inbox}"
|
nmquery="${nmquery:-tag:inbox}"
|
||||||
[ "$(tail -1 "$NMFHIST")" = "$nmquery" ] || echo "$nmquery" >> "$NMFHIST"
|
[ "$(tail -1 "$NMFHIST")" = "$nmquery" ] || echo "$nmquery" >> "$NMFHIST"
|
||||||
cmd=$(__notmuch_search "$nmquery" | $FZF \
|
cmd=$(list_threads "$nmquery" | $FZF \
|
||||||
--header="Query: $nmquery" \
|
--header="Query: $nmquery" \
|
||||||
--tiebreak=index \
|
--tiebreak=index \
|
||||||
--reverse \
|
--reverse \
|
||||||
@@ -127,12 +125,14 @@ while true; do
|
|||||||
--bind='page-up:preview-half-page-up,page-down:preview-half-page-down' \
|
--bind='page-up:preview-half-page-up,page-down:preview-half-page-down' \
|
||||||
--bind="enter,ctrl-l:execute:$0 --show-thread {4}" \
|
--bind="enter,ctrl-l:execute:$0 --show-thread {4}" \
|
||||||
--bind="alt-/:change-preview-window($FZF_ALTERNATE_PREVIEW_WINDOW|$FZF_DEFAULT_PREVIEW_WINDOW)" \
|
--bind="alt-/:change-preview-window($FZF_ALTERNATE_PREVIEW_WINDOW|$FZF_DEFAULT_PREVIEW_WINDOW)" \
|
||||||
|
--bind="alt-h:change-preview:$0 --preview-html-thread {4}" \
|
||||||
|
--bind="focus:change-preview:$0 --preview-thread {4}" \
|
||||||
--preview="$0 --preview-thread {4}" \
|
--preview="$0 --preview-thread {4}" \
|
||||||
--preview-window="$FZF_DEFAULT_PREVIEW_WINDOW" | head -1 || true)
|
--preview-window="$FZF_DEFAULT_PREVIEW_WINDOW" | head -1 || true)
|
||||||
[ -n "$cmd" ] || exit 0
|
[ -n "$cmd" ] || exit 0
|
||||||
case "$cmd" in
|
case "$cmd" in
|
||||||
$CMD_SEARCH_TAG*)
|
$CMD_SEARCH_TAG*)
|
||||||
tag=$(__notmuch_list_tags | $FZF \
|
tag=$(list_tags | $FZF \
|
||||||
--color="border:$ANSICOLORTAGS" \
|
--color="border:$ANSICOLORTAGS" \
|
||||||
--color="label:$ANSICOLORTAGS" \
|
--color="label:$ANSICOLORTAGS" \
|
||||||
--tac \
|
--tac \
|
||||||
@@ -155,7 +155,7 @@ while true; do
|
|||||||
field='to'
|
field='to'
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
address=$(__notmuch_list_address | $FZF \
|
address=$(list_addresses | $FZF \
|
||||||
--color="border:$ANSICOLORFROM" \
|
--color="border:$ANSICOLORFROM" \
|
||||||
--color="label:$ANSICOLORFROM" \
|
--color="label:$ANSICOLORFROM" \
|
||||||
--tiebreak=index \
|
--tiebreak=index \
|
||||||
@@ -175,7 +175,7 @@ while true; do
|
|||||||
esac
|
esac
|
||||||
;;
|
;;
|
||||||
$CMD_EDIT_QUERY*)
|
$CMD_EDIT_QUERY*)
|
||||||
nmquery=$($FZF \
|
nmquery=$(list_query_history | $FZF \
|
||||||
--color="border:$ANSICOLORSUBJ" \
|
--color="border:$ANSICOLORSUBJ" \
|
||||||
--color="label:$ANSICOLORSUBJ" \
|
--color="label:$ANSICOLORSUBJ" \
|
||||||
--tac \
|
--tac \
|
||||||
@@ -187,7 +187,7 @@ while true; do
|
|||||||
--prompt="Query: " \
|
--prompt="Query: " \
|
||||||
--bind='focus:replace-query' \
|
--bind='focus:replace-query' \
|
||||||
--border=double \
|
--border=double \
|
||||||
--border-label=' Query history ' < "$NMFHIST" | head -1 || true)
|
--border-label=' Query history ' | head -1 || true)
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
;;
|
;;
|
||||||
|
|||||||
@@ -1,11 +1,4 @@
|
|||||||
if [ ! "${AWK_LOADED:-}" ]; then
|
if [ ! "${AWK_LOADED:-}" ]; then
|
||||||
AWK_LASTMESSAGEINTHREAD=$(
|
|
||||||
cat <<'EOF'
|
|
||||||
@@include awk/lastmessageinthread.awk
|
|
||||||
EOF
|
|
||||||
)
|
|
||||||
export AWK_LASTMESSAGEINTHREAD
|
|
||||||
|
|
||||||
AWK_THREADOVERVIEW=$(
|
AWK_THREADOVERVIEW=$(
|
||||||
cat <<'EOF'
|
cat <<'EOF'
|
||||||
@@include awk/threadoverview.awk
|
@@include awk/threadoverview.awk
|
||||||
@@ -13,5 +6,26 @@ EOF
|
|||||||
)
|
)
|
||||||
export AWK_THREADOVERVIEW
|
export AWK_THREADOVERVIEW
|
||||||
|
|
||||||
|
AWK_MESSAGEHEADER=$(
|
||||||
|
cat <<'EOF'
|
||||||
|
@@include awk/messageheader.awk
|
||||||
|
EOF
|
||||||
|
)
|
||||||
|
export AWK_MESSAGEHEADER
|
||||||
|
|
||||||
|
AWK_MESSAGEPARTS=$(
|
||||||
|
cat <<'EOF'
|
||||||
|
@@include awk/messageparts.awk
|
||||||
|
EOF
|
||||||
|
)
|
||||||
|
export AWK_MESSAGEPARTS
|
||||||
|
|
||||||
|
AWK_PARTNR=$(
|
||||||
|
cat <<'EOF'
|
||||||
|
@@include awk/partnr.awk
|
||||||
|
EOF
|
||||||
|
)
|
||||||
|
export AWK_PARTNR
|
||||||
|
|
||||||
export AWK_LOADED=1
|
export AWK_LOADED=1
|
||||||
fi
|
fi
|
||||||
|
|||||||
32
src/sh/lists.sh
Normal file
32
src/sh/lists.sh
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
# These functions generate the lists that are fed into fzf
|
||||||
|
|
||||||
|
# List the messages within a thread
|
||||||
|
# @argument $1: thread id
|
||||||
|
list_messages_in_thread() {
|
||||||
|
$NOTMUCH show thread:"$1" \
|
||||||
|
| awk "$AWK_THREADOVERVIEW" \
|
||||||
|
| sed "s/^\(\S*\)\t\([^\t]*\)\t\(.*\) (\([^()]*\)) (\([^()]*\))$/${COLFROM}\3${COLRESET}\t${COLSUBJ}\2${COLRESET}\t${COLDATE}\4${COLRESET}\t${COLTAGS}\5${COLRESET}\t\1/"
|
||||||
|
}
|
||||||
|
|
||||||
|
# List all threads that match a query
|
||||||
|
# @argument $1: query
|
||||||
|
list_threads() {
|
||||||
|
$NOTMUCH search "$1" \
|
||||||
|
| sed "s/^thread:\([[:xdigit:]]\+\)\s\+\([^[]\+\)\s\+\[\([^]]\+\)\]\([^;]*\); \(.*\) (\([^(]*\))$/${COLDATE}\2${COLRESET}\t${COLCNTS}\3${COLRESET}\t${COLFROM}\4${COLRESET}\t${COLSUBJ}\5${COLRESET}\t${COLTAGS}\6${COLRESET}\t\1/" \
|
||||||
|
| column -s "$(printf '\t')" -t -l 3 -R 1,2
|
||||||
|
}
|
||||||
|
|
||||||
|
# List all available tags
|
||||||
|
list_tags() {
|
||||||
|
$NOTMUCH search --output=tags tag:/./
|
||||||
|
}
|
||||||
|
|
||||||
|
# List all email addresses
|
||||||
|
list_addresses() {
|
||||||
|
$NOTMUCH address '*'
|
||||||
|
}
|
||||||
|
|
||||||
|
# List query history
|
||||||
|
list_query_history() {
|
||||||
|
cat "$NMFHIST"
|
||||||
|
}
|
||||||
33
src/sh/notmuch.sh
Normal file
33
src/sh/notmuch.sh
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
# Wrapper around notmuch
|
||||||
|
|
||||||
|
# Print the message id of the last message within a thread.
|
||||||
|
# @argument $1: thread id
|
||||||
|
nm_last_message_in_thread() {
|
||||||
|
$NOTMUCH search --output=messages --offset=-1 thread:"$1" | sed 's/^...//'
|
||||||
|
}
|
||||||
|
|
||||||
|
# Print the header of a message (with trailing empty line)
|
||||||
|
# @argument $1: message id
|
||||||
|
nm_message_header() {
|
||||||
|
$NOTMUCH show --body=false id:"$1" | awk "$AWK_MESSAGEHEADER"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Print the message parts (with indents)
|
||||||
|
# @argument $1: message id
|
||||||
|
nm_message_parts() {
|
||||||
|
$NOTMUCH show --body=true id:"$1" | awk "$AWK_MESSAGEPARTS"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Print the message part number for text/plain or text/html
|
||||||
|
# @argument $1: message id
|
||||||
|
# @argument $2: if set, then the html part number will be printed
|
||||||
|
nm_message_part_nr() {
|
||||||
|
$NOTMUCH show --body=true id:"$1" | awk -v html="${2:-}" "$AWK_PARTNR"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Print part of message
|
||||||
|
# @argument $1: message id
|
||||||
|
# @argument $2: part number
|
||||||
|
nm_message_get_part() {
|
||||||
|
$NOTMUCH show --part="$2" id:"$1"
|
||||||
|
}
|
||||||
21
src/sh/preview.sh
Normal file
21
src/sh/preview.sh
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
# Preview functions
|
||||||
|
|
||||||
|
# Preview email
|
||||||
|
# If a thread identifier is given, then the last message in the thread is
|
||||||
|
# shown. If the HTML flag is set, then the HTML message is parsed using pandoc.
|
||||||
|
# This functions reverts to the HTML method if no HTML flag is set yet no
|
||||||
|
# text/plain part is found.
|
||||||
|
#
|
||||||
|
# @argument $1: "id" or "thread"
|
||||||
|
# @argument $2: message id or thread id
|
||||||
|
# @argument #3: if set, then the HTML messages will be taken
|
||||||
|
preview_message() {
|
||||||
|
[ "$1" = "id" ] && messageid="$2" || messageid="$(nm_last_message_in_thread "$2")"
|
||||||
|
parts="$(nm_message_parts "$messageid")"
|
||||||
|
html="${3:-}"
|
||||||
|
nr="$(nm_message_part_nr "$messageid" "$html")"
|
||||||
|
[ ! "$nr" ] && [ ! "$html" ] && html="html" && nr="$(nm_message_part_nr "$messageid" "html")"
|
||||||
|
[ "$html" ] && parser="$PANDOC" || parser="cat"
|
||||||
|
nm_message_header "$messageid" | $CATEMAIL
|
||||||
|
nm_message_get_part "$messageid" "$nr" | $parser | $CATMD
|
||||||
|
}
|
||||||
@@ -20,9 +20,17 @@ if [ ! "${TOOLS_LOADED:-}" ]; then
|
|||||||
elif command -v "batcat" >/dev/null; then
|
elif command -v "batcat" >/dev/null; then
|
||||||
CAT="batcat"
|
CAT="batcat"
|
||||||
fi
|
fi
|
||||||
CAT=${CAT:+$CAT -l email --style=plain --color=always --wrap}
|
CATEMAIL=${CAT:+$CAT -l email --style=plain --color=always}
|
||||||
CAT=${CAT:-cat}
|
CATMD=${CAT:+$CAT -l markdown --style=plain --color=always}
|
||||||
export CAT
|
CATEMAIL=${CATEMAIL:-cat}
|
||||||
|
CATEMD=${CATMD:-cat}
|
||||||
|
export CATEMAIL CATMD
|
||||||
|
|
||||||
|
if command -v "pandoc" >/dev/null; then
|
||||||
|
PANDOC="pandoc --from html --to markdown_strict-raw_html"
|
||||||
|
fi
|
||||||
|
PANDOC=${PANDOC:-cat}
|
||||||
|
export PANDOC
|
||||||
|
|
||||||
export TOOLS_LOADED=1
|
export TOOLS_LOADED=1
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user