package modules import ( "maps" "strings" "uberbringer/internal/pkg/receipt" ) // maxTitleMarkers is how many tag markers go in front of a title. const maxTitleMarkers = 2 // tagMarkers are the markers printed for ntfy's emoji tags, by tag. Every // glyph prints on CP437. var tagMarkers = map[string]string{ "warning": "[!]", "rotating_light": "(!)", "fire": "(!)", "boom": "(!)", "white_check_mark": "[√]", "heavy_check_mark": "[√]", "ballot_box_with_check": "[√]", "x": "[x]", "negative_squared_cross_mark": "[x]", "no_entry": "[x]", "no_entry_sign": "[x]", "information_source": "(i)", "question": "(?)", "+1": "(+1)", "thumbsup": "(+1)", "-1": "(-1)", "thumbsdown": "(-1)", "loudspeaker": "", "mega": "", "tada": `\o/`, "skull": "x_x", "hourglass": "(...)", } // typedEmoji are emoji typed into a text that print as the marker of a // tag. A variation selector after one is removed with the other emoji. var typedEmoji = []struct{ emoji, tag string }{ {"⚠", "warning"}, {"✅", "white_check_mark"}, {"❌", "x"}, {"🚨", "rotating_light"}, {"👍", "+1"}, } // Markers maps tags to the markers printed for them. A tag with an empty // marker is hidden. type Markers map[string]string // NewMarkers returns the built-in markers with overrides applied. A key of // overrides is a tag; an empty value hides it. func NewMarkers(overrides map[string]string) Markers { m := maps.Clone(tagMarkers) for tag, marker := range overrides { m[strings.ToLower(tag)] = marker } return m } // lookup returns the marker for tag, matched in lower case, and whether // the tag has one: an empty marker hides the tag. func (m Markers) lookup(tag string) (string, bool) { marker, ok := m[strings.ToLower(tag)] return marker, ok } // clean prepares text from a notification for the card: the typed emoji // that have markers become them, and the other emoji are removed, those // whose tag is hidden too. func (m Markers) clean(s string) string { for _, e := range typedEmoji { if !strings.Contains(s, e.emoji) { continue } if marker, _ := m.lookup(e.tag); marker != "" { s = strings.ReplaceAll(s, e.emoji, marker) } } return receipt.StripEmoji(s) } // sortTags splits the first MaxTags tags into the markers for the title, // at most maxTitleMarkers of them and each once, and the tags printed as // text: those without a marker and those with one when the title has no // room for it, cleaned and cut to MaxTag. Hidden tags, and tags that // cleaning leaves empty, appear in neither. func (m Markers) sortTags(tags []string) (markers, text []string) { seen := make(map[string]bool) for _, tag := range tags[:min(len(tags), MaxTags)] { tag = strings.TrimSpace(tag) if tag == "" { continue } marker, ok := m.lookup(tag) switch { case ok && marker == "": case ok && seen[marker]: case ok && len(markers) < maxTitleMarkers: seen[marker] = true markers = append(markers, marker) default: if tag = cut(strings.TrimSpace(m.clean(tag)), MaxTag); tag != "" { text = append(text, tag) } } } return markers, text }