Class: Exec
- Inherits:
-
Object
- Object
- Exec
- Defined in:
- lib/exec.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#debug ⇒ Object
Returns the value of attribute debug.
Instance Method Summary collapse
- #gets ⇒ Object
-
#initialize(cmd, **options) ⇒ Exec
constructor
A new instance of Exec.
- #interactive ⇒ Object
- #puts(data) ⇒ Object
- #read(size) ⇒ Object
- #read_until(str) ⇒ Object
- #readpartial(size) ⇒ Object
- #write(data) ⇒ Object
Constructor Details
#initialize(cmd, **options) ⇒ Exec
Returns a new instance of Exec.
8 9 10 11 12 13 14 15 |
# File 'lib/exec.rb', line 8 def initialize(cmd, **) handle_exception @i, @o, s = Open3.popen2(cmd) # Debug msg @debug = (.has_key? :debug) ? [:debug] : true # Log color Rainbow.enabled = false if [:color] == false end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color.
5 6 7 |
# File 'lib/exec.rb', line 5 def color @color end |
#debug ⇒ Object
Returns the value of attribute debug.
5 6 7 |
# File 'lib/exec.rb', line 5 def debug @debug end |
Instance Method Details
#gets ⇒ Object
35 36 37 |
# File 'lib/exec.rb', line 35 def gets read_until "\n" end |
#interactive ⇒ Object
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/exec.rb', line 50 def interactive loop do r = IO.select [@o, $stdin] if r[0].include? @o read 1 elsif r[0].include? $stdin @i.write $stdin.read(1) end end end |
#puts(data) ⇒ Object
31 32 33 |
# File 'lib/exec.rb', line 31 def puts(data) write "#{data}\n" end |
#read(size) ⇒ Object
17 18 19 |
# File 'lib/exec.rb', line 17 def read(size) @o.read(size).tap {|data| write_flush $stdout, data.color(:cyan) if @debug} end |
#read_until(str) ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/exec.rb', line 39 def read_until(str) result = "" loop do result << @o.read(1) if result.end_with? str write_flush $stdout, result.color(:cyan) if @debug return result end end end |
#readpartial(size) ⇒ Object
21 22 23 |
# File 'lib/exec.rb', line 21 def readpartial(size) @o.readpartial(size).tap {|data| write_flush $stdout, data.color(:cyan) if @debug} end |
#write(data) ⇒ Object
25 26 27 28 29 |
# File 'lib/exec.rb', line 25 def write(data) data = data.to_s if data.is_a? Integer write_flush $stdout, data.color(:yellow) if @debug write_flush @i, data end |