File: tmux.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 (132 lines) | stat: -rw-r--r-- 4,320 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
require "notiffany/notifier/base"

require "notiffany/notifier/tmux/client"
require "notiffany/notifier/tmux/session"
require "notiffany/notifier/tmux/notification"

# TODO: this probably deserves a gem of it's own
module Notiffany
  class Notifier
    # Changes the color of the Tmux status bar and optionally
    # shows messages in the status bar.
    class Tmux < Base
      @session = nil

      DEFAULTS = {
        tmux_environment:       "TMUX",
        success:                "green",
        failed:                 "red",
        pending:                "yellow",
        default:                "green",
        timeout:                5,
        display_message:        false,
        default_message_format: "%s - %s",
        default_message_color:  "white",
        display_on_all_clients: false,
        display_title:          false,
        default_title_format:   "%s - %s",
        line_separator:         " - ",
        change_color:           true,
        color_location:         "status-left-bg"
      }

      class Error < RuntimeError
      end

      ERROR_NOT_INSIDE_TMUX = ":tmux notifier is only available inside a "\
        "TMux session."

      ERROR_ANCIENT_TMUX = "Your tmux version is way too old (%s)!"

      # Notification starting, save the current Tmux settings
      # and quiet the Tmux output.
      #
      def turn_on
        self.class._start_session
      end

      # Notification stopping. Restore the previous Tmux state
      # if available (existing options are restored, new options
      # are unset) and unquiet the Tmux output.
      #
      def turn_off
        self.class._end_session
      end

      private

      def _gem_name
        nil
      end

      def _check_available(opts = {})
        @session ||= nil # to avoid unitialized error
        fail "PREVIOUS TMUX SESSION NOT CLEARED!" if @session

        var_name = opts[:tmux_environment]
        fail Error, ERROR_NOT_INSIDE_TMUX unless ENV.key?(var_name)

        version = Client.version
        fail Error, format(ERROR_ANCIENT_TMUX, version) if version < 1.7

        true
      rescue Error => e
        fail UnavailableError, e.message
      end

      # Shows a system notification.
      #
      # By default, the Tmux notifier only makes
      # use of a color based notification, changing the background color of the
      # `color_location` to the color defined in either the `success`,
      # `failed`, `pending` or `default`, depending on the notification type.
      #
      # You may enable an extra explicit message by setting `display_message`
      # to true, and may further disable the colorization by setting
      # `change_color` to false.
      #
      # @param [String] message the notification message
      # @param [Hash] options additional notification library options
      # @option options [String] type the notification type. Either 'success',
      #   'pending', 'failed' or 'notify'
      # @option options [String] message the notification message body
      # @option options [String] image the path to the notification image
      # @option options [Boolean] change_color whether to show a color
      #   notification
      # @option options [String,Array] color_location the location where to draw
      #   the color notification
      # @option options [Boolean] display_message whether to display a message
      #   or not
      # @option options [Boolean] display_on_all_clients whether to display a
      #   message on all tmux clients or not
      #
      def _perform_notify(message, options = {})
        locations = Array(options[:color_location])
        type  = options[:type].to_s
        title = options[:title]

        tmux = Notification.new(type, options)
        tmux.colorize(locations) if options[:change_color]
        tmux.display_title(title, message) if options[:display_title]
        tmux.display_message(title, message) if options[:display_message]
      end

      class << self
        def _start_session
          fail "Already turned on!" if @session
          @session = Session.new
        end

        def _end_session
          fail "Already turned off!" unless @session
          @session.close
          @session = nil
        end

        def _session
          @session
        end
      end
    end
  end
end