Class: OpenHAB::Console::Stdin
- Inherits:
-
Stdio
- Object
- Stdio
- OpenHAB::Console::Stdin
show all
- Defined in:
- lib/openhab/console/stdio.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Stdio
#inspect, #set_encoding, #tty?, #winsize
Constructor Details
#initialize(terminal) ⇒ Stdin
Returns a new instance of Stdin.
37
38
39
40
41
42
|
# File 'lib/openhab/console/stdio.rb', line 37
def initialize(terminal)
super
@byte_stream = terminal.input
@buffer = StringIO.new.set_encoding(external_encoding)
end
|
Instance Attribute Details
#external_encoding ⇒ Object
Returns the value of attribute external_encoding.
35
36
37
|
# File 'lib/openhab/console/stdio.rb', line 35
def external_encoding
@external_encoding
end
|
#internal_encoding ⇒ Object
Returns the value of attribute internal_encoding.
35
36
37
|
# File 'lib/openhab/console/stdio.rb', line 35
def internal_encoding
@internal_encoding
end
|
Instance Method Details
#getbyte ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/openhab/console/stdio.rb', line 44
def getbyte
unless @buffer.eof?
b = @buffer.getbyte
@buffer.truncate(0) if @buffer.eof?
return b
end
b = @byte_stream.read
return nil if b.negative?
b
end
|
#getc ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/openhab/console/stdio.rb', line 57
def getc
unless @buffer.eof?
c = @buffer.getc
@buffer.truncate(0) if @buffer.eof?
return c
end
bytes = (+"").force_encoding(Encoding::BINARY)
loop do
b = getbyte
return nil if b.nil?
bytes << b.chr
c = bytes.encode(external_encoding,
internal_encoding || Encoding.default_internal,
invalid: :replace,
undef: :replace,
replace: "")
return c unless c.empty?
end
rescue java.io.InterruptedIOException
raise Interrupt
end
|
#gets ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/openhab/console/stdio.rb', line 88
def gets
result = +""
loop do
c = getc
if c.nil?
return nil if result.empty?
break
end
if c == "\x04" && result.empty? return nil
elsif c == "\x7f"
result.slice(0...-1)
else
result << c
end
break if c == "\n"
end
result
rescue java.io.InterruptedIOException
raise Interrupt
end
|
#raw ⇒ Object
145
146
147
148
149
150
|
# File 'lib/openhab/console/stdio.rb', line 145
def raw(*)
previous_attributes = @terminal.enter_raw_mode
yield self
ensure
@terminal.set_attributes(previous_attributes)
end
|
#read(bytes) ⇒ Object
112
113
114
115
116
|
# File 'lib/openhab/console/stdio.rb', line 112
def read(bytes)
r = readpartial(bytes)
r.concat(readpartial(bytes - r.bytesize)) while r.bytesize < bytes
r
end
|
#readpartial(bytes) ⇒ Object
Also known as:
read_nonblock
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/openhab/console/stdio.rb', line 118
def readpartial(bytes)
available = @buffer.size - @buffer.tell
if available.positive?
bytes = available if available < bytes
r = @buffer.read(bytes)
@buffer.truncate(0) if @buffer.eof?
return r
end
buffer = Java::byte[bytes].new
read = @byte_stream.read_buffered(buffer)
buffer = buffer[0..read] if read != bytes
String.from_java_bytes(buffer)
end
|
#ungetbyte(byte) ⇒ Object
80
81
82
|
# File 'lib/openhab/console/stdio.rb', line 80
def ungetbyte(byte)
@buffer.ungetbyte(byte)
end
|
#ungetc(char) ⇒ Object
84
85
86
|
# File 'lib/openhab/console/stdio.rb', line 84
def ungetc(char)
@buffer.ungetc(char)
end
|
#wait_readable(timeout = nil) ⇒ Object
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/openhab/console/stdio.rb', line 134
def wait_readable(timeout = nil)
return true if (@buffer.size - @buffer.tell).positive?
timeout = timeout ? 0 : timeout * 1000
char = @byte_stream.read(timeout)
return nil if char.negative?
ungetc(char.chr(external_encoding))
self
end
|