File: icon_finder.rb

package info (click to toggle)
ruby-libnotify 0.9.4-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 200 kB
  • sloc: ruby: 358; makefile: 4
file content (40 lines) | stat: -rw-r--r-- 787 bytes parent folder | download
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
module Libnotify
  class IconFinder
    def initialize(dirs)
      @dirs = dirs
    end

    def icon_path(name)
      list = @dirs.map do |dir|
        glob = File.join(dir, name)
        Dir[glob].map { |fullpath| Icon.new(fullpath) }
      end
      if found = list.flatten.sort.first
        found.to_s
      end
    end

    class Icon
      attr_reader :fullpath

      def initialize(fullpath)
        @fullpath = fullpath
      end

      ICON_REGEX = /(\d+)x\d+/
      def resolution
        @resolution ||= @fullpath[ICON_REGEX, 1].to_i
      end

      def to_s
        fullpath
      end

      def <=>(other)
        result = other.resolution <=> self.resolution
        result = self.fullpath    <=> other.fullpath if result == 0
        result
      end
    end
  end
end