From 69e00b6f58b9b0a76e6227d32f436783ca8b0aa8 Mon Sep 17 00:00:00 2001 From: Lucas Date: Wed, 11 Mar 2020 23:25:28 +0000 Subject: [PATCH] Add mblaze related files --- mblaze/Makefile | 34 ++++++++++++ mblaze/env | 72 ++++++++++++++++++++++++ mblaze/m.sh | 117 +++++++++++++++++++++++++++++++++++++++ mblaze/mless-key-handler | 41 ++++++++++++++ mblaze/mless.sh | 114 ++++++++++++++++++++++++++++++++++++++ mblaze/mlesskey | 20 +++++++ 6 files changed, 398 insertions(+) create mode 100644 mblaze/Makefile create mode 100644 mblaze/env create mode 100644 mblaze/m.sh create mode 100644 mblaze/mless-key-handler create mode 100644 mblaze/mless.sh create mode 100644 mblaze/mlesskey diff --git a/mblaze/Makefile b/mblaze/Makefile new file mode 100644 index 0000000..7f688de --- /dev/null +++ b/mblaze/Makefile @@ -0,0 +1,34 @@ +# mblaze.env +# Written in 2020 by Lucas +# CC0 1.0 Universal/Public domain - No rights reserved +# +# To the extent possible under law, the author(s) have dedicated all +# copyright and related and neighboring rights to this software to the +# public domain worldwide. This software is distributed without any +# warranty. You should have received a copy of the CC0 Public Domain +# Dedication along with this software. If not, see +# . +.POSIX: +.SUFFIXES: +.SUFFIXES: .sh + +PREFIX = ${HOME}/.mblaze + +BIN = m mless + +all: ${BIN} + +clean: + rm -f ${BIN} + +install: all env mlesskey mless-key-handler + mkdir -pm 700 ${PREFIX} + lesskey -o ${PREFIX}/mless mlesskey + cp -f env mless-key-handler ${PREFIX} + mkdir -p ${PREFIX}/bin + cp -f ${BIN} ${PREFIX}/bin + cd ${PREFIX}/bin && chmod 555 ${BIN} + +uninstall: + cd ${PREFIX}/bin && rm -f ${BIN} + rm -f ${PREFIX}/env ${PREFIX}/mless ${PREFIX}/mless-key-handler diff --git a/mblaze/env b/mblaze/env new file mode 100644 index 0000000..32fa470 --- /dev/null +++ b/mblaze/env @@ -0,0 +1,72 @@ +# mblaze.env +# Written in 2020 by Lucas +# CC0 1.0 Universal/Public domain - No rights reserved +# +# To the extent possible under law, the author(s) have dedicated all +# copyright and related and neighboring rights to this software to the +# public domain worldwide. This software is distributed without any +# warranty. You should have received a copy of the CC0 Public Domain +# Dedication along with this software. If not, see +# . + +if [ -z "$mblaze_account" ]; then + printf "Usage: mblaze_account=example.com . ~/.mblaze/env\n" >&2 + return 1 +fi +MBLAZE=~/.mblaze/$mblaze_account + +if [ ! -d "$MBLAZE" ]; then + printf "Unknown account \"%s\"\n" "$mblaze_account" >&2 + return 1 +fi +export MBLAZE + +if [ -d ~/.mblaze/bin ]; then + PATH=~/.mblaze/bin:$PATH + export PATH +fi + +# account in prompt +PS1='@$mblaze_account \$ ' +export PS1 +# mless colors +MCOLOR_CUR=10 +MCOLOR_MISS=1 +export MCOLOR_CUR MCOLOR_MISS +# mcolor colors +MCOLOR_SEP=7 +MCOLOR_FROM=14 +MCOLOR_HEADER=6 +MCOLOR_FOOTER=7 +MCOLOR_SIG=5 +MCOLOR_QQQUOTE=3 +MCOLOR_QQUOTE=3 +MCOLOR_QUOTE=3 +export MCOLOR_SEP MCOLOR_FROM MCOLOR_HEADER MCOLOR_FOOTER MCOLOR_SIG +export MCOLOR_QQQUOTE MCOLOR_QQUOTE MCOLOR_QUOTE + +if [ -f "$MBLAZE/private.env" ]; then + . "$MBLAZE/private.env" +fi + +mfetch() +{ + command -v _account_mfetch >/dev/null 2>&1 && + _account_mfetch "$mblaze_account" + + lockfile=%h/mail/.fdm-$mblaze_account-lock + fdm -qa "$mblaze_account" -D "\$lockfile=$lockfile" fetch || return 1 + mcount=$(mdirs ~/mail/"$mblaze_account" | xargs minc | wc -l) + mcount=$(printf "%u\n" "$mcount") + prefix="$(date "+%a %d %b %T") [$mblaze_account]" + case $mcount in + 0) printf "%s: no new emails.\n" "$prefix" + ;; + 1) printf "%s: 1 new email.\n" "$prefix" + ;; + *) printf "%s: %u new emails.\n" "$prefix" "$mcount" + ;; + esac +} + +cd ~/mail/"$mblaze_account" diff --git a/mblaze/m.sh b/mblaze/m.sh new file mode 100644 index 0000000..013b8a0 --- /dev/null +++ b/mblaze/m.sh @@ -0,0 +1,117 @@ +#!/bin/sh +# mblaze.env +# Written in 2020 by Lucas +# CC0 1.0 Universal/Public domain - No rights reserved +# +# To the extent possible under law, the author(s) have dedicated all +# copyright and related and neighboring rights to this software to the +# public domain worldwide. This software is distributed without any +# warranty. You should have received a copy of the CC0 Public Domain +# Dedication along with this software. If not, see +# . + +usage() +{ + cat - <<. >&2 +Usage: + ${0##*/} f|fetch + ${0##*/} l|list [dirs...] + ${0##*/} r|read dir [dirs...] + ${0##*/} s|scan [-a] dir [dirs...] +. + exit 1 +} + +_mdirs() +{ + mdirs "$@" | sort -t / +} + +m_fetch_usage() +{ + printf "Usage: %s f|fetch\n" "${0##*/}" >&2 + exit 1 +} + +m_read_usage() +{ + printf "Usage: %s r|read dir [dirs...]\n" "${0##*/}" >&2 + exit 1 +} + +m_scan_usage() +{ + printf "Usage: %s s|scan [-a] dir [dirs...]\n" "${0##*/}" >&2 + exit 1 +} + +m_fetch() +{ + if [ $# -ne 0 ]; then + m_fetch_usage + fi + printf "%s: fetch TBD.\n" >&2 + exit 1 +} + +m_list() +{ + pwd=$(pwd) + # cut(1) lists are 1-based, and +1 for leading slash + _mdirs "${@:-.}" | cut -c $((${#pwd} + 2))- | mlist -i +} + +m_read() +{ + if [ $# -eq 0 ]; then + m_read_usage + fi + + _mdirs "$@" | mlist -s | msort -d | mthread | mless +} + +m_scan() +{ + mlist_flags=-s + while getopts a flag; do + case $flag in + a) + mlist_flags= + ;; + *) + m_scan_usage + ;; + esac + done + shift $(($OPTIND - 1)) + + if [ $# -eq 0 ]; then + m_scan_usage + fi + + _mdirs "$@" | mlist $mlist_flags | msort -d | mthread | mseq -S | mscan +} + +if [ $# -lt 1 ]; then + usage +fi +cmd=$1 +shift + +case $cmd in +f|fetch) + m_fetch "$@" + ;; +l|list) + m_list "$@" + ;; +r|read) + m_read "$@" + ;; +s|scan) + m_scan "$@" + ;; +*) + usage + ;; +esac diff --git a/mblaze/mless-key-handler b/mblaze/mless-key-handler new file mode 100644 index 0000000..a97c674 --- /dev/null +++ b/mblaze/mless-key-handler @@ -0,0 +1,41 @@ +#!/bin/sh +# mblaze.env +# Written in 2020 by Lucas +# CC0 1.0 Universal/Public domain - No rights reserved +# +# To the extent possible under law, the author(s) have dedicated all +# copyright and related and neighboring rights to this software to the +# public domain worldwide. This software is distributed without any +# warranty. You should have received a copy of the CC0 Public Domain +# Dedication along with this software. If not, see +# . + +keyhandler() +{ + case "$1" in + 0|1) + exit $1 + ;; + 70) # F flag + mflag -F . + mseq -f : | mseq -S + mseq -C + + ;; + 82) # R mark read + mflag -S . + mseq -f : | mseq -S + mseq -C + + ;; + 94) # ^ goto parent + mseq -C '.^' 2>/dev/null + ;; + 123) # { prev thread + prev=$(mseq .-1 | sed 's/^ *//') + prev_root=$(mseq -c "$prev" = | head -n 1 | sed 's/^ *//') + mseq -C "$prev_root" 2>/dev/null + ;; + 125) # } next thread + mseq -C "$(mseq .+1: | sed -n '/^[^ <]/{p;q;}')" + ;; + esac +} diff --git a/mblaze/mless.sh b/mblaze/mless.sh new file mode 100644 index 0000000..9bde47b --- /dev/null +++ b/mblaze/mless.sh @@ -0,0 +1,114 @@ +#!/bin/sh +# mless [MSG] - less(1)-wrapper around mshow + +colorscan() { +awk ' +function co(n, c) { e = ENVIRON["MCOLOR_" n]; return e ? e : c } +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(co("CUR",119), $0)); next } +/^ *\\_/ { print fg(co("MISS",242), $0); next } +{ print }' +} + +keyhandler() { + case "$?" in + 0|1) exit $?;; + 36) # $ goto end + mseq -C '$' 2>/dev/null + ;; + 78) # N go to next unseen message + nu=$(magrep -v -m1 :S .:) && mseq -C "$nu" + ;; + 107) # k next thread + mseq -C "$(mseq .+1: | sed -n '/^[^ <]/{p;q;}')" + ;; + 100) # d mark read + mflag -S . + mseq -f : | mseq -S + mseq -C + + ;; + 82) # R toggle raw mode + MLESS_RAW=$((1-$MLESS_RAW)) + ;; + 72) # H toggle HTML mode + MLESS_HTML=$((1-$MLESS_HTML)) + ;; + 94) # ^ goto parent + mseq -C '.^' 2>/dev/null + ;; + esac +} + +if [ -n "${NO_COLOR+set}" ]; then + colorscan() { cat -; } +fi + +if [ "$1" = --filter ]; then + if [ "$2" = //scan ]; then + mscan : 2>/dev/null | colorscan + exit $? + fi + + mseq -C "$2" + mscan .-2:.+3 2>/dev/null | colorscan + echo + + if ! [ -f "$(mseq -r "$2")" ]; then + mseq "$2" + exit + fi + + if [ $MLESS_RAW -eq 0 ]; then + if [ $MLESS_HTML -eq 1 ]; then + mshow -A text/html "$2" + else + mshow "$2" + fi | mcolor + else + mseq -r "$2" + echo + cat "$(mseq -r "$2")" + fi + exit $? +fi + +if [ "$#" -eq 0 ] && ! [ -t 0 ]; then + mseq -S >/dev/null + set -- : +fi + +if ! [ -t 1 ]; then + exec mseq : +fi + +[ "$#" -eq 1 ] && set -- ${1:-.} + +if [ "$#" -ge 1 ]; then + mseq -C "$1" +fi + +nl=" +" +export MLESS_RAW=0 +export MLESS_HTML=0 +if [ -f "$MBLAZE/mless" ]; then + export LESSKEY="$MBLAZE/mless" +elif [ -f "$HOME/.mblaze/mless" ]; then + export LESSKEY="$HOME/.mblaze/mless" +elif [ -f "$HOME/.mless" ]; then + export LESSKEY="$HOME/.mless" +fi +if [ -f "$MBLAZE/mless-key-handler" ]; then + . "$MBLAZE/mless-key-handler" +elif [ -f "$HOME/.mblaze/mless-key-handler" ]; then + . "$HOME/.mblaze/mless-key-handler" +elif [ -f "$HOME/.mless-key-handler" ]; then + . "$HOME/.mless-key-handler" +fi +while :; do + LESSOPEN="|$0 --filter %s" \ + less -Ps"mless %f?m (message %i of %m).." -R \ + "+:e $(mscan -n .)$nl" //scan $(mscan -n :) + keyhandler $? +done diff --git a/mblaze/mlesskey b/mblaze/mlesskey new file mode 100644 index 0000000..d5a9bb4 --- /dev/null +++ b/mblaze/mlesskey @@ -0,0 +1,20 @@ +# mblaze.env +# Written in 2020 by Lucas +# CC0 1.0 Universal/Public domain - No rights reserved +# +# To the extent possible under law, the author(s) have dedicated all +# copyright and related and neighboring rights to this software to the +# public domain worldwide. This software is distributed without any +# warranty. You should have received a copy of the CC0 Public Domain +# Dedication along with this software. If not, see +# . +Q quit \1 +[ prev-file +] next-file +S noaction E//scan\n +` noaction E\#\n +F quit F +R quit R +{ quit { +} quit } +\^ quit \^