utils

package
v0.35.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 12 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	AttachmentMarkerStart = "\x00<ATTM>\x00"
	AttachmentMarkerEnd   = "\x00</ATTM>\x00"
)
View Source
const BlockQuoteCharDefault = ">"

Variables

View Source
var GetEmojiMap = sync.OnceValue(func() map[string]string {
	data := emoji.Gemoji()
	aliasMap := make(map[string]string, (len(data)*6)+len(defaultAliases))

	register := func(name string, unicodeVal string, supportsSkinTone bool) {
		if name == "" {
			return
		}

		if _, exists := aliasMap[name]; !exists {
			aliasMap[name] = unicodeVal
		}

		if !supportsSkinTone {
			return
		}

		for _, st := range skinToneSuffixes {
			variantKey := name + st.suffix
			if _, exists := aliasMap[variantKey]; !exists {
				aliasMap[variantKey] = applySkinTone(unicodeVal, st.tone)
			}
		}
	}

	registerWithVariants := func(name string, unicodeVal string, supportsSkinTone bool) {
		register(name, unicodeVal, supportsSkinTone)

		if strings.ContainsRune(name, '-') {
			register(strings.ReplaceAll(name, "-", "_"), unicodeVal, supportsSkinTone)
		}

		if len(name) == 2 && len(unicodeVal) == 8 &&
			unicodeVal[:3] == "\xf0\x9f\x87" && unicodeVal[4:7] == "\xf0\x9f\x87" &&
			unicodeVal[3] >= 0xA6 && unicodeVal[3] <= 0xBF &&
			unicodeVal[7] >= 0xA6 && unicodeVal[7] <= 0xBF {
			register("flag_"+name, unicodeVal, supportsSkinTone)
			register("flag-"+name, unicodeVal, supportsSkinTone)
		} else if rest, ok := strings.CutPrefix(name, "flag_"); ok {
			register("flag-"+rest, unicodeVal, supportsSkinTone)
		}

		if rest, ok := strings.CutPrefix(name, "flight_"); ok {
			register("airplane_"+rest, unicodeVal, supportsSkinTone)
			register("airplane-"+strings.ReplaceAll(rest, "_", "-"), unicodeVal, supportsSkinTone)

			if rest == "arrival" {
				register("airplane_arriving", unicodeVal, supportsSkinTone)
				register("airplane-arriving", unicodeVal, supportsSkinTone)
			}
		} else if rest, ok := strings.CutPrefix(name, "airplane_"); ok {
			register("flight_"+rest, unicodeVal, supportsSkinTone)
			register("flight-"+strings.ReplaceAll(rest, "_", "-"), unicodeVal, supportsSkinTone)

			if rest == "arriving" {
				register("flight_arrival", unicodeVal, supportsSkinTone)
				register("flight-arrival", unicodeVal, supportsSkinTone)
			}
		}

		if rest, ok := strings.CutSuffix(name, "_man"); ok {
			register("man_"+rest, unicodeVal, supportsSkinTone)
			register("man-"+strings.ReplaceAll(rest, "_", "-"), unicodeVal, supportsSkinTone)
		} else if rest, ok := strings.CutSuffix(name, "_woman"); ok {
			register("woman_"+rest, unicodeVal, supportsSkinTone)
			register("woman-"+strings.ReplaceAll(rest, "_", "-"), unicodeVal, supportsSkinTone)
		} else if rest, ok := strings.CutPrefix(name, "male_"); ok {
			register("man_"+rest, unicodeVal, supportsSkinTone)
			register("man-"+strings.ReplaceAll(rest, "_", "-"), unicodeVal, supportsSkinTone)
		} else if rest, ok := strings.CutPrefix(name, "female_"); ok {
			register("woman_"+rest, unicodeVal, supportsSkinTone)
			register("woman-"+strings.ReplaceAll(rest, "_", "-"), unicodeVal, supportsSkinTone)
		}
	}

	for _, e := range data {
		if e.Emoji == "" {
			continue
		}

		if len(e.Emoji) == 8 &&
			e.Emoji[0] == 0xF0 && e.Emoji[1] == 0x9F && e.Emoji[2] == 0x87 &&
			e.Emoji[4] == 0xF0 && e.Emoji[5] == 0x9F && e.Emoji[6] == 0x87 {
			c1 := 'a' + (e.Emoji[3] - 0xA6)
			c2 := 'a' + (e.Emoji[7] - 0xA6)

			if c1 >= 'a' && c1 <= 'z' && c2 >= 'a' && c2 <= 'z' {
				code := string([]byte{c1, c2})

				registerWithVariants(code, e.Emoji, false)
			}
		}

		for _, alias := range e.Aliases {
			registerWithVariants(alias, e.Emoji, e.SkinTones)

			if e.Category == "Flags" && !strings.HasPrefix(alias, "flag") {
				registerWithVariants("flag_"+alias, e.Emoji, false)
				registerWithVariants("flag-"+alias, e.Emoji, false)
			}
		}
	}

	for _, e := range data {
		if e.Emoji == "" {
			continue
		}

		for _, tag := range e.Tags {
			registerWithVariants(tag, e.Emoji, e.SkinTones)
		}
	}

	for alias, target := range defaultAliases {
		targetClean := trimColons(target)

		if unicodeVal, exists := aliasMap[targetClean]; exists {
			registerWithVariants(alias, unicodeVal, true)
		}
	}

	return aliasMap
})

GetEmojiMap initializes and returns the immutable alias-to-Unicode map once using Go 1.21+ sync.OnceValue.

Functions

func AppendMessageSuffix added in v0.35.0

func AppendMessageSuffix(msg, suffix string) string

AppendMessageSuffix appends a formatted suffix (such as a reply context or action suffix) to msg, stripping trailing newlines and placing single-line CTCP ACTION suffixes inside \x01.

func EmojiFromAlias added in v0.31.0

func EmojiFromAlias(alias string, customAliases map[string]string) (string, bool)

EmojiFromAlias looks up an emoji by shortcode, supporting with or without surrounding colons.

func EmojiReplaceAliases added in v0.31.0

func EmojiReplaceAliases(s string, customAliases map[string]string) string

EmojiReplaceAliases scans the input and replaces :alias: tokens with Unicode emojis.

func ExpandDirectoryURL added in v0.35.0

func ExpandDirectoryURL(tmpl, nick, username string, extra ...string) string

ExpandDirectoryURL expands {nick}, {username}, {email}, {firstname}, and {lastname} placeholders in a DirectoryURL template, escaping them appropriately for the path and query components.

func FindClosestIRCColor added in v0.32.0

func FindClosestIRCColor(hexColor string) string

FindClosestIRCColor uses the precalculated palette to quickly find the nearest match.

func FormatActionSuffix added in v0.35.0

func FormatActionSuffix(kind ActionSuffixKind, useUnicode bool) string

FormatActionSuffix returns the italicized IRC suffix for a message state change.

func FormatAndShortenSummary added in v0.32.0

func FormatAndShortenSummary(text string, opts SummaryOpts) string

FormatAndShortenSummary formats markdown/emoji, collapses newlines, and shortens text.

func FormatFullCodeBlock added in v0.32.0

func FormatFullCodeBlock(text, lexer, indent string, opts ProcessMessageOpts, yield func(string))

FormatFullCodeBlock handles syntax highlighting entirely in-memory and yields lines

func FormatLastOnline added in v0.35.0

func FormatLastOnline(lastActivity int64, isOnline ...bool) string

FormatLastOnline formats a Unix timestamp (seconds) as a human-readable "Last online ..." or "Last activity ..." string for WHOIS output.

func FormatMarkdownAndEmoji added in v0.32.0

func FormatMarkdownAndEmoji(msg string, disableMarkdown bool, disableEmoji bool, blockQuoteChar string, inlineCode string, customEmoji map[string]string) string

FormatMarkdownAndEmoji applies Markdown formatting and Emoji alias replacement. It utilizes a fast-path single-pass check to bypass processing entirely if no trigger characters are present, drastically reducing allocations and CPU usage.

func Irc2Markdown added in v0.33.0

func Irc2Markdown(msg string) string

Irc2Markdown converts IRC formatting back to Markdown, handling links and text styles.

func Markdown2irc

func Markdown2irc(msg string, blockQuoteChar string, inlineCode string) string

func ProcessMessageText added in v0.32.0

func ProcessMessageText(text string, opts ProcessMessageOpts, yield func(line string))

ProcessMessageText abstracts the parsing loop, multi-line code handling, and formatting. It uses a zero-allocation callback (yield) to return lines.

func SanitizeDefaultMentions added in v0.34.0

func SanitizeDefaultMentions(text string, useUnicode bool) string

SanitizeDefaultMentions defangs @channel, @all, @here, @everyone, and Matrix @room mentions to prevent unwanted highlights in IRC clients. When useUnicode is enabled, it inserts a Zero-Width Non-Joiner (ZWNJ) after the '@'. When disabled, it inserts a hyphen.

func StringWidth added in v0.34.0

func StringWidth(s string) int

StringWidth returns the visual cell width of a string in a monospace terminal, accounting for East Asian wide characters, combining marks, format characters, regional indicator pairs (flags), and emoji sequences.

func WrapMessage added in v0.31.0

func WrapMessage(msg string, maxLen int, hardLineSplit ...bool) string

WrapMessage soft-wraps msg into lines of at most maxLen bytes, breaking only at spaces/newlines. Words longer than maxLen are left unbroken and overflow their line rather than being split unless hardLineSplit is enabled.

Types

type ActionSuffixKind added in v0.35.0

type ActionSuffixKind string

ActionSuffixKind identifies a message state suffix appended to IRC messages.

const (
	SuffixEdited    ActionSuffixKind = "edited"
	SuffixPinned    ActionSuffixKind = "pinned"
	SuffixUnpinned  ActionSuffixKind = "unpinned"
	SuffixUpdated   ActionSuffixKind = "updated"
	SuffixDeleted   ActionSuffixKind = "deleted"
	SuffixEncrypted ActionSuffixKind = "encrypted"
)

type IRCColor added in v0.32.0

type IRCColor struct {
	Code    string
	R, G, B int
}

IRCColor represents a pre-parsed IRC color for fast distance calculations.

type ProcessMessageOpts added in v0.32.0

type ProcessMessageOpts struct {
	DisableEmoji bool
	CustomEmoji  map[string]string

	DisableMarkdown    bool
	SyntaxHighlighting string
	CodeBlockPrefix    string
	CodeBlockSeparator string
	BlockquoteChar     string
	InlineCodeChar     string

	PreserveNewLines string // "all" (default), "max-one", or "none" / "zero"
}

ProcessMessageOpts holds the configuration for text processing

type SkinTone added in v0.32.0

type SkinTone rune
const (
	SkinToneNeutral     SkinTone = 0
	SkinToneLight       SkinTone = 0x1f3fb
	SkinToneMediumLight SkinTone = 0x1f3fc
	SkinToneMedium      SkinTone = 0x1f3fd
	SkinToneMediumDark  SkinTone = 0x1f3fe
	SkinToneDark        SkinTone = 0x1f3ff
)

type SummaryOpts added in v0.32.0

type SummaryOpts struct {
	DisableEmoji bool
	CustomEmoji  map[string]string

	DisableMarkdown bool
	BlockquoteChar  string
	InlineCodeChar  string
	MaxLength       int
	UncountedPrefix string
	Unicode         bool
}

SummaryOpts holds configuration for shortening and summarizing text.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL