Class: Hiiro::Notification

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#hiiroObject (readonly)

Returns the value of attribute hiiro.



7
8
9
# File 'lib/hiiro/notification.rb', line 7

def hiiro
  @hiiro
end

#original_argsObject (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

#argsObject



24
25
26
# File 'lib/hiiro/notification.rb', line 24

def args
  @args ||= hiiro.args
end

#binpathObject



56
57
58
# File 'lib/hiiro/notification.rb', line 56

def binpath
  `command -v terminal-notifier`
end

#has_cmd?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/hiiro/notification.rb', line 60

def has_cmd?
  system("command -v terminal-notifier &>/dev/null")
end

#optionsObject



14
15
16
17
18
19
20
21
22
# File 'lib/hiiro/notification.rb', line 14

def options
  @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_soundObject



50
51
52
53
54
# File 'lib/hiiro/notification.rb', line 50

def play_sound
  if options.sound && sounds[options.sound]
    Background.run('afplay', sounds[options.sound.downcase])
  end
end

#showObject



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', options.message] if options.message
  cmd += ['-title', options.title.tr('()[]', '')] if options.title
  cmd += ['-open', options.link] if options.link
  cmd += ['-execute', options.command] if options.command
  Background.run(*cmd)

  play_sound
end

#soundsObject



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