Class: Notify

Inherits:
Object
  • Object
show all
Defined in:
lib/notify.rb

Class Method Summary collapse

Class Method Details

.bubble(message, title) ⇒ Object

Display a notification bubble



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/notify.rb', line 3

def self.bubble(message, title)
  begin
    if Utils.os == :macosx
      if !osx_notification(message, title)
        raise "No handler found, cannot show bubble notification"
      end
    else
      if !notifysend(message, title)
        raise "No handler found, cannot show bubble notification"
      #elsif zenity(message, title)
      #  raise "No handler found, cannot show bubble notification"
      end
    end
  rescue Exception => e
    warning("[self.bubble] #{e.message}")
  end
end

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Notify)

    the object that the method was called on



89
90
91
# File 'lib/notify.rb', line 89

def self.configure
  yield self if block_given?
end

.error(message) ⇒ Object

Prints a pre-formatted error message to the console



34
35
36
37
38
39
40
# File 'lib/notify.rb', line 34

def self.error(message)
  message = message.split("\n").join("\n\u2716 ")
  inline("\u2716 #{message} - #{Utils.formatted_time}", :red)
  inline("\u2716 Exiting...", :red)

  exit
end

.info(message) ⇒ Object

Prints a pre-formatted informational message to the console



49
50
51
# File 'lib/notify.rb', line 49

def self.info(message)
  inline("#{message} - #{Utils.formatted_time}", :blue)
end

.inline(message, colour = nil, style = nil) ⇒ Object

Collate colour and style, build message string in format of “e[#style;#colourm#texte[0m”



108
109
110
# File 'lib/notify.rb', line 108

def self.inline(message, colour = nil, style = nil)
  puts Style.format(message, colour, style)
end


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/notify.rb', line 21

def self.modal(message, title)
  begin
    if Utils.os == :macosx
      if !osx_modal(message, title)
        raise "No handler found, cannot show system popup"
      end
    end
  rescue Exception => e
    warning("[self.bubble] #{e.message}")
  end
end

.notifysend(message, title) ⇒ Object

Linux system notification



134
135
136
137
138
139
140
141
142
# File 'lib/notify.rb', line 134

def self.notifysend(message, title)
  begin
    @response = `notify-send "#{title}" "#{message}"`

    $?.exitstatus == 0
  rescue SystemExit, Interrupt
    error("Interrupt caught, exiting")
  end
end

.osx_modal(message, title, icon = :caution) ⇒ Object

OSX system modal popup



123
124
125
126
127
128
129
130
131
# File 'lib/notify.rb', line 123

def self.osx_modal(message, title, icon = :caution)
  begin
    @response = `osascript -e 'tell app "System Events" to display dialog "#{message}" buttons {"OK"} default button 1 with title "#{title}" with icon #{icon}'`

    $?.exitstatus == 0
  rescue SystemExit, Interrupt
    error("Interrupt caught, exiting")
  end
end

.osx_notification(message, title) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'lib/notify.rb', line 112

def self.osx_notification(message, title)
  begin
    @response = `osascript -e 'display notification "#{message}" with title "#{title}"'`

    $?.exitstatus == 0
  rescue SystemExit, Interrupt
    error("Interrupt caught, exiting")
  end
end

.plugins=(plugin_config_arr) ⇒ Object

register new plugins



94
95
96
97
98
99
100
101
102
103
# File 'lib/notify.rb', line 94

def self.plugins=(plugin_config_arr)
  plugin_config_arr.each do |hash|
    hash.each_pair do |plugin, key|
      # include the requested plugin
      require_relative "plugins/#{plugin.downcase}.rb"

      instance_variable_set("@#{plugin}_key".to_sym, key)
    end
  end
end

.sinfo(message) ⇒ Object

Prints a pre-formatted secondary informational message to the console



54
55
56
57
# File 'lib/notify.rb', line 54

def self.sinfo(message)
  message = message.split("\n").join("\n\u2011 ")
  inline("\u2011 #{message} - #{Utils.formatted_time}", :cyan)
end

.spacerObject

pretty-print a spacer



85
86
87
# File 'lib/notify.rb', line 85

def self.spacer
  inline("\u2011 =============", :magenta)
end

.spit(message) ⇒ Object

Prints a pre-formatted unstyled message to the console



66
67
68
# File 'lib/notify.rb', line 66

def self.spit(message)
  inline(message, :null)
end

.success(message) ⇒ Object

Prints a pre-formatted success message to the console



60
61
62
63
# File 'lib/notify.rb', line 60

def self.success(message)
  message = message.split("\n").join("\n\u2713 ")
  inline("\u2713 #{message} - #{Utils.formatted_time}", :green)
end

.warning(message) ⇒ Object

Prints a pre-formatted warning message to the console



43
44
45
46
# File 'lib/notify.rb', line 43

def self.warning(message)
  message = message.split("\n").join("\n\u2011 ")
  inline("\u2011 #{message} - #{Utils.formatted_time}", :yellow)
end

.workingon(message, print_info_message = false) ⇒ Object

Send status updates to WorkingOn



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/notify.rb', line 71

def self.workingon(message, print_info_message = false)
  begin
    plugin = Plugin::WorkingOn.new
    plugin.send(message)

    if print_info_message
      info(message)
    end
  rescue Exception => e
    error("Error notifying WO - #{e.message}")
  end
end

.zenity(message, title) ⇒ Object



144
145
146
147
148
149
150
151
152
# File 'lib/notify.rb', line 144

def self.zenity(message, title)
  begin
    @response = `echo "message:#{message}" | zenity --notification --listen`

    $?.exitstatus == 0
  rescue SystemExit, Interrupt
    error("Interrupt caught, exiting")
  end
end