rohrpost

A commandline mail client to change the world as we see it.
git clone git://r-36.net/rohrpost
Log | Files | Refs | README | LICENSE

print.sh (2066B)


      1 #!/bin/bash
      2 
      3 # structure
      4 reset="$(tput sgr0)"
      5 idformat="$(tput bold)"
      6 keyformat="$(tput setaf 3)"
      7 valueformat="$(tput setaf 7)"
      8 partformat="$(tput setaf 3)"
      9 alertformat="$(tput setaf 1; tput bold; tput blink;)"
     10 
     11 # body text
     12 textformat="$reset"
     13 quoteformat="$(tput setaf 6)"
     14 headerformat="$(tput setaf 8)"
     15 
     16 italicformat="$(tput sitm)"
     17 italicreset="$(tput ritm)"
     18 boldformat="$(tput bold)"
     19 boldreset="$(echo -ne "\033[22m")"
     20 underlineformat="$(tput smul)"
     21 underlinereset="$(tput rmul)"
     22 reverseformat="$(tput rev)"
     23 reversereset="$(echo -ne "\033[27m")"
     24 
     25 state="id"
     26 
     27 function formatheader {
     28 	line="$1"
     29 	if [ "$line" = "" ] || [ -z "$line" ];
     30 	then
     31 		printf "\n"
     32 		state="body"
     33 		return
     34 	fi
     35 
     36 	# GNU sed sucks at UTF-8
     37 	key=$(echo "$line" | perl -pe 's/: .*//')
     38 	value=$(echo "$line" | perl -pe 's/[A-Za-z0-9-]*: //')
     39 
     40 	uvformat="${valueformat}"
     41 	if [ "$key" = "X-Mailer" ];
     42 	then
     43 		case "$value" in
     44 		"Apple Mail"*|"iPhone Mail"*)
     45 			uvformat="${alertformat}"
     46 			;;
     47 		esac
     48 	fi
     49 
     50 	printf "${keyformat}%s: ${reset}${uvformat}%s${reset}\n" \
     51 		"$key" "$value"
     52 }
     53 
     54 IFS="\\"
     55 while read -r line;
     56 do
     57 	if [ "$line" = "" ];
     58 	then
     59 		printf "\n"
     60 		state="id"
     61 		continue
     62 	fi
     63 
     64 	case $state in
     65 	id)
     66 		te=$(echo -n "$line" | grep "^--")
     67 		if [ -n "$te" ];
     68 		then
     69 			printf "${idformat}%s${reset}\n" "$line"
     70 		else
     71 			formatheader "$line"
     72 		fi
     73 		state="header"
     74 		;;
     75 	header)
     76 		formatheader "$line"
     77 		;;
     78 	body)
     79 		line=$(printf "%s" "$line" | sed 's///g')
     80 		case $line in
     81 		--MIME-Part*)
     82 			printf "${partformat}%s${reset}\n" "$line"
     83 			;;
     84 		--*)
     85 			printf "${headerformat}%s${reset}\n" "$line"
     86 			;;
     87 		++*)
     88 			printf "${headerformat}%s${reset}\n" "$line"
     89 			;;
     90 		\>*|\ \>*)
     91 			printf "${quoteformat}%s${reset}\n" "$line"
     92 			;;
     93 		*)
     94 			line=$(printf "%s" "$line" \
     95 				| sed "s,\(\*[^ \t\v\r\f]*\*\),${boldformat}\1${boldreset},g" \
     96 				| sed "s,\(\/[^ \t\v\r\f]*\/\),${italicformat}\1${italicreset},g" \
     97 				| sed "s,\(_[^ \t\v\r\f]*_\),${underlineformat}\1${underlinereset},g")
     98 			printf "${textformat}%s${reset}\n" "$line"
     99 			;;
    100 		esac
    101 		;;
    102 	*)
    103 		echo "unknown state"
    104 		;;
    105 	esac
    106 done
    107