Class: RETerm::Components::CmdOutput

Inherits:
ScrollingArea show all
Defined in:
lib/reterm/components/cmd_output.rb

Overview

Run command, capture and display output

Direct Known Subclasses

YouTube

Constant Summary

Constants included from LogHelpers

LogHelpers::LOG_FILE

Instance Attribute Summary

Attributes inherited from ScrollingArea

#lines, #title

Attributes inherited from RETerm::Component

#activatable, #activate_focus, #highlight_focus, #window

Instance Method Summary collapse

Methods inherited from ScrollingArea

#<<, #activatable?, #requested_cols, #requested_rows

Methods included from RETerm::CDKComponent

#_component, #activatable?, #activate!, #bind_key, #cdk?, #colors=, #component, #deactivate!, #draw!, #early_exit?, #erase, #escape_hit?, #init?, #init_cdk, #normal_exit?, #strip_formatting, #title_attrib=, #value

Methods inherited from RETerm::Component

#activatable?, #activate!, #activate_focus?, #cdk?, #colored?, #colors=, #deactivate!, #deactivate?, #distance_from, #extra_padding, #highlight_focus?, #reactivate!, #requested_cols, #requested_rows, #resize, #sync_getch

Methods included from KeyBindings

#bind_key, #invoke_key_bindings, #key_bindings, #key_bound?

Methods included from LogHelpers

#logger

Methods included from EventDispatcher

#dispatch, #handle

Constructor Details

#initialize(args = {}) ⇒ CmdOutput

Initialize the CmdOutput component

Parameters:

  • args (Hash) (defaults to: {})

    cmd output params

Options Hash (args):

  • :cmd (String)

    command to run



12
13
14
15
16
17
# File 'lib/reterm/components/cmd_output.rb', line 12

def initialize(args={})
  activate_sync!

  super
  @cmd = args[:cmd]
end

Instance Method Details

#finalize!Object



32
33
34
35
36
37
38
# File 'lib/reterm/components/cmd_output.rb', line 32

def finalize!
  if running?
    Process.kill @pid
    @stdout.close
    @stdout.close
  end
end

#running?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/reterm/components/cmd_output.rb', line 19

def running?
  !!@pid && process_alive?(@pid)
end

#start!Object



40
41
42
43
# File 'lib/reterm/components/cmd_output.rb', line 40

def start!
  @stdout, @stdin, @pid = PTY.spawn(@cmd)
  self
end

#sync!Object



23
24
25
26
27
28
29
30
# File 'lib/reterm/components/cmd_output.rb', line 23

def sync!
  out = nil
  begin
    out = @stdout.gets
  rescue Errno::EIO
  end
  self << out if out
end

#waitObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/reterm/components/cmd_output.rb', line 45

def wait
  while running?
    begin
      Timeout.timeout(SYNC_TIMEOUT.to_f/1000) do
        begin
          Process.waitpid @pid
        rescue
          # rescue needed incase process exit between
          # running? check and waitpid
        end
      end
    rescue Timeout::Error
      run_sync!
    end
  end

  # NOTE: at this point process will be closed but
  # there may still be unread data in stdout/stderr
  # buffers!!!

  self
end