Skip to content

Commit 55e8c48

Browse files
feat: messaging and fmts
1 parent 499ec8e commit 55e8c48

File tree

5 files changed

+98
-28
lines changed

5 files changed

+98
-28
lines changed

pkg/notify.go

+27-25
Original file line numberDiff line numberDiff line change
@@ -50,67 +50,69 @@ func Notify(result *ScanResult, f Flags, version string) {
5050
Message: version,
5151
},
5252
{
53-
Label: "File Path",
53+
Label: "File",
5454
Message: result.FilePath,
5555
},
5656
{
57-
Label: "Running Every",
58-
Message: fmt.Sprintf("%d secs", f.Every),
59-
},
60-
{
61-
Label: "Match Pattern",
57+
Label: "Match",
6258
Message: f.Match,
6359
},
6460
{
65-
Label: "Ignore Pattern",
61+
Label: "Ignore",
6662
Message: f.Ignore,
6763
},
6864
{
69-
Label: "First Line",
70-
Message: Truncate(result.FirstLine, TruncateMax),
71-
},
72-
{
73-
Label: "Mid Lines",
74-
Message: result.PreviewLine,
65+
Label: "Lines",
66+
Message: fmt.Sprintf(
67+
"%s\n\r%s\n\r%s",
68+
Truncate(result.FirstLine, TruncateMax),
69+
ReduceToNLines(result.PreviewLine, 3),
70+
Truncate(result.LastLine, TruncateMax),
71+
),
7572
},
7673
{
77-
Label: "Last Line",
78-
Message: Truncate(result.LastLine, TruncateMax),
74+
Label: "Settings",
75+
Message: fmt.Sprintf(
76+
"min (%d), every (%d secs), max streak (%d)",
77+
f.Min,
78+
f.Every,
79+
f.Streak,
80+
),
7981
},
8082
{
81-
Label: "Details",
83+
Label: "Scan Details",
8284
Message: fmt.Sprintf(
83-
"Min Threshold: %d, Lines Read: %d\n\rMatches Found: %d, Ratio %.2f%%",
84-
f.Min,
85-
result.LinesRead,
86-
result.ErrorCount,
85+
"lines read (%s), %.2f%% errors (%s), scans til date (%s)",
86+
NumberToK(result.LinesRead),
8787
result.ErrorPercent,
88+
NumberToK(result.ErrorCount),
89+
NumberToK(result.ScanCount),
8890
),
8991
},
9092
{
9193
Label: "Streaks",
92-
Message: StreakSymbols(result.Streak, f.Streak, f.Min) + "\n\r" + fmt.Sprintf("Last %d failed. Scan counter: %d", f.Streak, result.ScanCount),
94+
Message: StreakSymbols(result.Streak, f.Streak, f.Min),
9395
},
9496
}
9597
if result.FirstDate != "" || result.LastDate != "" {
9698
var duration string
9799
if result.FirstDate != "" && result.LastDate != "" {
98100
firstDate, err := time.Parse("2006-01-02 15:04:05", result.FirstDate)
99101
if err != nil {
100-
duration = "X"
102+
duration = ""
101103
} else {
102104
lastDate, err := time.Parse("2006-01-02 15:04:05", result.LastDate)
103105
if err == nil {
104-
duration = lastDate.Sub(firstDate).String()
106+
duration = fmt.Sprintf("(%s)", lastDate.Sub(firstDate).String())
105107
} else {
106-
duration = "X"
108+
duration = ""
107109
}
108110
}
109111
}
110112

111113
details = append(details, gmt.Details{
112114
Label: "Range",
113-
Message: fmt.Sprintf("%s to %s (Duration: %s)", result.FirstDate, result.LastDate, duration),
115+
Message: fmt.Sprintf("%s to %s %s", result.FirstDate, result.LastDate, duration),
114116
})
115117
}
116118

pkg/strings.go

+22-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/sha256"
55
"encoding/hex"
66
"log/slog"
7+
"strconv"
78
"strings"
89
"sync"
910

@@ -87,6 +88,26 @@ func StreakSymbols(arr []int, length int, minimum int) string {
8788
for i := len(symbols); i < DisplayableStreakNumber(length); i++ {
8889
symbols = append([]string{"□"}, symbols...)
8990
}
91+
// if last is ✕ then replace with ✖(bold)
92+
if symbols[len(symbols)-1] == "✕" {
93+
symbols[len(symbols)-1] = "✖"
94+
}
95+
96+
return strings.Join(symbols, "")
97+
}
98+
99+
func NumberToK(num int) string {
100+
if num >= 1000 {
101+
return strconv.FormatFloat(float64(num)/1000, 'f', 1, 64) + "K"
102+
}
103+
return strconv.Itoa(num)
104+
}
90105

91-
return strings.Join(symbols, " ")
106+
// reduce to n lines
107+
func ReduceToNLines(s string, n int) string {
108+
lines := strings.Split(s, "\n")
109+
if len(lines) <= n {
110+
return s
111+
}
112+
return strings.Join(lines[:n], "\n")
92113
}

pkg/strings_test.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package pkg
2+
3+
import (
4+
"strconv"
5+
"testing"
6+
)
7+
8+
func TestNumberToK(t *testing.T) {
9+
tests := []struct {
10+
input int
11+
expected string
12+
}{
13+
{999, "999"}, // Less than 1000
14+
{1000, "1.0K"}, // Exactly 1000
15+
{1500, "1.5K"}, // Simple thousands
16+
{25000, "25.0K"}, // Larger thousands
17+
{1000000, "1000.0K"}, // Very large numbers
18+
{0, "0"}, // Edge case: zero
19+
{-500, "-500"}, // Negative numbers (no conversion)
20+
{-1500, "-1500"}, // Negative thousand (not supported)
21+
}
22+
23+
for _, test := range tests {
24+
t.Run(
25+
"Input: "+strconv.Itoa(test.input),
26+
func(t *testing.T) {
27+
result := NumberToK(test.input)
28+
if result != test.expected {
29+
t.Errorf("ConvertToK(%d) = %s; expected %s", test.input, result, test.expected)
30+
}
31+
},
32+
)
33+
}
34+
}

pkg/watcher.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,23 @@ func (w *Watcher) Scan() (*ScanResult, error) {
117117
currentLineNum := 1
118118
linesRead := 0
119119
bytesRead := w.lastFileSize
120+
isFirstScan := w.getScanCount() == 0
120121

121122
for scanner.Scan() {
122-
line := scanner.Bytes()
123-
bytesRead += int64(len(line)) + 1 // Adding 1 for the newline character
124123
currentLineNum++
125124
linesRead = currentLineNum - w.lastLineNum
126125
// Convert to positive number
127126
if linesRead < 0 {
128127
linesRead = -linesRead
129128
}
130129

130+
line := scanner.Bytes()
131+
bytesRead += int64(len(line)) + 1 // Adding 1 for the newline character
132+
133+
if isFirstScan {
134+
continue
135+
}
136+
131137
if w.ignorePattern != "" && regIgnore.Match(line) {
132138
continue
133139
}

pkg/watcher_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ func TestNewWatcher(t *testing.T) {
3535
caches := make(map[string]*cache.Cache)
3636
caches[filePath] = cache.New(cache.NoExpiration, cache.NoExpiration)
3737
watcher, err := NewWatcher(filePath, f, caches[filePath])
38+
watcher.incrementScanCount()
3839
assert.NoError(t, err)
3940
assert.NotNil(t, watcher)
4041

@@ -62,6 +63,7 @@ error:1`
6263
caches := make(map[string]*cache.Cache)
6364
caches[filePath] = cache.New(cache.NoExpiration, cache.NoExpiration)
6465
watcher, err := NewWatcher(filePath, f, caches[filePath])
66+
watcher.incrementScanCount()
6567
assert.NoError(t, err)
6668
defer watcher.Close()
6769

@@ -91,6 +93,7 @@ line2`
9193
caches := make(map[string]*cache.Cache)
9294
caches[filePath] = cache.New(cache.NoExpiration, cache.NoExpiration)
9395
watcher, err := NewWatcher(filePath, f, caches[filePath])
96+
watcher.incrementScanCount()
9497
assert.NoError(t, err)
9598
defer watcher.Close()
9699

@@ -134,6 +137,7 @@ error:1`
134137
caches := make(map[string]*cache.Cache)
135138
caches[filePath] = cache.New(cache.NoExpiration, cache.NoExpiration)
136139
watcher, err := NewWatcher(filePath, f, caches[filePath])
140+
watcher.incrementScanCount()
137141
if err != nil {
138142
b.Fatal(err)
139143
}
@@ -160,6 +164,7 @@ func BenchmarkLoadAndSaveState(b *testing.B) {
160164
caches := make(map[string]*cache.Cache)
161165
caches[filePath] = cache.New(cache.NoExpiration, cache.NoExpiration)
162166
watcher, err := NewWatcher(filePath, f, caches[filePath])
167+
watcher.incrementScanCount()
163168
if err != nil {
164169
b.Fatal(err)
165170
}
@@ -171,6 +176,7 @@ func BenchmarkLoadAndSaveState(b *testing.B) {
171176
caches := make(map[string]*cache.Cache)
172177
caches[filePath] = cache.New(cache.NoExpiration, cache.NoExpiration)
173178
_, err := NewWatcher(filePath, f, caches[filePath])
179+
watcher.incrementScanCount()
174180
if err != nil {
175181
b.Fatal(err)
176182
}
@@ -198,6 +204,7 @@ line2`
198204
caches := make(map[string]*cache.Cache)
199205
caches[filePath] = cache.New(cache.NoExpiration, cache.NoExpiration)
200206
watcher, err := NewWatcher(filePath, f, caches[filePath])
207+
watcher.incrementScanCount()
201208
if err != nil {
202209
b.Fatal(err)
203210
}

0 commit comments

Comments
 (0)