Class: Process::Terminal::Device

Inherits:
Object
  • Object
show all
Defined in:
lib/process/terminal/device.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = STDIN) ⇒ Device

Returns a new instance of Device.



41
42
43
# File 'lib/process/terminal/device.rb', line 41

def initialize(io = STDIN)
  @io = io
end

Class Method Details

.new?(io = STDIN) ⇒ Boolean

Create a new device instance, but only if the supplied IO is a TTY.

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/process/terminal/device.rb', line 35

def self.new?(io = STDIN)
  if io.tty?
    self.new(io)
  end
end

.open(path = "/dev/tty", mode = "r+") ⇒ Object

Attempts to open the current TTY. Prefer ‘.new?` if possible.



28
29
30
31
32
# File 'lib/process/terminal/device.rb', line 28

def self.open(path = "/dev/tty", mode = "r+")
  if File.readable?(path)
    self.new(File.open(path, mode))
  end
end

Instance Method Details

#foregroundInteger

Returns the foreground process group.

Returns:

  • (Integer)

    the foreground process group.



60
61
62
63
64
65
66
67
68
# File 'lib/process/terminal/device.rb', line 60

def foreground
  result = Unistd.tcgetpgrp(@io.fileno)
  
  if result == -1
    raise SystemCallError.new('tcgetpgrp', FFI.errno)
  end
  
  return result
end

#foreground=(pid) ⇒ Object

Make the specified pid the foreground processs in the current terminal.

Parameters:

  • pid (Integer)

    the foreground process group.



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/process/terminal/device.rb', line 47

def foreground=(pid)
  current = Signal.trap(:SIGTTOU, :IGNORE)
  
  result = Unistd.tcsetpgrp(@io.fileno, pid)
  
  if result == -1
    raise SystemCallError.new('tcsetpgrp', FFI.errno)
  end
ensure
  Signal.trap(:SIGTTOU, current) if current
end