Class: Hiiro::Notification
- Inherits:
-
Object
- Object
- Hiiro::Notification
- Defined in:
- lib/hiiro/notification.rb
Instance Attribute Summary collapse
-
#hiiro ⇒ Object
readonly
Returns the value of attribute hiiro.
-
#original_args ⇒ Object
readonly
Returns the value of attribute original_args.
Class Method Summary collapse
Instance Method Summary collapse
- #args ⇒ Object
- #binpath ⇒ Object
- #has_cmd? ⇒ Boolean
-
#initialize(hiiro) ⇒ Notification
constructor
A new instance of Notification.
- #options ⇒ Object
- #play_sound ⇒ Object
- #show ⇒ Object
- #sounds ⇒ Object
Constructor Details
#initialize(hiiro) ⇒ Notification
Returns a new instance of Notification.
9 10 11 12 |
# File 'lib/hiiro/notification.rb', line 9 def initialize(hiiro) @hiiro = hiiro @original_args = hiiro.args.dup end |
Instance Attribute Details
#hiiro ⇒ Object (readonly)
Returns the value of attribute hiiro.
7 8 9 |
# File 'lib/hiiro/notification.rb', line 7 def hiiro @hiiro end |
#original_args ⇒ Object (readonly)
Returns the value of attribute original_args.
7 8 9 |
# File 'lib/hiiro/notification.rb', line 7 def original_args @original_args end |
Class Method Details
.show(hiiro) ⇒ Object
3 4 5 |
# File 'lib/hiiro/notification.rb', line 3 def self.show(hiiro) new(hiiro).show end |
Instance Method Details
#args ⇒ Object
24 25 26 |
# File 'lib/hiiro/notification.rb', line 24 def args @args ||= hiiro.args end |
#binpath ⇒ Object
56 57 58 |
# File 'lib/hiiro/notification.rb', line 56 def binpath `command -v terminal-notifier` end |
#has_cmd? ⇒ Boolean
60 61 62 |
# File 'lib/hiiro/notification.rb', line 60 def has_cmd? system("command -v terminal-notifier &>/dev/null") end |
#options ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/hiiro/notification.rb', line 14 def @options ||= Options.parse(args) do option(:sound, short: :s, default: 'basso', desc: 'sound name') option(:title, short: :t, desc: 'title') option(:message, short: :m, desc: 'message') option(:link, short: :l, desc: 'link to open') option(:command, short: :c, desc: 'command to run') end end |
#play_sound ⇒ Object
50 51 52 53 54 |
# File 'lib/hiiro/notification.rb', line 50 def play_sound if .sound && sounds[.sound] Background.run('afplay', sounds[.sound.downcase]) end end |
#show ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/hiiro/notification.rb', line 37 def show return unless has_cmd? cmd = [binpath] cmd += ['-message', .] if . cmd += ['-title', .title.tr('()[]', '')] if .title cmd += ['-open', .link] if .link cmd += ['-execute', .command] if .command Background.run(*cmd) play_sound end |
#sounds ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/hiiro/notification.rb', line 28 def sounds custom = Dir.glob(File.join(Dir.home, '.config/hiiro/sounds/*')) @sounds ||= custom.each_with_object({}) do |fn, h| basename = File.basename(fn, File.extname(fn)) h[basename.downcase] = fn end end |