Sha256: 3da30a82b128dfe818a8fb61ad518d2278cc6ac6b57a7c3d6702b09370815455

Contents?: true

Size: 1.31 KB

Versions: 62

Compression:

Stored size: 1.31 KB

Contents

module Opsicle
  module Monitor
    class Subpanel

      attr_reader :height
      attr_reader :width
      attr_reader :top
      attr_reader :left
      attr_reader :data
      attr_reader :dividers
      attr_reader :content_width

      def initialize(window, height, width, top, left, opts = {})
        @window = window
        @height = height
        @width  = width
        @top    = top
        @left   = left

        @data = {
          :left  => opts[:data_l] || '',
          :right => opts[:data_r] || '',
        }

        @dividers = {
          :left  => opts[:divider_l] || '',
          :right => opts[:divider_r] || '',
        }

        @content_width = @width - @dividers.values.map(&:length).inject(:+)
      end

      def refresh
        new_current = content(@data[:left], @data[:right], @content_width)

        unless new_current == @data[:current] # don't redraw if hasn't changed
          @window.setpos(@top, @left)
          @window.addstr(@dividers[:left] + new_current + @dividers[:right])
        end

        @data[:current] = new_current
      end

      private

      def content(data_l, data_r, width)
        l = data_l.is_a?(Proc) ? data_l.call : data_l
        r = data_r.is_a?(Proc) ? data_r.call : data_r

        ("#{l}%#{width - l.to_s.length}s" % r)[0...width]
      end

    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
opsicle-0.2.1 lib/opsicle/monitor/subpanel.rb
opsicle-0.2.0 lib/opsicle/monitor/subpanel.rb