summaryrefslogtreecommitdiff
path: root/indexers/pikdex/meta.go
blob: ef2c029336ee541be92824695146e49b81588a7a (plain)
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
package pikdex

import (
	"github.com/charmbracelet/lipgloss"
	"github.com/ewy1/pik/describe"
	"strings"
)

type MetaSetter func(s *SourceData, content string)

var MetaFiles = map[string]MetaSetter{
	".wants": func(s *SourceData, content string) {
		s.Wants = contentLines(content)
	},
	".includes": func(s *SourceData, content string) {
		s.Includes = contentLines(content)
	},
	".alias": func(s *SourceData, content string) {
		s.Aliases = contentLines(content)
	},
	".icon": func(s *SourceData, content string) {
		lines := contentLines(content)
		if len(lines) == 0 {
			return
		}
		icon := lines[0]
		desiredWidth := lipgloss.Width(icon)
		diff := desiredWidth - len([]rune(icon))
		icon += strings.Repeat(" ", diff)
		s.Icon = icon
	},
}

func contentLines(input string) []string {
	result := make([]string, 0, len(input))
nextLine:
	for _, l := range strings.Split(input, "\n") {
		l = strings.TrimSpace(l)

		if l == "" {
			continue nextLine
		}

		for _, c := range describe.CommentPrefixes {
			if strings.HasPrefix(l, c) {
				continue nextLine
			}
		}

		result = append(result, l)
	}
	return result
}