about summary refs log tree commit diff stats
path: root/html/024compare.cc.html
Commit message (Expand)AuthorAgeFilesLines
* 2062Kartik K. Agaram2015-08-231-63/+62
* 1949Kartik K. Agaram2015-08-061-48/+49
* 1925Kartik K. Agaram2015-08-031-1/+1
* 1885Kartik K. Agaram2015-07-291-80/+79
* 1853Kartik K. Agaram2015-07-251-40/+78
* 1766Kartik K. Agaram2015-07-121-5/+5
* 1631 - update html versionsKartik K. Agaram2015-06-231-4/+4
* 1556Kartik K. Agaram2015-06-121-1/+1
* 1549Kartik K. Agaram2015-06-091-2/+2
* 1517Kartik K. Agaram2015-05-301-3/+3
* 1461 - descriptions/table of contents for the layersKartik K. Agaram2015-05-261-1/+1
* 1459Kartik K. Agaram2015-05-251-82/+14
* 1376 - update github docsKartik K. Agaram2015-05-141-97/+163
* 1291Kartik K. Agaram2015-05-061-1/+1
* 1279 - colorized rendering of the source filesKartik K. Agaram2015-05-061-0/+298
Drew DeVault <sir@cmpwn.com> 2019-03-31 12:14:37 -0400 Make the message viewer real, part one' href='/akspecs/aerc/commit/widgets/msgviewer.go?h=0.2.0&id=27b25174e2f0249a6a1d4ba45b70f8504b63ffb1'>27b2517 ^
fa04a1e ^
27b2517 ^

fa04a1e ^
fa04a1e ^
27b2517 ^
fa04a1e ^




27b2517 ^
fa04a1e ^










27b2517 ^
fa04a1e ^



27b2517 ^
fa04a1e ^


27b2517 ^

fa04a1e ^
27b2517 ^
337dd18 ^






fa04a1e ^
27b2517 ^
fa04a1e ^

27b2517 ^






0abafa6 ^

27b2517 ^




0abafa6 ^

27b2517 ^
fa04a1e ^
0abafa6 ^



27b2517 ^
fa04a1e ^

0abafa6 ^








fa04a1e ^





























27b2517 ^



4bdc0f3 ^




337dd18 ^
4bdc0f3 ^

337dd18 ^
4bdc0f3 ^

337dd18 ^
4bdc0f3 ^
27b2517 ^

fa04a1e ^










337dd18 ^





















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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204



               
             


                 
                                     


                                       
                                       
                                          
                                                


                           




                             

 















                                                                                    
 

                                                
 
                                                
                                                                           




                                    
                                                                       










                                                   
                                                                  



                                    
                                                                


                            

                                                    
                                      
                                                             






                                                
 
                                   

                                   






                                                                           

                           




                                                                

                                      
          
 



                                    
                     

 








                                                      





























                                                               



                                                             




                                                    
                             

                                                                        
                

                                                      
         
                                                              

                                          










                                                            





















                                                                             
package widgets

import (
	"bytes"
	"fmt"
	"io"
	"os/exec"

	"github.com/emersion/go-imap"
	"github.com/gdamore/tcell"
	"github.com/mattn/go-runewidth"

	"git.sr.ht/~sircmpwn/aerc2/lib"
	"git.sr.ht/~sircmpwn/aerc2/lib/ui"
	"git.sr.ht/~sircmpwn/aerc2/worker/types"
)

type MessageViewer struct {
	cmd    *exec.Cmd
	source io.Reader
	sink   io.WriteCloser
	grid   *ui.Grid
	term   *Terminal
}

func formatAddresses(addrs []*imap.Address) string {
	val := bytes.Buffer{}
	for i, addr := range addrs {
		if addr.PersonalName != "" {
			val.WriteString(fmt.Sprintf("%s <%s@%s>",
				addr.PersonalName, addr.MailboxName, addr.HostName))
		} else {
			val.WriteString(fmt.Sprintf("%s@%s",
				addr.MailboxName, addr.HostName))
		}
		if i != len(addrs)-1 {
			val.WriteString(", ")
		}
	}
	return val.String()
}

func NewMessageViewer(store *lib.MessageStore,
	msg *types.MessageInfo) *MessageViewer {

	grid := ui.NewGrid().Rows([]ui.GridSpec{
		{ui.SIZE_EXACT, 3}, // TODO: Based on number of header rows
		{ui.SIZE_WEIGHT, 1},
	}).Columns([]ui.GridSpec{
		{ui.SIZE_WEIGHT, 1},
	})

	// TODO: let user specify additional headers to show by default
	headers := ui.NewGrid().Rows([]ui.GridSpec{
		{ui.SIZE_EXACT, 1},
		{ui.SIZE_EXACT, 1},
		{ui.SIZE_EXACT, 1},
	}).Columns([]ui.GridSpec{
		{ui.SIZE_WEIGHT, 1},
		{ui.SIZE_WEIGHT, 1},
	})
	headers.AddChild(
		&HeaderView{
			Name:  "From",
			Value: formatAddresses(msg.Envelope.From),
		}).At(0, 0)
	headers.AddChild(
		&HeaderView{
			Name:  "To",
			Value: formatAddresses(msg.Envelope.To),
		}).At(0, 1)
	headers.AddChild(
		&HeaderView{
			Name:  "Subject",
			Value: msg.Envelope.Subject,
		}).At(1, 0).Span(1, 2)
	headers.AddChild(ui.NewFill(' ')).At(2, 0).Span(1, 2)

	body := ui.NewGrid().Rows([]ui.GridSpec{
		{ui.SIZE_WEIGHT, 1},
	}).Columns([]ui.GridSpec{
		{ui.SIZE_WEIGHT, 1},
		{ui.SIZE_EXACT, 20},
	})

	cmd := exec.Command("less")
	pipe, _ := cmd.StdinPipe()
	term, _ := NewTerminal(cmd)
	// TODO: configure multipart view. I left a spot for it in the grid
	body.AddChild(term).At(0, 0).Span(1, 2)

	grid.AddChild(headers).At(0, 0)
	grid.AddChild(body).At(1, 0)

	viewer := &MessageViewer{
		cmd:  cmd,
		sink: pipe,
		grid: grid,
		term: term,
	}

	store.FetchBodyPart(msg.Uid, 0, func(reader io.Reader) {
		viewer.source = reader
		viewer.attemptCopy()
	})

	term.OnStart = func() {
		viewer.attemptCopy()
	}

	return viewer
}

func (mv *MessageViewer) attemptCopy() {
	if mv.source != nil && mv.cmd.Process != nil {
		go func() {
			io.Copy(mv.sink, mv.source)
			mv.sink.Close()
		}()
	}
}

func (mv *MessageViewer) Draw(ctx *ui.Context) {
	mv.grid.Draw(ctx)
}

func (mv *MessageViewer) Invalidate() {
	mv.grid.Invalidate()
}

func (mv *MessageViewer) OnInvalidate(fn func(d ui.Drawable)) {
	mv.grid.OnInvalidate(func(_ ui.Drawable) {
		fn(mv)
	})
}

func (mv *MessageViewer) Event(event tcell.Event) bool {
	return mv.term.Event(event)
}

func (mv *MessageViewer) Focus(focus bool) {
	mv.term.Focus(focus)
}

type HeaderView struct {
	onInvalidate func(d ui.Drawable)

	Name  string
	Value string
}

func (hv *HeaderView) Draw(ctx *ui.Context) {
	name := hv.Name
	size := runewidth.StringWidth(name)
	lim := ctx.Width() - size - 1
	value := runewidth.Truncate(" "+hv.Value, lim, "…")
	var (
		hstyle tcell.Style
		vstyle tcell.Style
	)
	// TODO: Make this more robust and less dumb
	if hv.Name == "PGP" {
		vstyle = tcell.StyleDefault.Foreground(tcell.ColorGreen)
		hstyle = tcell.StyleDefault.Bold(true)
	} else {
		vstyle = tcell.StyleDefault
		hstyle = tcell.StyleDefault.Bold(true)
	}
	ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', vstyle)
	ctx.Printf(0, 0, hstyle, name)
	ctx.Printf(size, 0, vstyle, value)
}

func (hv *HeaderView) Invalidate() {
	if hv.onInvalidate != nil {
		hv.onInvalidate(hv)
	}
}

func (hv *HeaderView) OnInvalidate(fn func(d ui.Drawable)) {
	hv.onInvalidate = fn
}

type MultipartView struct {
	onInvalidate func(d ui.Drawable)
}

func (mpv *MultipartView) Draw(ctx *ui.Context) {
	ctx.Fill(0, 0, ctx.Width(), ctx.Height(), ' ', tcell.StyleDefault)
	ctx.Fill(0, 0, ctx.Width(), 1, ' ', tcell.StyleDefault.Reverse(true))
	ctx.Printf(0, 0, tcell.StyleDefault.Reverse(true), "text/plain")
	ctx.Printf(0, 1, tcell.StyleDefault, "text/html")
	ctx.Printf(0, 2, tcell.StyleDefault, "application/pgp-si…")
}

func (mpv *MultipartView) Invalidate() {
	if mpv.onInvalidate != nil {
		mpv.onInvalidate(mpv)
	}
}

func (mpv *MultipartView) OnInvalidate(fn func(d ui.Drawable)) {
	mpv.onInvalidate = fn
}