Class: Multish

Inherits:
Object
  • Object
show all
Defined in:
lib/multish.rb,
lib/multish/version.rb

Constant Summary collapse

VERSION =
'0.1.0'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run!(args) ⇒ Object



91
92
93
# File 'lib/multish.rb', line 91

def self.run!(args)
  self.new.run!(args)
end

Instance Method Details

#run!(args) ⇒ Object



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
# File 'lib/multish.rb', line 95

def run!(args)
  @commands = args.each_with_index.map { |arg, index| MultishItem.new(arg, index, args.count) }
  Curses.init_screen
  Curses.start_color
  Curses.curs_set(0)
  Curses.init_pair(2, Curses::COLOR_WHITE, Curses::COLOR_GREEN)
  Curses.init_pair(3, Curses::COLOR_WHITE, Curses::COLOR_RED)
  Curses.init_pair(4, Curses::COLOR_WHITE, Curses::COLOR_BLACK)
  Curses.use_default_colors
  Curses.cbreak
  @commands.each(&:create_window!)
  @commands.each(&:open_process!)
  fdlist = @commands.flat_map(&:streams)
  begin
    while true
      fdlist.reject!(&:closed?)
      break if fdlist.empty?
      ready = IO.select(fdlist)[0]
      ready.each do |fd|
        @commands.each { |command| command.try_update(fd) }
      end
      @commands.each(&:update_title!)
      break if @commands.all?(&:finished?)
    end
  ensure
    Curses.curs_set(1)
    Curses.close_screen
    exit(@commands.any?(&:errored?) ? 1 : 0)
  end
end