Class: Mux

Inherits:
Object
  • Object
show all
Defined in:
lib/mux.rb

Defined Under Namespace

Classes: Options

Class Method Summary collapse

Class Method Details

.open_streams(targets, type) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/mux.rb', line 59

def self.open_streams(targets, type)
  handles = []
  targets.each do |target|
    case type
      when 'pipe'
        handles << IO.popen(target, 'w')
      when 'funnel'
        handles << File.open(target, 'w')
      when 'append'
        handles << File.open(target, 'a')
    end
  end
  handles
end

.run(args) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/mux.rb', line 34

def self.run(args)
  options = Mux::Options.parse(args)
  
  streams = []
  
  if options.to_stdout
    streams << $stdout
  end
  
  streams.concat(open_streams(options.pipes, 'pipe'))
  streams.concat(open_streams(options.funnels, 'funnel'))
  streams.concat(open_streams(options.appends, 'append'))

  while buffer = $stdin.gets
    streams.each do |stream|
      stream.puts buffer
    end
  end

  streams.each do |stream|
    stream.close
  end
  
end