class Process::Terminal::Device

Public Class Methods

new(io = STDIN) click to toggle source
# File lib/process/terminal/device.rb, line 41
def initialize(io = STDIN)
        @io = io
end
new?(io = STDIN) click to toggle source

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

# 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+") click to toggle source

Attempts to open the current TTY. Prefer `.new?` if possible. @returns [Device | nil] the terminal device if the file was readable.

# 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

Public Instance Methods

foreground() click to toggle source

@return [Integer] the foreground process group.

# 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) click to toggle source

Make the specified pid the foreground processs in the current terminal. @param pid [Integer] the foreground process group.

# 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