all repos — gnotif @ c7bd6b552052b34ec54f0174a4fcab43a49d1e72

A simple dbus monitor to see desktop notifications in the terminal

main.go (view raw)

 1package main
 2
 3import (
 4	"fmt"
 5	"os"
 6
 7	"github.com/godbus/dbus/v5"
 8)
 9
10var DATETIME_FORMAT = "02-01-2006 15:03"
11
12func main() {
13	conn, err := dbus.ConnectSessionBus()
14	if err != nil {
15		fmt.Fprintln(os.Stderr, "Failed to connect to session bus:", err)
16		os.Exit(1)
17	}
18	defer conn.Close()
19
20	// https://github.com/godbus/dbus/blob/master/_examples/monitor.go
21	rules := []string{
22		"type='signal',member='Notify',path='/org/freedesktop/Notifications',interface='org.freedesktop.Notifications'",
23		"type='method_call',member='Notify',path='/org/freedesktop/Notifications',interface='org.freedesktop.Notifications'",
24		"type='method_return',member='Notify',path='/org/freedesktop/Notifications',interface='org.freedesktop.Notifications'",
25		"type='error',member='Notify',path='/org/freedesktop/Notifications',interface='org.freedesktop.Notifications'",
26	}
27	var flag uint = 0
28
29	call := conn.BusObject().Call("org.freedesktop.DBus.Monitoring.BecomeMonitor", 0, rules, flag)
30	if call.Err != nil {
31		fmt.Fprintln(os.Stderr, "Failed to become monitor:", call.Err)
32		os.Exit(1)
33	}
34
35	// Clear terminal
36	fmt.Print("\033[H\033[2J")
37	fmt.Println(textStyle.Render("💬 Listing dbus notifications\n"))
38
39	c := make(chan *dbus.Message, 10)
40	conn.Eavesdrop(c)
41	for v := range c {
42		if len(v.Body) >= 5 {
43			notification := NewNotification(v)
44			fmt.Println(notification)
45
46		}
47	}
48}