Skip to content

Commit 8d9925b

Browse files
committed
refactor(emitter): simplify banOrPickFilter with slices.Contains
1 parent 5638a6a commit 8d9925b

File tree

1 file changed

+37
-38
lines changed

1 file changed

+37
-38
lines changed

internal/emitter/emitter.go

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,93 +7,95 @@ import (
77
"fmt"
88
"io"
99
"os"
10+
"slices"
1011
"strings"
1112

1213
"github.com/rokath/trice/internal/receiver"
1314
"github.com/rokath/trice/pkg/msg"
1415
)
1516

1617
var (
17-
// Verbose gives more information on output if set. The value is injected from main packages.
18+
// Verbose enables optional diagnostic output.
1819
Verbose bool
1920

20-
// HostStamp is used for line timestamps.
21-
// off = no timestamp
22-
// none = no timestamp
21+
// HostStamp configures line timestamps.
22+
// off/none = no timestamp
2323
// LOCmicro = local time with microseconds
24-
// UTCmicro = universal time with microseconds
24+
// UTCmicro = UTC time with microseconds
2525
// zero = fixed "2006-01-02_1504-05" timestamp (for tests)
2626
HostStamp string
2727

28-
// Prefix starts lines. It follows line timestamp, if any.
28+
// Prefix is written before the first payload fragment of each line.
2929
Prefix string
3030

31-
// Suffix lollows lines. Usually empty.
31+
// Suffix is written when a line is completed. Usually empty.
3232
Suffix string
3333

34-
// ColorPalette determines the way color is handled.
34+
// ColorPalette controls tag-prefix handling.
3535
// off = no color handling at all. Lower case color prefixes are not removed. Use with care.
3636
// none = no colors. Lower case color prefixes are removed.
3737
// default = color codes added (TODO: change to ANSI)
3838
ColorPalette string
3939

40-
// IPAddr ist the remote display IP address.
40+
// IPAddr is the remote display server IP address.
4141
IPAddr string
4242

43-
// IPPort ist the remote display port number.
43+
// IPPort is the remote display server TCP port.
4444
IPPort string
4545

46-
// DisplayRemote if set, sends trice lines over TCP.
46+
// DisplayRemote sends lines to a remote RPC display server when true.
4747
DisplayRemote bool
4848

49-
// TestTableMode is set externally to avoid Prefix overwrite
49+
// TestTableMode avoids Prefix rewrites in New.
5050
TestTableMode bool
5151

52-
// NextLine is set true as help for decoder.TestTableMode, where it is clreared at line start.
52+
// NextLine is set when a line is completed.
53+
// It is used by decoder table-mode output coordination.
5354
NextLine bool
5455

55-
// Ban is a string slice containing all channel descriptors to suppress
56+
// Ban contains channel tags to suppress.
5657
Ban channelArrayFlag
5758

58-
// Pick is a string slice containing all channel descriptors only to display
59+
// Pick contains channel tags to display exclusively.
5960
Pick channelArrayFlag
6061

61-
// UserLabel is a string slice containing all additional channel descriptors
62+
// UserLabel adds user-defined tag names at runtime.
6263
UserLabel ArrayFlag
6364
)
6465

65-
// ArrayFlag is a slice type for multi flag
66+
// ArrayFlag supports repeated CLI values.
6667
type ArrayFlag []string
6768

68-
// String method is the needed for interface satisfaction.
69+
// String implements flag.Value.
6970
func (i *ArrayFlag) String() string {
7071
return ""
7172
}
7273

73-
// Set is a needed method for multi flags.
74+
// Set implements flag.Value.
7475
func (i *ArrayFlag) Set(value string) error {
7576
*i = append(*i, value)
7677
return nil
7778
}
7879

80+
// channelArrayFlag stores canonical and alias tag names used by -ban/-pick.
7981
type channelArrayFlag []string
8082

81-
// String method is the needed for interface satisfaction.
83+
// String implements flag.Value.
8284
func (i *channelArrayFlag) String() string {
8385
return fmt.Sprintf("%v", *i)
8486
}
8587

86-
// https://stackoverflow.com/questions/9251234/go-append-if-unique
87-
func appendIfMissing(slice []string, i string) []string {
88+
// appendIfMissing appends item only when not already present.
89+
func appendIfMissing(slice []string, item string) []string {
8890
for _, ele := range slice {
89-
if ele == i {
91+
if ele == item {
9092
return slice
9193
}
9294
}
93-
return append(slice, i)
95+
return append(slice, item)
9496
}
9597

96-
// Set is a needed method for multi flags.
98+
// Set expands known tag aliases and appends unique entries.
9799
func (i *channelArrayFlag) Set(value string) error {
98100
ss := strings.Split(value, ":")
99101
for _, s := range ss {
@@ -112,7 +114,7 @@ type LineWriter interface {
112114
WriteLine([]string)
113115
}
114116

115-
// newLineWriter provides a lineWriter which can be a remote Display or the local console.
117+
// newLineWriter creates either a remote or local line writer.
116118
func newLineWriter(w io.Writer) (lwD LineWriter) {
117119
if DisplayRemote {
118120
p := newRemoteDisplay(w, os.Args)
@@ -134,7 +136,7 @@ func New(w io.Writer) *TriceLineComposer {
134136
return newLineComposer(newLineWriter(w))
135137
}
136138

137-
// setPrefix changes "source:" to e.g., "JLINK:".
139+
// setPrefix rewrites the default "source:" prefix to "<receiver.Port>:".
138140
func setPrefix() {
139141
defaultPrefix := "source:"
140142
if strings.HasPrefix(Prefix, defaultPrefix) {
@@ -144,7 +146,7 @@ func setPrefix() {
144146
}
145147
}
146148

147-
// BanOrPickFilter returns len of b if b ist not filtered out, otherwise 0.
149+
// BanOrPickFilter returns len(b) when b passes filtering, otherwise 0.
148150
// If Ban and Pick are nil nothing is filtered out.
149151
// If Ban and Pick are both not nil this is a fatal error (os.Exit).
150152
// If b starts with a known channel specifier existent in Ban 0, is returned.
@@ -153,31 +155,28 @@ func BanOrPickFilter(b []byte) (n int) {
153155
return banOrPickFilter(Ban, Pick, b)
154156
}
155157

158+
// banOrPickFilter applies the -ban/-pick channel rules.
156159
func banOrPickFilter(ban, pick channelArrayFlag, b []byte) int {
157160
if ban == nil && pick == nil {
158161
return len(b) // nothing to filter
159162
}
160-
msg.FatalInfoOnTrue(nil != Ban && nil != Pick, "switches -ban and -pick cannot be used together")
163+
msg.FatalInfoOnTrue(ban != nil && pick != nil, "switches -ban and -pick cannot be used together")
161164
s := string(b)
162165
sc := strings.SplitN(s, ":", 2) // example: "deb" -> []string{ "deb"} "deb:" -> []string{ "deb", "" }
163-
if nil != Ban {
166+
if ban != nil {
164167
if len(sc) < 2 { // no color separator
165168
return len(b) // nothing to filter
166169
}
167-
for _, c := range Ban {
168-
if sc[0] == c {
169-
return 0 // filter match
170-
}
170+
if slices.Contains(ban, sc[0]) {
171+
return 0 // filter match
171172
}
172173
return len(b) // no filter match
173174
} else { // Pick is set
174175
if len(sc) < 2 { // no color separator
175176
return 0 // filter out
176177
}
177-
for _, c := range Pick {
178-
if sc[0] == c {
179-
return len(b) // filter match
180-
}
178+
if slices.Contains(pick, sc[0]) {
179+
return len(b) // filter match
181180
}
182181
return 0 // no filter match
183182
}

0 commit comments

Comments
 (0)