summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorReto Brunner <reto@labrat.space>2020-10-14 08:42:26 +0200
committerReto Brunner <reto@labrat.space>2020-10-14 08:42:26 +0200
commit75cbf767326dfc67325773850ab1f6451dc9fb14 (patch)
tree4b47de3da4944ed2a84a7f175a742fda3dd2ea24
parentb6bcf89784605aa8baa982677de9d8d8ddbaa404 (diff)
downloadaerc-75cbf767326dfc67325773850ab1f6451dc9fb14.tar.gz
refactor ParseMessageFormat to use a ctx object
-rw-r--r--config/triggers.go9
-rw-r--r--lib/format/format.go114
-rw-r--r--widgets/msglist.go11
3 files changed, 74 insertions, 60 deletions
diff --git a/config/triggers.go b/config/triggers.go
index 3187cf7..847a10f 100644
--- a/config/triggers.go
+++ b/config/triggers.go
@@ -37,9 +37,12 @@ func (trig *TriggersConfig) ExecNewEmail(account *AccountConfig,
 	err := trig.ExecTrigger(trig.NewEmail,
 		func(part string) (string, error) {
 			formatstr, args, err := format.ParseMessageFormat(
-				account.From,
-				part,
-				conf.Ui.TimestampFormat, account.Name, 0, msg, false)
+				part, conf.Ui.TimestampFormat,
+				format.Ctx{
+					FromAddress: account.From,
+					AccountName: account.Name,
+					MsgInfo:     msg},
+			)
 			if err != nil {
 				return "", err
 			}
diff --git a/lib/format/format.go b/lib/format/format.go
index 66dced1..787821a 100644
--- a/lib/format/format.go
+++ b/lib/format/format.go
@@ -48,20 +48,26 @@ func FormatAddresses(l []*models.Address) string {
 	return strings.Join(formatted, ", ")
 }
 
-func ParseMessageFormat(
-	fromAddress string,
-	format string, timestampformat string,
-	accountName string, number int, msg *models.MessageInfo,
-	marked bool) (string,
+type Ctx struct {
+	FromAddress string
+	AccountName string
+	MsgNum      int
+	MsgInfo     *models.MessageInfo
+	MsgIsMarked bool
+}
+
+func ParseMessageFormat(format string, timeFmt string, ctx Ctx) (string,
 	[]interface{}, error) {
 	retval := make([]byte, 0, len(format))
 	var args []interface{}
 
-	accountFromAddress, err := ParseAddress(fromAddress)
+	accountFromAddress, err := ParseAddress(ctx.FromAddress)
 	if err != nil {
 		return "", nil, err
 	}
 
+	envelope := ctx.MsgInfo.Envelope
+
 	var c rune
 	for i, ni := 0, 0; i < len(format); {
 		ni = strings.IndexByte(format[i:], '%')
@@ -108,80 +114,80 @@ func ParseMessageFormat(
 		case '%':
 			retval = append(retval, '%')
 		case 'a':
-			if msg.Envelope == nil {
+			if envelope == nil {
 				return "", nil,
 					errors.New("no envelope available for this message")
 			}
-			if len(msg.Envelope.From) == 0 {
+			if len(envelope.From) == 0 {
 				return "", nil,
 					errors.New("found no address for sender")
 			}
-			addr := msg.Envelope.From[0]
+			addr := envelope.From[0]
 			retval = append(retval, 's')
 			args = append(args, addr.Address)
 		case 'A':
-			if msg.Envelope == nil {
+			if envelope == nil {
 				return "", nil,
 					errors.New("no envelope available for this message")
 			}
 			var addr *models.Address
-			if len(msg.Envelope.ReplyTo) == 0 {
-				if len(msg.Envelope.From) == 0 {
+			if len(envelope.ReplyTo) == 0 {
+				if len(envelope.From) == 0 {
 					return "", nil,
 						errors.New("found no address for sender or reply-to")
 				} else {
-					addr = msg.Envelope.From[0]
+					addr = envelope.From[0]
 				}
 			} else {
-				addr = msg.Envelope.ReplyTo[0]
+				addr = envelope.ReplyTo[0]
 			}
 			retval = append(retval, 's')
 			args = append(args, addr.Address)
 		case 'C':
 			retval = append(retval, 'd')
-			args = append(args, number)
+			args = append(args, ctx.MsgNum)
 		case 'd':
-			date := msg.Envelope.Date
+			date := envelope.Date
 			if date.IsZero() {
-				date = msg.InternalDate
+				date = ctx.MsgInfo.InternalDate
 			}
 			retval = append(retval, 's')
 			args = append(args,
-				dummyIfZeroDate(date.Local(), timestampformat))
+				dummyIfZeroDate(date.Local(), timeFmt))
 		case 'D':
-			date := msg.Envelope.Date
+			date := envelope.Date
 			if date.IsZero() {
-				date = msg.InternalDate
+				date = ctx.MsgInfo.InternalDate
 			}
 			retval = append(retval, 's')
 			args = append(args,
-				dummyIfZeroDate(date.Local(), timestampformat))
+				dummyIfZeroDate(date.Local(), timeFmt))
 		case 'f':
-			if msg.Envelope == nil {
+			if envelope == nil {
 				return "", nil,
 					errors.New("no envelope available for this message")
 			}
-			if len(msg.Envelope.From) == 0 {
+			if len(envelope.From) == 0 {
 				return "", nil,
 					errors.New("found no address for sender")
 			}
-			addr := msg.Envelope.From[0].Format()
+			addr := envelope.From[0].Format()
 			retval = append(retval, 's')
 			args = append(args, addr)
 		case 'F':
-			if msg.Envelope == nil {
+			if envelope == nil {
 				return "", nil,
 					errors.New("no envelope available for this message")
 			}
-			if len(msg.Envelope.From) == 0 {
+			if len(envelope.From) == 0 {
 				return "", nil,
 					errors.New("found no address for sender")
 			}
-			addr := msg.Envelope.From[0]
+			addr := envelope.From[0]
 			var val string
 
-			if addr.Name == accountFromAddress.Name && len(msg.Envelope.To) != 0 {
-				addr = msg.Envelope.To[0]
+			if addr.Name == accountFromAddress.Name && len(envelope.To) != 0 {
+				addr = envelope.To[0]
 			}
 
 			if addr.Name != "" {
@@ -194,25 +200,25 @@ func ParseMessageFormat(
 
 		case 'g':
 			retval = append(retval, 's')
-			args = append(args, strings.Join(msg.Labels, ", "))
+			args = append(args, strings.Join(ctx.MsgInfo.Labels, ", "))
 
 		case 'i':
-			if msg.Envelope == nil {
+			if envelope == nil {
 				return "", nil,
 					errors.New("no envelope available for this message")
 			}
 			retval = append(retval, 's')
-			args = append(args, msg.Envelope.MessageId)
+			args = append(args, envelope.MessageId)
 		case 'n':
-			if msg.Envelope == nil {
+			if envelope == nil {
 				return "", nil,
 					errors.New("no envelope available for this message")
 			}
-			if len(msg.Envelope.From) == 0 {
+			if len(envelope.From) == 0 {
 				return "", nil,
 					errors.New("found no address for sender")
 			}
-			addr := msg.Envelope.From[0]
+			addr := envelope.From[0]
 			var val string
 			if addr.Name != "" {
 				val = addr.Name
@@ -222,53 +228,53 @@ func ParseMessageFormat(
 			retval = append(retval, 's')
 			args = append(args, val)
 		case 'r':
-			if msg.Envelope == nil {
+			if envelope == nil {
 				return "", nil,
 					errors.New("no envelope available for this message")
 			}
-			addrs := FormatAddresses(msg.Envelope.To)
+			addrs := FormatAddresses(envelope.To)
 			retval = append(retval, 's')
 			args = append(args, addrs)
 		case 'R':
-			if msg.Envelope == nil {
+			if envelope == nil {
 				return "", nil,
 					errors.New("no envelope available for this message")
 			}
-			addrs := FormatAddresses(msg.Envelope.Cc)
+			addrs := FormatAddresses(envelope.Cc)
 			retval = append(retval, 's')
 			args = append(args, addrs)
 		case 's':
-			if msg.Envelope == nil {
+			if envelope == nil {
 				return "", nil,
 					errors.New("no envelope available for this message")
 			}
 			retval = append(retval, 's')
-			args = append(args, msg.Envelope.Subject)
+			args = append(args, envelope.Subject)
 		case 't':
-			if msg.Envelope == nil {
+			if envelope == nil {
 				return "", nil,
 					errors.New("no envelope available for this message")
 			}
-			if len(msg.Envelope.To) == 0 {
+			if len(envelope.To) == 0 {
 				return "", nil,
 					errors.New("found no address for recipient")
 			}
-			addr := msg.Envelope.To[0]
+			addr := envelope.To[0]
 			retval = append(retval, 's')
 			args = append(args, addr.Address)
 		case 'T':
 			retval = append(retval, 's')
-			args = append(args, accountName)
+			args = append(args, ctx.AccountName)
 		case 'u':
-			if msg.Envelope == nil {
+			if envelope == nil {
 				return "", nil,
 					errors.New("no envelope available for this message")
 			}
-			if len(msg.Envelope.From) == 0 {
+			if len(envelope.From) == 0 {
 				return "", nil,
 					errors.New("found no address for sender")
 			}
-			addr := msg.Envelope.From[0]
+			addr := envelope.From[0]
 			mailbox := addr.Address // fallback if there's no @ sign
 			if split := strings.SplitN(addr.Address, "@", 2); len(split) == 2 {
 				mailbox = split[1]
@@ -276,15 +282,15 @@ func ParseMessageFormat(
 			retval = append(retval, 's')
 			args = append(args, mailbox)
 		case 'v':
-			if msg.Envelope == nil {
+			if envelope == nil {
 				return "", nil,
 					errors.New("no envelope available for this message")
 			}
-			if len(msg.Envelope.From) == 0 {
+			if len(envelope.From) == 0 {
 				return "", nil,
 					errors.New("found no address for sender")
 			}
-			addr := msg.Envelope.From[0]
+			addr := envelope.From[0]
 			// check if message is from current user
 			if addr.Name != "" {
 				retval = append(retval, 's')
@@ -300,7 +306,7 @@ func ParseMessageFormat(
 			seen := false
 			recent := false
 			answered := false
-			for _, flag := range msg.Flags {
+			for _, flag := range ctx.MsgInfo.Flags {
 				if flag == models.SeenFlag {
 					seen = true
 				} else if flag == models.RecentFlag {
@@ -328,7 +334,7 @@ func ParseMessageFormat(
 					readReplyFlag = "O" // message is old
 				}
 			}
-			if marked {
+			if ctx.MsgIsMarked {
 				markedFlag = "*"
 			}
 			retval = append(retval, '4', 's')
@@ -339,7 +345,7 @@ func ParseMessageFormat(
 		case 'l':
 			// TODO: number of lines in the message
 			retval = append(retval, 'd')
-			args = append(args, msg.Size)
+			args = append(args, ctx.MsgInfo.Size)
 		case 'e':
 			// TODO: current message number in thread
 			fallthrough
diff --git a/widgets/msglist.go b/widgets/msglist.go
index 09b0868..b7c10d7 100644
--- a/widgets/msglist.go
+++ b/widgets/msglist.go
@@ -150,9 +150,14 @@ func (ml *MessageList) Draw(ctx *ui.Context) {
 
 		ctx.Fill(0, row, ctx.Width(), 1, ' ', style)
 		fmtStr, args, err := format.ParseMessageFormat(
-			ml.aerc.SelectedAccount().acct.From,
-			uiConfig.IndexFormat,
-			uiConfig.TimestampFormat, "", i, msg, store.IsMarked(uid))
+			uiConfig.IndexFormat, uiConfig.TimestampFormat,
+			format.Ctx{
+				FromAddress: ml.aerc.SelectedAccount().acct.From,
+				AccountName: ml.aerc.SelectedAccount().Name(),
+				MsgInfo:     msg,
+				MsgNum:      i,
+				MsgIsMarked: store.IsMarked(uid),
+			})
 		if err != nil {
 			ctx.Printf(0, row, style, "%v", err)
 		} else {
id=06152bdc5e20cbece35dd4709509b0bf024b428d'>^
9588a0fb ^

ee4687c3 ^





9588a0fb ^

ee4687c3 ^


9588a0fb ^
09d8404c ^
9588a0fb ^










09d8404c ^


9588a0fb ^
93ecd88d ^
9588a0fb ^




09752233 ^

d6429e27 ^







dfd2ef35 ^

09752233 ^
d6429e27 ^
dfd2ef35 ^











09752233 ^
933463fd ^
228f3f57 ^
















3f388ce5 ^
228f3f57 ^









09d8404c ^
228f3f57 ^




09d8404c ^
228f3f57 ^




09d8404c ^
933463fd ^
228f3f57 ^




3f388ce5 ^
228f3f57 ^

933463fd ^
228f3f57 ^
















933463fd ^
d8cd2b75 ^
cfe081ec ^
ee4687c3 ^
d6429e27 ^
ee4687c3 ^
d8cd2b75 ^

d6429e27 ^


d8cd2b75 ^

cfe081ec ^

d8cd2b75 ^
e5c44775 ^






ee4687c3 ^
95e3dfb1 ^
cfe081ec ^
95e3dfb1 ^

d8cd2b75 ^

cfe081ec ^

d8cd2b75 ^
ee4687c3 ^

e5c44775 ^




89466526 ^
ee4687c3 ^

d8cd2b75 ^
39db95d9 ^
d8cd2b75 ^
ca909b9a ^
95e3dfb1 ^
8d56a585 ^


95e3dfb1 ^

8d56a585 ^
95e3dfb1 ^
8d56a585 ^
d8cd2b75 ^
ee4687c3 ^

d8cd2b75 ^
8d56a585 ^

d8cd2b75 ^
8d56a585 ^
ca909b9a ^




d8cd2b75 ^
39db95d9 ^
cfe081ec ^

39db95d9 ^
ee4687c3 ^

39db95d9 ^
ee4687c3 ^
e5c44775 ^
39db95d9 ^

2feb26de ^
39db95d9 ^
ee4687c3 ^


39db95d9 ^







e5c44775 ^


ee4687c3 ^
39db95d9 ^



e5c44775 ^
39db95d9 ^


d8cd2b75 ^
4ba2112b ^
cfe081ec ^

4ba2112b ^
ee4687c3 ^

4ba2112b ^

89466526 ^
4ba2112b ^
ee4687c3 ^

4ba2112b ^








ca909b9a ^
cfe081ec ^

ca909b9a ^

ee4687c3 ^


ca909b9a ^



bfac461b ^
d6429e27 ^

ca909b9a ^




c6953a55 ^
2c9557d5 ^
ca909b9a ^

e5c44775 ^
cfe081ec ^

e5c44775 ^



89466526 ^
e5c44775 ^
ee4687c3 ^



e5c44775 ^

13ecffe7 ^










e5c44775 ^

ee4687c3 ^
e5c44775 ^



89466526 ^

e5c44775 ^
ee4687c3 ^
e5c44775 ^
89466526 ^

e5c44775 ^
c70c915b ^

e975b52f ^

c70c915b ^

e975b52f ^

c70c915b ^

4e9384fd ^
0b306138 ^






ef011c91 ^
0b306138 ^
ef011c91 ^
0b306138 ^

ef011c91 ^
0b306138 ^


feaf9e0a ^

0b306138 ^
ef011c91 ^
0b306138 ^



ef011c91 ^
0b306138 ^



ef011c91 ^






0b306138 ^
e5c44775 ^
ef011c91 ^



feaf9e0a ^

ef011c91 ^


93ecd88d ^
feaf9e0a ^

ef011c91 ^

feaf9e0a ^

ef011c91 ^
feaf9e0a ^

93ecd88d ^
feaf9e0a ^

ef011c91 ^
























4e9384fd ^
93ecd88d ^





















95947388 ^











93ecd88d ^
feaf9e0a ^











93ecd88d ^
d8cd2b75 ^
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620