Skip to content
Snippets Groups Projects

listen for xdg notifications

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Nogweii S

    A ruby script (powered by ruby-dbus and pastel) to print out all Linux desktop notifications to the terminal as they happen. Useful to provide a bit of history when your existing notification daemon does not.

    Edited
    notification-watch.rb 1.55 KiB
    #!/usr/bin/ruby
    
    require 'dbus'
    require 'pastel'
    
    class EavesdropRule < DBus::MatchRule
      def to_s
        result = super
        "eavesdrop=true,#{result}"
      end
    end
    
    class MonitoringBus < DBus::Connection
      attr_accessor :monitoring_mode
      attr_accessor :monitoring_func
    
      def new(path)
        super(path)
        @monitoring_mode = false
        @monitoring_func = Proc.new {}
      end
    
      def process(m)
        if @monitoring_mode
          @monitoring_func.call(m)
        else
          super(m)
        end
      end
    end
    
    @pastel = Pastel.new(enabled: $stdout.tty?)
    
    mr = EavesdropRule.new
    mr.interface = 'org.freedesktop.Notifications'
    mr.member = 'Notify'
    mr.path = '/org/freedesktop/Notifications'
    
    # bus = DBus::SessionBus.instance
    bus = MonitoringBus.new(DBus::SessionBus.session_bus_address)
    bus.send(:send_hello)
    
    monitoring = bus.service("org.freedesktop.DBus").object("/org/freedesktop/DBus")["org.freedesktop.DBus.Monitoring"]
    monitoring.BecomeMonitor([mr.to_s], 0)
    
    bus.monitoring_mode = true
    bus.monitoring_func = ->(message) do
      # Skip the NameLost message we receive when becoming a monitor
      return if message.member == "NameLost"
    
      sender = @pastel.cyan(message.params[0])
      notif_summary = message.params[3]
      notif_body = message.params[4]
      if notif_body.empty?
        notification_text = @pastel.green(notif_summary)
      else
        notification_text = "#{@pastel.green(notif_summary)}: #{@pastel.magenta(notif_body)}"
      end
      puts "#{sender}: #{notification_text}"
    end
    
    begin
      puts "Waiting for XDG notifications..."
      main = DBus::Main.new
      main << bus
      main.run
    rescue Interrupt => e
      puts "Goodbye!"
    end
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment