Skip to content

Commit 99aaa63

Browse files
committed
Rules for silencing or ignoring applications.
Added simple regex based rules to perform following actions: 1. *Silence* Do not play audio on notification 2. *Ignore* Ignore the notification
1 parent 45d5ebd commit 99aaa63

File tree

6 files changed

+170
-8
lines changed

6 files changed

+170
-8
lines changed

cmd/hyprnotify/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
func main() {
99
var disableSound bool
10+
var configPathOverride string
1011

1112
Cmd := &cobra.Command{
1213
Use: "hyprnotify",
@@ -21,6 +22,9 @@ func main() {
2122
CmdFlags.BoolVarP(&disableSound, "no-sound", "s", false, "disable sound, silent mode")
2223
CmdFlags.Uint8VarP(&internal.DefaultFontSize, "font-size", "f", 13, "set default font size (range 1-255)")
2324
CmdFlags.BoolVar(&internal.FixedFontSize, "fixed-font-size", false, "makes font size fixed, ignoring new sizes")
25+
CmdFlags.StringVarP(&configPathOverride, "config", "c", "", "override the config path")
26+
27+
internal.Config.Load(configPathOverride)
2428

2529
Cmd.Execute()
2630
}

flake.nix

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
description = "Hyprnotification Go DevShell";
3+
4+
inputs = {
5+
nixpkgs.url = "nixpkgs/nixos-unstable";
6+
flake-utils.url = "github:numtide/flake-utils";
7+
goflake.url = "github:sagikazarmark/go-flake";
8+
goflake.inputs.nixpkgs.follows = "nixpkgs";
9+
};
10+
11+
outputs = { self, nixpkgs, flake-utils, goflake, ... }:
12+
flake-utils.lib.eachDefaultSystem (system:
13+
let
14+
pkgs = import nixpkgs {
15+
inherit system;
16+
17+
overlays = [ goflake.overlay ];
18+
};
19+
buildDeps = with pkgs; [ git go_1_17 gnumake ];
20+
devDeps = with pkgs; buildDeps ++ [
21+
golangci-lint
22+
gotestsum
23+
goreleaser
24+
protobuf
25+
protoc-gen-go
26+
protoc-gen-go-grpc
27+
protoc-gen-kit
28+
# gqlgen
29+
openapi-generator-cli
30+
ent
31+
32+
libnotify
33+
alsa-lib.dev
34+
];
35+
in
36+
{ devShell = pkgs.mkShell { buildInputs = devDeps; }; });
37+
}

go.mod

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,29 @@ require github.com/godbus/dbus/v5 v5.1.0
66

77
require github.com/spf13/cobra v1.8.1
88

9+
require github.com/spf13/viper v1.20.1
10+
911
require (
12+
github.com/fsnotify/fsnotify v1.8.0 // indirect
13+
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
1014
github.com/inconshreveable/mousetrap v1.1.0 // indirect
11-
github.com/spf13/pflag v1.0.5 // indirect
15+
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
16+
github.com/sagikazarmark/locafero v0.7.0 // indirect
17+
github.com/sourcegraph/conc v0.3.0 // indirect
18+
github.com/spf13/afero v1.12.0 // indirect
19+
github.com/spf13/cast v1.7.1 // indirect
20+
github.com/spf13/pflag v1.0.6 // indirect
21+
github.com/subosito/gotenv v1.6.0 // indirect
22+
go.uber.org/atomic v1.9.0 // indirect
23+
go.uber.org/multierr v1.9.0 // indirect
24+
golang.org/x/text v0.21.0 // indirect
25+
gopkg.in/yaml.v3 v3.0.1 // indirect
1226
)
1327

1428
require (
1529
github.com/ebitengine/oto/v3 v3.1.0 // indirect
1630
github.com/ebitengine/purego v0.5.0 // indirect
1731
github.com/gopxl/beep v1.4.0
1832
github.com/pkg/errors v0.9.1 // indirect
19-
golang.org/x/sys v0.12.0 // indirect
33+
golang.org/x/sys v0.29.0 // indirect
2034
)

go.sum

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,71 @@
11
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
2+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
23
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
34
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
45
github.com/ebitengine/oto/v3 v3.1.0 h1:9tChG6rizyeR2w3vsygTTTVVJ9QMMyu00m2yBOCch6U=
56
github.com/ebitengine/oto/v3 v3.1.0/go.mod h1:IK1QTnlfZK2GIB6ziyECm433hAdTaPpOsGMLhEyEGTg=
67
github.com/ebitengine/purego v0.5.0 h1:JrMGKfRIAM4/QVKaesIIT7m/UVjTj5GYhRSQYwfVdpo=
78
github.com/ebitengine/purego v0.5.0/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
9+
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
10+
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
11+
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
12+
github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
13+
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
14+
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
815
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
916
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
17+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
18+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
1019
github.com/gopxl/beep v1.4.0 h1:pJERVDZMJkf49R1g/tV9DhVct4xNRuTlyMnMa53gGsc=
1120
github.com/gopxl/beep v1.4.0/go.mod h1:gGVz7MJKlfHrmkzr0wSLGNyY7oisM6rFWJnaLjNxEwA=
1221
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
1322
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
23+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
24+
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
25+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
26+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
1427
github.com/orcaman/writerseeker v0.0.0-20200621085525-1d3f536ff85e h1:s2RNOM/IGdY0Y6qfTeUKhDawdHDpK9RGBdx80qN4Ttw=
1528
github.com/orcaman/writerseeker v0.0.0-20200621085525-1d3f536ff85e/go.mod h1:nBdnFKj15wFbf94Rwfq4m30eAcyY9V/IyKAGQFtqkW0=
29+
github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
30+
github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
1631
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
1732
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1833
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1934
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
35+
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
36+
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
2037
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
38+
github.com/sagikazarmark/locafero v0.7.0 h1:5MqpDsTGNDhY8sGp0Aowyf0qKsPrhewaLSsFaodPcyo=
39+
github.com/sagikazarmark/locafero v0.7.0/go.mod h1:2za3Cg5rMaTMoG/2Ulr9AwtFaIppKXTRYnozin4aB5k=
40+
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
41+
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
42+
github.com/spf13/afero v1.12.0 h1:UcOPyRBYczmFn6yvphxkn9ZEOY65cpwGKb5mL36mrqs=
43+
github.com/spf13/afero v1.12.0/go.mod h1:ZTlWwG4/ahT8W7T0WQ5uYmjI9duaLQGy3Q2OAl4sk/4=
44+
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
45+
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
2146
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
2247
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
23-
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
2448
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
25-
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
26-
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
27-
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
28-
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
49+
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
50+
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
51+
github.com/spf13/viper v1.20.1 h1:ZMi+z/lvLyPSCoNtFCpqjy0S4kPbirhpTMwl8BkW9X4=
52+
github.com/spf13/viper v1.20.1/go.mod h1:P9Mdzt1zoHIG8m2eZQinpiBjo6kCmZSKBClNNqjJvu4=
53+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
54+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
55+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
56+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
57+
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
58+
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
59+
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
60+
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
61+
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
62+
go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
63+
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
64+
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
65+
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
66+
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
2967
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
68+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
69+
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3070
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
3171
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/config.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package internal
2+
3+
import (
4+
"fmt"
5+
"path"
6+
// "log"
7+
"github.com/spf13/viper"
8+
)
9+
10+
type action struct {
11+
Silence bool
12+
Ignore bool
13+
}
14+
15+
type RuleEntry struct {
16+
Matches string
17+
Action action
18+
}
19+
20+
type config struct {
21+
Rule []RuleEntry
22+
}
23+
24+
var (
25+
Config config
26+
)
27+
28+
func (c *config) Load(location string) {
29+
if location != "" {
30+
fileDir := path.Dir(location)
31+
fileName := path.Base(location)
32+
33+
viper.SetConfigName(fileName)
34+
viper.AddConfigPath(fileDir)
35+
} else {
36+
viper.SetConfigName("hyprnotify")
37+
viper.AddConfigPath("$HOME/.config/hypr")
38+
}
39+
40+
if err := viper.ReadInConfig(); err != nil {
41+
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
42+
// Config file not found; ignore error if desired
43+
fmt.Printf("Config file not found: %s \n", err)
44+
} else {
45+
fmt.Printf("Fatal error in config file: %s \n", err)
46+
}
47+
*c = config{}
48+
}
49+
50+
if err := viper.Unmarshal(c); err != nil {
51+
fmt.Printf("Could not unmarshal the config file: %s \n", err)
52+
}
53+
}

internal/dbus.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ func (n DBusNotify) Notify(
9292
expire_timeout int32,
9393
) (uint32, *dbus.Error) {
9494

95+
ruleAction := match_config(app_name)
96+
if ruleAction.Ignore {
97+
return current_id, nil
98+
}
99+
95100
if replaces_id > 0 {
96101
n.CloseNotification(replaces_id)
97102
}
@@ -130,7 +135,7 @@ func (n DBusNotify) Notify(
130135
}
131136
hyprsock.SendNotification(&nf)
132137

133-
if sound {
138+
if sound && !ruleAction.Silence {
134139
go PlayAudio()
135140
}
136141
// ClosedNotification Signal Stuff
@@ -211,6 +216,15 @@ func parse_hints(nf *Notification, hints map[string]dbus.Variant) {
211216
}
212217
}
213218

219+
func match_config(appName string) action {
220+
for _, rule := range Config.Rule {
221+
if match, err := regexp.MatchString(rule.Matches, appName); err == nil && match {
222+
return rule.Action
223+
}
224+
}
225+
return action{}
226+
}
227+
214228
func InitDBus(enable_sound bool) {
215229
sound = enable_sound
216230

0 commit comments

Comments
 (0)