Class: Multish

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

Constant Summary collapse

VERSION =
'0.2.0'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.run!(args) ⇒ Object



104
105
106
# File 'lib/multish.rb', line 104

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

Instance Method Details

#errored?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/multish.rb', line 108

def errored?
  @commands.any?(&:errored?)
end

#run!(args) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/multish.rb', line 112

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
    if errored?
      warn 'At least one of the commands exited with error.'
      @commands.select(&:errored?).each do |command|
        command.print_output!
      end
      exit 1
    else
      exit 0
    end
  end
end