class Cigarette

Constants

CANCER_DO_NOT_APPEAR

Hope.

VERSION

Public Class Methods

new() click to toggle source
# File lib/cigarette.rb, line 14
def initialize
  begin
    cnf = YAML::load(File.open("#{Dir.pwd}/.cigarette"))
    unless cnf["rvm"].nil?
      @rubies = cnf["rvm"]
      raise "rvm error in yaml. You have to put a list. (-)" unless @rubies.is_a? Array
    else
      RVM.use_system!
      @rubies = [RUBY_VERSION]
    end
    @time = cnf['each'].to_i
    @next_check = Time.now.to_i + @time
    @command = cnf['command']
  rescue NoMethodError
    abort "Problem during .cigarette loading: 'each:' and 'command:' attributes are MANDATORY."
  rescue TypeError
    abort "Didn't you make a mistake in .cigarette file ?"
  rescue ArgumentError
    abort "Did you configure attribute like this: 'attribute: <value>'."
  rescue Exception => e
    abort "Problem during .cigarette loading: #{e.message}."
  end
  deploy_trap
  init_curses
  roll_baby_roll!
  lighter_please!
end

Private Instance Methods

dec_pos() click to toggle source
# File lib/cigarette.rb, line 174
def dec_pos
  if @pos == 0
    @pos = @rubies.length - 1
  else
    @pos -= 1
  end
end
deploy_trap() click to toggle source
# File lib/cigarette.rb, line 121
def deploy_trap
  for i in 1..15  # SIGHUP .. SIGTERM
    if trap(i, "SIG_IGN") != 0 then  # 0 for SIG_IGN
      trap(i) { |sig| onsig(sig) }
    end
  end
end
display(line, column, text, color = DEFAULT) click to toggle source
# File lib/cigarette.rb, line 129
def display(line, column, text, color = DEFAULT)
  Curses.setpos(line, column)
  Curses.attron(Curses.color_pair(color | DEFAULT)) { Curses.addstr(text) }
end
display_main_screen(status, output, time, color = DEFAULT) click to toggle source
# File lib/cigarette.rb, line 44
def display_main_screen(status, output, time, color = DEFAULT)
  Curses.clear
  display_menu
  display(0,0, "cigarette - Version #{VERSION}")
  display(4, 8, status, color)
  padding_for_time = 8 + status.length
  display(4, padding_for_time," - #{time.strftime("%T")}")
  display(4, 0, "STATUS:")
  display(6, 0, output)
  Curses.refresh
end
display_menu() click to toggle source
# File lib/cigarette.rb, line 134
def display_menu
  padding = 0
  @rubies.each { |ruby|
    if !@outputs[ruby.to_s].nil?
      color =  @outputs[ruby.to_s][:color]
    else
      color = RED
    end
    color = MAGENTA if ruby.eql?(@current_rb) && @rubies.length > 1
    display(2, padding, ruby, color)
    padding += 8
  }
end
inc_pos() click to toggle source
# File lib/cigarette.rb, line 161
def inc_pos
  if @pos == @rubies.length - 1
    @pos = 0
  else
    @pos += 1
  end
end
init_curses() click to toggle source
# File lib/cigarette.rb, line 108
def init_curses
  Curses.noecho # do not show typed keys
  Curses.init_screen
  Curses.stdscr.keypad(true) # enable arrow keys
  Curses.start_color
  Curses.init_pair(Colors::RED, RED, BLACK)
  Curses.init_pair(GREEN, GREEN, BLACK)
  Curses.init_pair(MAGENTA, MAGENTA, WHITE)
  Curses.init_pair(CYAN, CYAN, BLACK)
  display(0,0, "WAIT, INITIALIZATION...")
  Curses.refresh
end
lighter_please!() click to toggle source
# File lib/cigarette.rb, line 56
def lighter_please!
  #initial position
  @pos = 0
  while CANCER_DO_NOT_APPEAR
    sleep 0.1
    case Curses.getch
      when Curses::Key::RIGHT then move_right
      when Curses::Key::LEFT then move_left
      when ?r then rebuild
      when ?q then onsig
    end
  end
end
move() click to toggle source
# File lib/cigarette.rb, line 155
def move
  @current_rb = @rubies[@pos]
  out = @outputs[@rubies[@pos]]
  display_main_screen(out[:status], out[:output], out[:time], out[:color])
end
move_left() click to toggle source
# File lib/cigarette.rb, line 182
def move_left
  dec_pos
  move
end
move_right() click to toggle source
# File lib/cigarette.rb, line 169
def move_right
  inc_pos
  move
end
onsig(sig = nil) click to toggle source
# File lib/cigarette.rb, line 187
def onsig(sig = nil)
  Curses.close_screen
  @t_array.each { |t| Thread.kill(t) } unless @t_array.nil?
  !sig.nil? ? exit(sig) : exit(0)
end
rebuild() click to toggle source
# File lib/cigarette.rb, line 148
def rebuild
  @current_rb = @rubies[@pos]
  @outputs[@current_rb] = RVM.run(@current_rb) { @command }
  out = @outputs[@current_rb]
  display_main_screen(out[:status], out[:output], out[:time], out[:color])
end
roll_baby_roll!() click to toggle source
# File lib/cigarette.rb, line 70
def roll_baby_roll!
  @current_rb = @rubies.first
  @t_array = []
  @outputs = {}
  @rubies.each { |ruby|
    @t_array << Thread.new {
      # Run test(s) at cigarette start
      next_check = 0
      while CANCER_DO_NOT_APPEAR
        time = Time.now.to_i
        if time >= next_check
          @outputs[ruby.to_s] = RVM.run(ruby) { @command }
          next_check = time + @time
          if @current_rb.eql?(ruby)
            out = @outputs[ruby.to_s]
            display_main_screen(out[:status], out[:output],
                                out[:time], out[:color])
          else
            display_menu
            Curses.refresh
          end
        end
        sleep 0.1
      end
    }
  }
end
time_to_light_up?() click to toggle source
# File lib/cigarette.rb, line 98
def time_to_light_up?
  @current_time = Time.now
  if @current_time.to_i >= @next_check
    @next_check = Time.now.to_i + @time
    true
  else
    false
  end
end