-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.message-formatter
executable file
·77 lines (71 loc) · 3.09 KB
/
.message-formatter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env python
# This is ~/.message-formatter - a mutt display filter
# to format an email message for the screen width
# (C) Silas S. Brown 2007,2010,2012,2020,2022. Version 1.43.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Where to find history:
# on GitHub at https://github.com/ssb22/config
# and on GitLab at https://gitlab.com/ssb22/config
# and on BitBucket https://bitbucket.org/ssb22/config
# and at https://gitlab.developers.cam.ac.uk/ssb22/config
# and in China: https://gitee.com/ssb22/config
import sys,os,subprocess
try: width = sys.argv[1]
except: width = "48" # probably means some script without terminal ran mutt in background
quote_level = -1 # in header
quote_prefix = []
para = [] ; para_start = 1
try: import textwrap
except: textwrap = None
def output_para(para,quote_prefix):
fmt = "fmt -"+width ; para = ''.join(para)
if len(para) > 3000: # don't try to optimise, if the paragraph is too big (especially if contains CJK)
if textwrap:
for l in textwrap.wrap(para,int(width)):
p=''.join(quote_prefix)+l
if type(p)==type(u"") and not type("")==type(u""): p=p.encode("utf-8")
print(p)
return
fmt += " -s"
if quote_prefix: cmd = fmt+" '--prefix="+''.join(quote_prefix)+"'"
else: cmd = fmt
sp=subprocess.Popen(cmd,shell=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,universal_newlines=True)
if type(u"")==type(""): out,err = sp.communicate(''.join(para)) # Python 3
else: out,err = sp.communicate(''.join(para).encode('utf-8')) # Python 2
sys.stdout.write(out)
while 1:
r = sys.stdin.readline().replace("\r\n","\n")
if not r: break
try: r = r.decode('utf-8') # for Python 2
except: pass # Python 3 already decoded, or not UTF-8
if r=="\n" or r=="-- \n": quote_level,para_start = 0,1
new_quote_level = 0 ; new_quote_prefix = []
# Figure out the quote type for just this line:
for c in r:
if c in "|>:}#": new_quote_level += 1
elif not c==' ': break
new_quote_prefix.append(c)
if not new_quote_level==quote_level: para_start=1
if para_start and para:
# Start of new paragraph, so format the previous one if any
output_para(para,quote_prefix)
quote_prefix = new_quote_prefix ; new_quote_prefix = []
para = []
para_start = 0
if quote_level==-1 or r=="-- \n" or r=="\n":
if type(u"")==type(""): sys.stdout.write(r) # Python 3
elif type(r)==type(u""): sys.stdout.write(r.encode('utf-8')) # Python 2
else: sys.stdout.write(repr(r)+'\n') # Python 2 + wasn't valid UTF-8
else: para.append(r)
if not quote_level==-1: quote_level = new_quote_level
if para: output_para(para,quote_prefix)