Class: Process::Terminal::Device
- Inherits:
-
Object
- Object
- Process::Terminal::Device
- Defined in:
- lib/process/terminal/device.rb
Class Method Summary collapse
-
.new?(io = STDIN) ⇒ Boolean
Create a new device instance, but only if the supplied IO is a TTY.
-
.open(path = "/dev/tty", mode = "r+") ⇒ Object
Attempts to open the current TTY.
Instance Method Summary collapse
-
#foreground ⇒ Integer
The foreground process group.
-
#foreground=(pid) ⇒ Object
Make the specified pid the foreground processs in the current terminal.
-
#initialize(io = STDIN) ⇒ Device
constructor
A new instance of Device.
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.
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
#foreground ⇒ Integer
Returns 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.
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 |