Debian Codegraph @

pkg/util/message.go

raw
1 package util
2
3 import (
4 "strings"
5
6 "github.com/nicholas-fedor/shoutrrr/pkg/types"
7 )
8
9 // ellipsis is the suffix appended to truncated strings.
10 const ellipsis = " [...]"
11
12 // PartitionMessage splits input into chunks that fit within the specified limits.
13 //
14 // The function attempts to split at word boundaries (spaces or newlines) to improve
15 // readability. It searches backwards up to distance runes from the chunk boundary
16 // to find a suitable split point.
17 //
18 // Parameters:
19 // - input: The string to partition into chunks.
20 // - limits: The message limits containing ChunkSize, TotalChunkSize, and ChunkCount.
21 // - distance: The maximum number of runes to search backwards for a whitespace character.
22 //
23 // Returns:
24 // - A slice of MessageItem containing the partitioned chunks.
25 // - The number of runes that were omitted (exceeded limits).
26 func PartitionMessage(
27 input string,
28 limits types.MessageLimit,
29 distance int,
30 ) ([]types.MessageItem, int) {
31 items := make([]types.MessageItem, 0, limits.ChunkCount-1)
32 runes := []rune(input)
33 chunkOffset := 0
34 maxTotal := Min(len(runes), limits.TotalChunkSize)
35 maxCount := limits.ChunkCount - 1
36
37 if input == "" {
38 // Return an empty slice for empty input.
39 return items, 0
40 }
41
42 for range maxCount {
43 // Default chunkEnd is the maximum chunk size from current offset.
44 chunkEnd := chunkOffset + limits.ChunkSize
45 // Default next chunk starts immediately after this one.
46 nextChunkStart := chunkEnd
47
48 if chunkEnd >= maxTotal {
49 // The remaining content fits within limits; use all remaining runes.
50 chunkEnd = maxTotal
51 nextChunkStart = maxTotal
52 } else {
53 // Search backwards for a whitespace to split at a word boundary.
54 for r := range distance {
55 rp := chunkEnd - r
56 if runes[rp] == '\n' || runes[rp] == ' ' {
57 // Found a suitable split point at this whitespace.
58 chunkEnd = rp
59 // Skip the whitespace in the next chunk.
60 nextChunkStart = chunkEnd + 1
61
62 break
63 }
64 }
65 }
66
67 //nolint:exhaustruct // MessageItem only requires Text field for this use case
68 items = append(items, types.MessageItem{
69 Text: string(runes[chunkOffset:chunkEnd]),
70 })
71
72 chunkOffset = nextChunkStart
73 if chunkOffset >= maxTotal {
74 break
75 }
76 }
77
78 return items, len(runes) - chunkOffset
79 }
80
81 // Ellipsis truncates text to maxLength runes, appending an ellipsis if truncated.
82 //
83 // Parameters:
84 // - text: The string to potentially truncate.
85 // - maxLength: The maximum length in runes; must be at least len(ellipsis).
86 //
87 // Returns:
88 // - The original text if it fits within maxLength, otherwise the truncated text
89 // with " [...]" appended.
90 func Ellipsis(text string, maxLength int) string {
91 if len(text) > maxLength {
92 text = text[:maxLength-len(ellipsis)] + ellipsis
93 }
94
95 return text
96 }
97
98 // MessageItemsFromLines creates batches of MessageItem from newline-separated text.
99 //
100 // This function splits the input by newlines and creates batches that respect
101 // the chunk count and total chunk size limits. Individual lines that exceed
102 // ChunkSize are truncated and have an ellipsis appended.
103 //
104 // Parameters:
105 // - plain: The input text containing newline-separated lines.
106 // - limits: The message limits for chunking (ChunkSize, TotalChunkSize, ChunkCount).
107 //
108 // Returns:
109 // - A slice of message item batches, where each batch is a slice of MessageItem.
110 func MessageItemsFromLines(plain string, limits types.MessageLimit) [][]types.MessageItem {
111 maxCount := limits.ChunkCount
112 lines := strings.Split(plain, "\n")
113 batches := make([][]types.MessageItem, 0)
114 items := make([]types.MessageItem, 0, Min(maxCount, len(lines)))
115
116 totalLength := 0
117
118 for _, line := range lines {
119 maxLen := limits.ChunkSize
120
121 if len(items) == maxCount || totalLength+maxLen > limits.TotalChunkSize {
122 // Current batch is full; start a new batch.
123 batches = append(batches, items)
124 items = items[:0]
125 }
126
127 runes := []rune(line)
128 if len(runes) > maxLen {
129 // Truncate long lines and append ellipsis.
130 runes = runes[:maxLen-len(ellipsis)]
131 line = string(runes) + ellipsis
132 }
133
134 if len(runes) < 1 {
135 // Skip empty lines.
136 continue
137 }
138
139 //nolint:exhaustruct // MessageItem only requires Text field for this use case
140 items = append(items, types.MessageItem{
141 Text: line,
142 })
143
144 totalLength += len(runes)
145 }
146
147 if len(items) > 0 {
148 batches = append(batches, items)
149 }
150
151 return batches
152 }