File: notification.rb

package info (click to toggle)
ruby-notiffany 0.1.3-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 580 kB
  • sloc: ruby: 3,094; makefile: 10; sh: 4
file content (62 lines) | stat: -rw-r--r-- 1,805 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module Notiffany
  class Notifier
    class Tmux < Base
      # Wraps a notification with it's options
      class Notification
        def initialize(type, options)
          @type = type
          @options = options
          @color = options[type.to_sym] || options[:default]
          @separator = options[:line_separator]
          @message_color = _value_for(:message_color)
          @client = Client.new(options[:display_on_all_clients] ? :all : nil)
        end

        def display_title(title, message)
          title_format = _value_for(:title_format)

          teaser_message = message.split("\n").first
          display_title = format(title_format, title, teaser_message)

          client.title = display_title
        end

        def display_message(title, message)
          message = _message_for(title, message)

          client.display_time = options[:timeout] * 1000
          client.message_fg = message_color
          client.message_bg = color
          client.display_message(message)
        end

        def colorize(locations)
          locations.each do |location|
            client.set(location, color)
          end
        end

        private

        attr_reader :type
        attr_reader :options
        attr_reader :color
        attr_reader :message_color
        attr_reader :client
        attr_reader :separator

        def _value_for(field)
          format = "#{type}_#{field}".to_sym
          default = options["default_#{field}".to_sym]
          options.fetch(format, default)
        end

        def _message_for(title, message)
          message_format = _value_for(:message_format)
          formatted_message = message.split("\n").join(separator)
          format(message_format, title, formatted_message)
        end
      end
    end
  end
end