Sha256: 22b0c08b5b92ea864d4acea92f6943eb7f55549e6a70bbc9a7407828825ae90f

Contents?: true

Size: 1.11 KB

Versions: 15

Compression:

Stored size: 1.11 KB

Contents

def fx(str, *args, bg: nil)
  eff = RubyText::Effects.new(*args, bg: bg)
  str.define_singleton_method(:effect) { eff } 
  str  # must return str
end

class RubyText::Effects   # dumb name?
  Modes  = {bold:    X::A_BOLD,
            normal:  X::A_NORMAL,
            reverse: X::A_REVERSE, 
            under:   X::A_UNDERLINE}

  Others = %[:show, :hide]  # show/hide cursor; more later??

  attr_reader :value, :fg, :bg

  # TODO rewrite logic to accommodate color pairs
  
  def initialize(*args, bg: nil)
    bits = 0
    @list = args
    args.each do |arg|
      if Modes.keys.include?(arg)
        val = Modes[arg]
        bits |= val
      elsif ::Colors.include?(arg)
        @fg = arg   # symbol
      end
    end
    @bg = bg || @bg
    @value = bits
  end

  def set(win)
    @old_fg, @old_bg  = win.fg, win.bg  # Save off current state?
    attr, fg, bg = self.value, self.fg, self.bg
    win.cwin.attron(attr)
    fg ||= win.fg
    bg ||= win.bg
    win.set_colors(fg, bg)
  end

  def reset(win)
    attr = self.value
    win.cwin.attroff(attr)
    win.set_colors(@old_fg, @old_bg)  # Does restore logic work?
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
rubytext-0.1.4 lib/effects.rb
rubytext-0.1.3 lib/effects.rb
rubytext-0.1.2 lib/effects.rb
rubytext-0.1.1 lib/effects.rb
rubytext-0.1.0 lib/effects.rb
rubytext-0.0.99 lib/effects.rb
rubytext-0.0.98 lib/effects.rb
rubytext-0.0.97 lib/effects.rb
rubytext-0.0.96 lib/effects.rb
rubytext-0.0.95 lib/effects.rb
rubytext-0.0.94 lib/effects.rb
rubytext-0.0.93 lib/effects.rb
rubytext-0.0.92 lib/effects.rb
rubytext-0.0.91 lib/effects.rb
rubytext-0.0.90 lib/effects.rb