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
|
package app
import (
"fmt"
"strings"
"time"
)
func truncate(text string, maxLength int) string {
if maxLength <= 0 {
return ""
}
text = strings.ReplaceAll(text, "\n", " ")
if len(text) <= maxLength {
return text
}
if maxLength <= 2 {
return text[:maxLength]
}
return text[:maxLength-2] + " …"
}
func formatTime(timestamp time.Time) string {
now := time.Now()
difference := now.Sub(timestamp)
switch {
case difference < time.Minute:
return "just now"
case difference < time.Hour:
return fmt.Sprintf("%dm ago", int(difference.Minutes()))
case difference < 24*time.Hour:
return fmt.Sprintf("%dh ago", int(difference.Hours()))
case difference < 7*24*time.Hour:
return fmt.Sprintf("%dd ago", int(difference.Hours()/24))
default:
return timestamp.Format("Jan 2")
}
}
func max(first, second int) int {
if first > second {
return first
}
return second
}
func min(first, second int) int {
if first < second {
return first
}
return second
}
func highlightMatches(text, query string) string {
if query == "" {
return text
}
queryLower := strings.ToLower(query)
textLower := strings.ToLower(text)
var result strings.Builder
lastEnd := 0
for {
index := strings.Index(textLower[lastEnd:], queryLower)
if index == -1 {
result.WriteString(text[lastEnd:])
break
}
matchStart := lastEnd + index
result.WriteString(text[lastEnd:matchStart])
matchEnd := matchStart + len(query)
result.WriteString("\033[43;30m")
result.WriteString(text[matchStart:matchEnd])
result.WriteString("\033[0m")
lastEnd = matchEnd
}
return result.String()
}
func wrapText(text string, width int) string {
if width <= 0 {
return text
}
var result strings.Builder
words := strings.Fields(text)
lineLength := 0
for wordIndex, word := range words {
wordLength := len(word)
if lineLength+wordLength+1 > width && lineLength > 0 {
result.WriteString("\n")
lineLength = 0
}
if lineLength > 0 {
result.WriteString(" ")
lineLength += 1
}
result.WriteString(word)
lineLength += wordLength
if lineLength > width && wordIndex < len(words)-1 {
result.WriteString("\n")
lineLength = 0
}
}
return result.String()
}
|