Class: SharedTools::Tools::Notification::LinuxDriver

Inherits:
BaseDriver
  • Object
show all
Defined in:
lib/shared_tools/tools/notification/linux_driver.rb

Overview

Linux notification driver.

notify — uses notify-send (libnotify); logs a warning if no display is available. alert — uses zenity when a display is present; falls back to a terminal prompt. speak — tries espeak-ng first, then espeak.

Instance Method Summary collapse

Instance Method Details

#alert(message:, title: nil, buttons: ['OK'], default_button: nil) ⇒ Hash

Returns includes :button with the label of the clicked button.

Parameters:

  • message (String)
  • title (String, nil) (defaults to: nil)
  • buttons (Array<String>) (defaults to: ['OK'])
  • default_button (String, nil) (defaults to: nil)

Returns:

  • (Hash)

    includes :button with the label of the clicked button



40
41
42
43
44
45
46
# File 'lib/shared_tools/tools/notification/linux_driver.rb', line 40

def alert(message:, title: nil, buttons: ['OK'], default_button: nil)
  if display_available? && command_available?('zenity')
    alert_zenity(message:, title:, buttons:, default_button:)
  else
    alert_terminal(message:, buttons:, default_button:)
  end
end

#notify(message:, title: nil, subtitle: nil, sound: nil) ⇒ Hash

Parameters:

  • message (String)
  • title (String, nil) (defaults to: nil)
  • subtitle (String, nil) (defaults to: nil)

    appended to the message body

  • sound (String, nil) (defaults to: nil)

    ignored on Linux

Returns:

  • (Hash)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/shared_tools/tools/notification/linux_driver.rb', line 19

def notify(message:, title: nil, subtitle: nil, sound: nil)
  unless display_available?
    RubyLLM.logger.warn('NotificationTool: No display server available, cannot show notification')
    return { success: false, error: 'No display server available' }
  end

  unless command_available?('notify-send')
    return { success: false, error: 'notify-send not found. Install libnotify-bin (Debian/Ubuntu) or libnotify (Fedora/Arch).' }
  end

  body = [message, subtitle].compact.join("\n")
  cmd  = ['notify-send', (title || 'Notification'), body]
  _, stderr, status = Open3.capture3(*cmd)
  status.success? ? { success: true, action: 'notify' } : { success: false, error: stderr.strip }
end

#speak(text:, voice: nil, rate: nil) ⇒ Hash

Parameters:

  • text (String)
  • voice (String, nil) (defaults to: nil)

    espeak voice name (e.g. ‘en’, ‘en-us’)

  • rate (Integer, nil) (defaults to: nil)

    words per minute (espeak -s flag)

Returns:

  • (Hash)


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/shared_tools/tools/notification/linux_driver.rb', line 52

def speak(text:, voice: nil, rate: nil)
  binary = espeak_binary
  unless binary
    return { success: false, error: 'espeak-ng or espeak not found. Install espeak-ng (recommended) or espeak.' }
  end

  cmd  = [binary, text]
  cmd += ['-v', voice]    if voice
  cmd += ['-s', rate.to_s] if rate
  _, stderr, status = Open3.capture3(*cmd)
  status.success? ? { success: true, action: 'speak' } : { success: false, error: stderr.strip }
end