~chrisppy/titan

ref: 723d8c78cdfebe669e51d6e5396c78b05941aabc titan/config.go -rw-r--r-- 19.3 KiB
723d8c78Chris Palmer set tab colors based on content and title colors 3 months ago
                                                                                
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
// This file is part of titan.
//
// Copyright © 2020-2021 Chris Palmer <chris@red-oxide.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

package main

import (
	"fmt"
	"io"
	"os"
	"path/filepath"
	"reflect"
	"strings"

	"git.sr.ht/~emersion/go-scfg"
	"github.com/gdamore/tcell/v2"
)

type params int

const (
	none params = iota
	one
	multiple
)

// Config contains all relevant configuration data for the application
type config struct {
	HomePage           string
	DisplayHiddenLinks bool
	Theme              theme
	CMDMode            keyCMD
	Enter              keyCMD
	Escape             keyCMD
	NavLeft            keyCMD
	NavUp              keyCMD
	NavDown            keyCMD
	NavRight           keyCMD
	NavPageUp          keyCMD
	NavPageDown        keyCMD
	Open               keyCMD
	OpenTab            keyCMD
	CloseTab           keyCMD
	Edit               keyCMD
	BrowserPage        keyCMD
	HelpPage           keyCMD
	Back               keyCMD
	Forward            keyCMD
	NavZero            keyCMD
	NavOne             keyCMD
	NavTwo             keyCMD
	NavThree           keyCMD
	NavFour            keyCMD
	NavFive            keyCMD
	NavSix             keyCMD
	NavSeven           keyCMD
	NavEight           keyCMD
	NavNine            keyCMD
	HTTPCommand        *command
	GopherCommand      *command
}

type command struct {
	Bin  string
	Args []string
}

type theme struct {
	ErrColor            tcell.Color
	CMDBGColor          tcell.Color
	CMDFGColor          tcell.Color
	StatusBGColor       tcell.Color
	StatusFGColor       tcell.Color
	TitleBGColor        tcell.Color
	TitleFGColor        tcell.Color
	ContentBGColor      tcell.Color
	ContentTextColor    tcell.Color
	ContentH1Color      string
	ContentH2Color      string
	ContentH3Color      string
	ContentPreColor     string
	ContentLinkColor    string
	ContentExtLinkColor string
}

type keyCMD struct {
	Text string
	Rune rune
	Key  tcell.Key
	Mod  tcell.ModMask
}

type contentTheme struct {
	Background tcell.Color
	Text       tcell.Color
	H1         string
	H2         string
	H3         string
	Pre        string
	Link       string
	ExtLink    string
}

func (a *app) loadConfig() {
	if _, err := os.Stat(filepath.Clean(a.Dirs.Config)); os.IsNotExist(err) {
		if errDir := os.MkdirAll(a.Dirs.Config, 0750); errDir != nil {
			a.Logger.Fatalf("unable to create config dir due to %s", errDir.Error())
			return
		}
	}
	a.Dirs.Config = filepath.Join(a.Dirs.Config, "config")

	if _, err := os.Stat(a.Dirs.Config); os.IsNotExist(err) {
		c := defaultConfig()
		if err := os.WriteFile(filepath.Clean(a.Dirs.Config), c, 0600); err != nil {
			a.Logger.Fatalf(`ERROR: unable to write default config to %s due to %s\n`, a.Dirs.Config, err.Error())
			return
		}
	}

	f, err := os.Open(a.Dirs.Config)
	if err != nil {
		a.Logger.Fatalf(`ERROR: unable to read config at %s due to %s\n`, a.Dirs.Config, err.Error())
		return
	}
	defer func() {
		if err := f.Close(); err != nil {
			a.Logger.Fatalf(`ERROR: unable to close config at %s due to %s\n`, a.Dirs.Config, err.Error())
		}
	}()

	if err := a.parseConfig(f); err != nil {
		a.Logger.Fatalf(`ERROR: unable to parse config at %s due to %s\n`, a.Dirs.Config, err.Error())
		return
	}
}

func (a *app) parseConfig(r io.Reader) error {
	cfg, err := scfg.Read(r)
	if err != nil {
		return err
	}

	a.Config = &config{}

	errors := make(map[error]bool)

	var fhome, fkeys, fTheme bool
	for _, dir := range cfg {
		switch n := dir.Name; n {
		case "home-page":
			fhome = true
			if err := validateDirective(n, dir, one); err != nil {
				errors[err] = false
			} else {
				a.Config.HomePage = dir.Params[0]
			}
		case "display-hidden-links":
			a.Config.DisplayHiddenLinks = true
		case "key-commands":
			fkeys = true
			if err := a.parseKeyCommands(dir); len(err) > 0 {
				for k, v := range err {
					errors[k] = v
				}
			}
		case "theme":
			fTheme = true
			if err := a.parseTheme(dir); len(err) > 0 {
				for k, v := range err {
					errors[k] = v
				}
			}
		case "external":
			if err := a.parseExternal(dir); len(err) > 0 {
				for k, v := range err {
					errors[k] = v
				}
			}
		default:
			errors[fmt.Errorf("unknown directive %q", n)] = false
		}
	}

	if !fhome {
		errors[fmt.Errorf("home-page is a required directive")] = false
	}
	if !fkeys {
		errors[fmt.Errorf("key-commands is a required directive")] = false
	}
	if !fTheme {
		errors[fmt.Errorf("theme is a required directive")] = false
	}

	var eb strings.Builder
	for err := range errors {
		eb.WriteString(fmt.Sprintf("%s\n", err.Error()))
	}

	if eb.String() != "" {
		return fmt.Errorf("errors found in the config:\n%s", eb.String())
	}
	return nil
}

func (a *app) parseKeyCommands(dir *scfg.Directive) map[error]bool {
	errors := make(map[error]bool)

	if k, err := createKeyCMD(":"); err != nil {
		errors[err] = false
	} else {
		a.Config.CMDMode = k
	}

	if k, err := createKeyCMD("Enter"); err != nil {
		errors[err] = false
	} else {
		a.Config.Enter = k
	}

	if k, err := createKeyCMD("Esc"); err != nil {
		errors[err] = false
	} else {
		a.Config.Escape = k
	}

	if k, err := createKeyCMD("0"); err != nil {
		errors[err] = false
	} else {
		a.Config.NavZero = k
	}
	if k, err := createKeyCMD("1"); err != nil {
		errors[err] = false
	} else {
		a.Config.NavOne = k
	}
	if k, err := createKeyCMD("2"); err != nil {
		errors[err] = false
	} else {
		a.Config.NavTwo = k
	}
	if k, err := createKeyCMD("3"); err != nil {
		errors[err] = false
	} else {
		a.Config.NavThree = k
	}
	if k, err := createKeyCMD("4"); err != nil {
		errors[err] = false
	} else {
		a.Config.NavFour = k
	}
	if k, err := createKeyCMD("5"); err != nil {
		errors[err] = false
	} else {
		a.Config.NavFive = k
	}
	if k, err := createKeyCMD("6"); err != nil {
		errors[err] = false
	} else {
		a.Config.NavSix = k
	}
	if k, err := createKeyCMD("7"); err != nil {
		errors[err] = false
	} else {
		a.Config.NavSeven = k
	}
	if k, err := createKeyCMD("8"); err != nil {
		errors[err] = false
	} else {
		a.Config.NavEight = k
	}
	if k, err := createKeyCMD("9"); err != nil {
		errors[err] = false
	} else {
		a.Config.NavNine = k
	}

	for _, d := range dir.Children {
		switch n := d.Name; n {
		case "back":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else if k, err := createKeyCMD(d.Params[0]); err != nil {
				errors[err] = false
			} else {
				a.Config.Back = k
			}
		case "browser-page":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else if k, err := createKeyCMD(d.Params[0]); err != nil {
				errors[err] = false
			} else {
				a.Config.BrowserPage = k
			}
		case "close-tab":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else if k, err := createKeyCMD(d.Params[0]); err != nil {
				errors[err] = false
			} else {
				a.Config.CloseTab = k
			}
		case "down":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else if k, err := createKeyCMD(d.Params[0]); err != nil {
				errors[err] = false
			} else {
				a.Config.NavDown = k
			}
		case "edit":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else if k, err := createKeyCMD(d.Params[0]); err != nil {
				errors[err] = false
			} else {
				a.Config.Edit = k
			}
		case "forward":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else if k, err := createKeyCMD(d.Params[0]); err != nil {
				errors[err] = false
			} else {
				a.Config.Forward = k
			}
		case "help-page":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else if k, err := createKeyCMD(d.Params[0]); err != nil {
				errors[err] = false
			} else {
				a.Config.HelpPage = k
			}
		case "left":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else if k, err := createKeyCMD(d.Params[0]); err != nil {
				errors[err] = false
			} else {
				a.Config.NavLeft = k
			}
		case "open":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else if k, err := createKeyCMD(d.Params[0]); err != nil {
				errors[err] = false
			} else {
				a.Config.Open = k
			}
		case "open-tab":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else if k, err := createKeyCMD(d.Params[0]); err != nil {
				errors[err] = false
			} else {
				a.Config.OpenTab = k
			}
		case "page-down":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else if k, err := createKeyCMD(d.Params[0]); err != nil {
				errors[err] = false
			} else {
				a.Config.NavPageDown = k
			}
		case "page-up":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else if k, err := createKeyCMD(d.Params[0]); err != nil {
				errors[err] = false
			} else {
				a.Config.NavPageUp = k
			}
		case "right":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else if k, err := createKeyCMD(d.Params[0]); err != nil {
				errors[err] = false
			} else {
				a.Config.NavRight = k
			}
		case "up":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else if k, err := createKeyCMD(d.Params[0]); err != nil {
				errors[err] = false
			} else {
				a.Config.NavUp = k
			}
		default:
			errors[fmt.Errorf("unknown directive within keycommands %q", n)] = false
		}
	}

	if reflect.DeepEqual(a.Config.NavDown, keyCMD{}) {
		errors[fmt.Errorf("down is a required directive for key-commands")] = false
	}
	if reflect.DeepEqual(a.Config.HelpPage, keyCMD{}) {
		errors[fmt.Errorf("help-page is a required directive for key-commands")] = false
	}
	if reflect.DeepEqual(a.Config.BrowserPage, keyCMD{}) {
		errors[fmt.Errorf("browser-page is a required directive for key-commands")] = false
	}
	if reflect.DeepEqual(a.Config.NavLeft, keyCMD{}) {
		errors[fmt.Errorf("left is a required directive for key-commands")] = false
	}
	if reflect.DeepEqual(a.Config.NavPageDown, keyCMD{}) {
		errors[fmt.Errorf("page-down is a required directive for key-commands")] = false
	}
	if reflect.DeepEqual(a.Config.NavPageUp, keyCMD{}) {
		errors[fmt.Errorf("page-up is a required directive for key-commands")] = false
	}
	if reflect.DeepEqual(a.Config.NavRight, keyCMD{}) {
		errors[fmt.Errorf("right is a required directive for key-commands")] = false
	}
	if reflect.DeepEqual(a.Config.NavUp, keyCMD{}) {
		errors[fmt.Errorf("up is a required directive for key-commands")] = false
	}
	if reflect.DeepEqual(a.Config.Open, keyCMD{}) {
		errors[fmt.Errorf("open is a required directive for key-commands")] = false
	}
	if reflect.DeepEqual(a.Config.OpenTab, keyCMD{}) {
		errors[fmt.Errorf("open-tab is a required directive for key-commands")] = false
	}
	if reflect.DeepEqual(a.Config.CloseTab, keyCMD{}) {
		errors[fmt.Errorf("close-tab is a required directive for key-commands")] = false
	}
	if reflect.DeepEqual(a.Config.Edit, keyCMD{}) {
		errors[fmt.Errorf("edit is a required directive for key-commands")] = false
	}
	if reflect.DeepEqual(a.Config.Back, keyCMD{}) {
		errors[fmt.Errorf("back is a required directive for key-commands")] = false
	}
	if reflect.DeepEqual(a.Config.Forward, keyCMD{}) {
		errors[fmt.Errorf("forward is a required directive for key-commands")] = false
	}

	return errors
}

func (a *app) parseTheme(dir *scfg.Directive) map[error]bool {
	errors := make(map[error]bool)

	var bg, fg string
	var ferr, ftitle, fstat, fcmd, fcnt bool
	for _, d := range dir.Children {
		switch n := d.Name; n {
		case "background-color":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				bg = d.Params[0]
			}
		case "command-line":
			fcmd = true
			if bga, fga, err := parseChildTheme(n, d); len(err) > 0 {
				for k, v := range err {
					errors[k] = v
				}
			} else {
				a.Config.Theme.CMDBGColor = tcell.GetColor(bga)
				a.Config.Theme.CMDFGColor = tcell.GetColor(fga)
			}
		case "content":
			fcnt = true
			if cnt, err := parseContentTheme(n, d); len(err) > 0 {
				for k, v := range err {
					errors[k] = v
				}
			} else {
				a.Config.Theme.ContentBGColor = cnt.Background
				a.Config.Theme.ContentTextColor = cnt.Text
				a.Config.Theme.ContentH1Color = cnt.H1
				a.Config.Theme.ContentH2Color = cnt.H2
				a.Config.Theme.ContentH3Color = cnt.H3
				a.Config.Theme.ContentPreColor = cnt.Pre
				a.Config.Theme.ContentLinkColor = cnt.Link
				a.Config.Theme.ContentExtLinkColor = cnt.ExtLink
			}
		case "error-color":
			ferr = true
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				a.Config.Theme.ErrColor = tcell.GetColor(d.Params[0])
			}
		case "foreground-color":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				fg = d.Params[0]
			}
		case "status-bar":
			fstat = true
			if bga, fga, err := parseChildTheme(n, d); len(err) > 0 {
				for k, v := range err {
					errors[k] = v
				}
			} else {
				a.Config.Theme.StatusBGColor = tcell.GetColor(bga)
				a.Config.Theme.StatusFGColor = tcell.GetColor(fga)
			}
		case "title-bar":
			ftitle = true
			if bga, fga, err := parseChildTheme(n, d); len(err) > 0 {
				for k, v := range err {
					errors[k] = v
				}
			} else {
				a.Config.Theme.TitleBGColor = tcell.GetColor(bga)
				a.Config.Theme.TitleFGColor = tcell.GetColor(fga)
			}
		default:
			errors[fmt.Errorf("unknown directive within theme %q", n)] = false
		}
	}

	if bg == "" {
		errors[fmt.Errorf("background-color is a required directive for theme")] = false
	}
	if fg == "" {
		errors[fmt.Errorf("foreground-color is a required directive for theme")] = false
	}
	if !ferr {
		errors[fmt.Errorf("error-color is a required directive for theme")] = false
	}

	if !fcmd && bg != "" && fg != "" {
		a.Config.Theme.CMDBGColor = tcell.GetColor(bg)
		a.Config.Theme.CMDFGColor = tcell.GetColor(fg)
	}
	if !fcnt && bg != "" && fg != "" {
		a.Config.Theme.ContentBGColor = tcell.GetColor(bg)
		a.Config.Theme.ContentTextColor = tcell.GetColor(fg)
		a.Config.Theme.ContentH1Color = fg
		a.Config.Theme.ContentH2Color = fg
		a.Config.Theme.ContentH3Color = fg
		a.Config.Theme.ContentPreColor = fg
		a.Config.Theme.ContentLinkColor = fg
		a.Config.Theme.ContentExtLinkColor = fg
	}
	if !fstat && bg != "" && fg != "" {
		a.Config.Theme.StatusBGColor = tcell.GetColor(bg)
		a.Config.Theme.StatusFGColor = tcell.GetColor(fg)
	}
	if !ftitle && bg != "" && fg != "" {
		a.Config.Theme.TitleBGColor = tcell.GetColor(bg)
		a.Config.Theme.TitleFGColor = tcell.GetColor(fg)
	}

	return errors
}

func parseChildTheme(name string, dir *scfg.Directive) (string, string, map[error]bool) {
	errors := make(map[error]bool)

	var bg, fg string
	for _, d := range dir.Children {
		switch n := d.Name; n {
		case "background-color":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				bg = d.Params[0]
			}
		case "foreground-color":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				fg = d.Params[0]
			}
		default:
			errors[fmt.Errorf("unknown directive within %s %q", name, n)] = false
		}
	}

	if bg == "" {
		errors[fmt.Errorf("background-color is a required directive for %s", name)] = false
	}

	if fg == "" {
		errors[fmt.Errorf("foreground-color is a required directive for %s", name)] = false
	}

	return bg, fg, errors
}

func parseContentTheme(name string, dir *scfg.Directive) (*contentTheme, map[error]bool) {
	errors := make(map[error]bool)

	cnt := contentTheme{}
	var bg, fg string
	for _, d := range dir.Children {
		switch n := d.Name; n {
		case "background-color":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				bg = d.Params[0]
			}
		case "text-color":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				fg = d.Params[0]
			}
		case "header1-color":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				cnt.H1 = d.Params[0]
			}
		case "header2-color":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				cnt.H2 = d.Params[0]
			}
		case "header3-color":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				cnt.H3 = d.Params[0]
			}
		case "preformatted-text-color":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				cnt.Pre = d.Params[0]
			}
		case "link-color":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				cnt.Link = d.Params[0]
			}
		case "ext-link-color":
			if err := validateDirective(n, d, one); err != nil {
				errors[err] = false
			} else {
				cnt.ExtLink = d.Params[0]
			}
		default:
			errors[fmt.Errorf("unknown directive within %s %q", name, n)] = false
		}
	}

	if bg == "" {
		errors[fmt.Errorf("background-color is a required directive for %s", name)] = false
	} else {
		cnt.Background = tcell.GetColor(bg)
	}

	if fg == "" {
		errors[fmt.Errorf("text-color is a required directive for %s", name)] = false
	} else {
		cnt.Text = tcell.GetColor(fg)
	}

	if cnt.H1 == "" {
		cnt.H1 = fg
	}

	if cnt.H2 == "" {
		cnt.H2 = fg
	}

	if cnt.H3 == "" {
		cnt.H3 = fg
	}

	if cnt.Pre == "" {
		cnt.Pre = fg
	}

	if cnt.Link == "" {
		cnt.Link = fg
	}

	if cnt.ExtLink == "" {
		cnt.ExtLink = fg
	}

	return &cnt, errors
}

func (a *app) parseExternal(dir *scfg.Directive) map[error]bool {
	errors := make(map[error]bool)
	if len(dir.Params) < 1 {
		errors[fmt.Errorf("protocol http or gopher is required for external")] = false
		return errors
	}

	var isGopher, isHTTP bool
	switch dir.Params[0] {
	case "gopher":
		isGopher = true
	case "http":
		isHTTP = true
	default:
		errors[fmt.Errorf("unknown protocol provided for external")] = false
		return errors
	}

	var isFound bool
	for _, d := range dir.Children {
		switch n := d.Name; n {
		case "command":
			isFound = true
			if err := validateDirective(n, d, multiple); err != nil {
				errors[err] = false
				continue
			}

			if len(d.Params) < 2 {
				errors[fmt.Errorf("at least 2 elements are required for external command")] = false
				continue
			}

			var found bool
			var bin string
			args := make([]string, len(d.Params)-1)
			for i, a := range d.Params {
				if i == 0 {
					bin = a
					continue
				}

				args[i-1] = a
				if a == "[URL]" {
					found = true
				}
			}

			cmd := command{
				Bin:  bin,
				Args: args,
			}

			if isHTTP {
				if !found {
					errors[fmt.Errorf(`[URL] must be one of the args provided for external "http" command`)] = false
				} else {
					a.Config.HTTPCommand = &cmd
				}
			} else if isGopher {
				if !found {
					errors[fmt.Errorf(`[URL] must be one of the args provided for external "gopher" command`)] = false
				} else {
					a.Config.GopherCommand = &cmd
				}
			}
		default:
			errors[fmt.Errorf("unknown directive within external %q", n)] = false
		}
	}

	if !isFound {
		errors[fmt.Errorf("command is a required directive for external")] = false
	}

	return errors
}