Module: LightIO::Library::KernelExt

Extended by:
Forwardable
Defined in:
lib/lightio/library/kernel_ext.rb

Constant Summary collapse

KERNEL_PROXY =
::LightIO::RawProxy.new(::Kernel,
methods: [:spawn, :`])

Instance Method Summary collapse

Instance Method Details

#`(cmd) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/lightio/library/kernel_ext.rb', line 40

def `(cmd)
  Open3.popen3(cmd, out: STDOUT, err: STDERR) do |stdin, stdout, stderr, wait_thr|
    output = LightIO::Library::IO._wrap(stdout).read
    KERNEL_PROXY.send(:`, "exit #{wait_thr.value.exitstatus}")
    return output
  end
end

#sleep(*duration) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/lightio/library/kernel_ext.rb', line 10

def sleep(*duration)
  if duration.size > 1
    raise ArgumentError, "wrong number of arguments (given #{duration.size}, expected 0..1)"
  elsif duration.size == 0
    LightIO::IOloop.current.transfer
  end
  duration = duration[0]
  if duration.zero?
    LightIO::Beam.pass
    return
  end
  timer = LightIO::Watchers::Timer.new duration
  LightIO::IOloop.current.wait(timer)
end

#spawn(*commands, **options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lightio/library/kernel_ext.rb', line 25

def spawn(*commands, **options)
  options = options.dup
  options.each do |key, v|
    if key.is_a?(LightIO::Library::IO)
      options.delete(key)
      key = convert_io_or_array_to_raw(key)
      options[key] = v
    end
    if (io = convert_io_or_array_to_raw(v))
      options[key] = io
    end
  end
  KERNEL_PROXY.send(:spawn, *commands, **options)
end

#system(*cmd, **opt) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/lightio/library/kernel_ext.rb', line 48

def system(*cmd, **opt)
  Open3.popen3(*cmd, **opt) do |stdin, stdout, stderr, wait_thr|
    return nil if LightIO::Library::IO._wrap(stderr).read.size > 0
    return wait_thr.value.exitstatus == 0
  end
rescue Errno::ENOENT
  nil
end