io-console 0.7.0-java → 0.7.2-java

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 04e00fc2882d045d482efb00903a0546b330a73d95893e92667be4de4cd6d6cb
4
- data.tar.gz: 18da261f918fdd309e6bc61746befd93b86e60733803c7f35957ff5b2d3babee
3
+ metadata.gz: a2f353bc5f4bdde430dd0c5166013af6b6601a2c2b25c60de3ec1b1a432934a5
4
+ data.tar.gz: 7b5a900fc2f6b2c5ba507b01dc89cc394a45b05fbf02d70d3cfff0312ee25b5c
5
5
  SHA512:
6
- metadata.gz: 1f04f40af8db7d89dc11cc16a6cd8be87c70bcfc62aef2c5728cbc1e820d9076fa741d345f22dc80370f609970279d3ab28a560ec394830937fb7a9e5584df9a
7
- data.tar.gz: 95b66a4722edd2e29a5e102857738e610ce848e3e7bbf9f6614079b357726f70e510d606cb6dac10d48a9febf945ab414607bbf99cd40e4886f2e607306a4f14
6
+ metadata.gz: 83d234297209f93e79b5c879e90575aadbbd6782a7b69d198426c427d6cf1feef7f3266aa401cb85cdd0480e4756dc037ef0701b0c0c3776b65e5eacfea96071
7
+ data.tar.gz: fc04c9b4870c50005897f0478e1294624ac0b2fc5ae63d9b4a5bf4e1bc11f48af9b58c12d75ed7b54caf8af8ba8966cb50ed1ed322d31f90fbe1c60136d03aae
data/.document CHANGED
@@ -1,4 +1,5 @@
1
1
  LICENSE.txt
2
2
  README.md
3
+ docs/
3
4
  ext/
4
5
  lib/io/console/size.rb
@@ -1,5 +1,11 @@
1
1
  require 'ffi'
2
2
 
3
+ tested_platforms = %w[i386 x86_64]
4
+
5
+ if RbConfig::CONFIG['host_os'].downcase =~ /darwin/ && FFI::Platform::ARCH !~ /#{tested_platforms.join('|')}/
6
+ raise LoadError.new("native console on MacOS only supported on #{tested_platforms.join(', ')}")
7
+ end
8
+
3
9
  module IO::LibC
4
10
  extend FFI::Library
5
11
  ffi_lib FFI::Library::LIBC
@@ -1,5 +1,47 @@
1
1
  # Methods common to all backend impls
2
2
  class IO
3
+ # TODO: Windows version uses "conin$" and "conout$" instead of /dev/tty
4
+ def self.console(sym = nil, *args)
5
+ raise TypeError, "expected Symbol, got #{sym.class}" unless sym.nil? || sym.kind_of?(Symbol)
6
+
7
+ # klass = self == IO ? File : self
8
+ if defined?(@console) # using ivar instead of hidden const as in MRI
9
+ con = @console
10
+ # MRI checks IO internals : (!RB_TYPE_P(con, T_FILE) || (!(fptr = RFILE(con)->fptr) || GetReadFD(fptr) == -1))
11
+ if !con.kind_of?(File) || (con.kind_of?(IO) && (con.closed? || !FileTest.readable?(con)))
12
+ remove_instance_variable :@console
13
+ con = nil
14
+ end
15
+ end
16
+
17
+ if sym
18
+ if sym == :close
19
+ if con
20
+ con.close
21
+ remove_instance_variable :@console if defined?(@console)
22
+ end
23
+ return nil
24
+ end
25
+ end
26
+
27
+ if !con
28
+ if $stdin.isatty && $stdout.isatty
29
+ begin
30
+ con = File.open('/dev/tty', 'r+')
31
+ rescue
32
+ return nil
33
+ end
34
+
35
+ con.sync = true
36
+ end
37
+
38
+ @console = con
39
+ end
40
+
41
+ return con.send(sym, *args) if sym
42
+ return con
43
+ end
44
+
3
45
  def getch(*, **opts)
4
46
  raw(**opts) do
5
47
  getc
@@ -20,6 +62,80 @@ class IO
20
62
  str.chomp
21
63
  end
22
64
 
65
+ def cursor
66
+ raw do
67
+ syswrite "\e[6n"
68
+
69
+ return nil if getbyte != 0x1b
70
+ return nil if getbyte != ?[.ord
71
+
72
+ num = 0
73
+ result = []
74
+
75
+ while b = getbyte
76
+ c = b.to_i
77
+ if c == ?;.ord
78
+ result.push num
79
+ num = 0
80
+ elsif c >= ?0.ord && c <= ?9.ord
81
+ num = num * 10 + c - ?0.ord
82
+ #elsif opt && c == opt
83
+ else
84
+ last = c
85
+ result.push num
86
+ b = last.chr
87
+ return nil unless b == ?R
88
+ break
89
+ end
90
+ end
91
+
92
+ result.map(&:pred)
93
+ end
94
+ end
95
+
96
+ def cursor=(pos)
97
+ pos = pos.to_ary if !pos.kind_of?(Array)
98
+
99
+ raise "expected 2D coordinates" unless pos.size == 2
100
+
101
+ x, y = pos
102
+ syswrite(format("\x1b[%d;%dH", x + 1, y + 1))
103
+
104
+ self
105
+ end
106
+
107
+ def cursor_down(n)
108
+ raw do
109
+ syswrite "\x1b[#{n}B"
110
+ end
111
+
112
+ self
113
+ end
114
+
115
+ def cursor_right(n)
116
+ raw do
117
+ syswrite "\x1b[#{n}C"
118
+ end
119
+
120
+ self
121
+ end
122
+
123
+ def cursor_left(n)
124
+ raw do
125
+ syswrite "\x1b[#{n}D"
126
+ end
127
+
128
+ self
129
+ end
130
+
131
+ def cursor_up(n)
132
+ raw do
133
+ syswrite "\x1b[#{n}A"
134
+ end
135
+
136
+ self
137
+ end
138
+
23
139
  module GenericReadable
24
140
  def getch(*)
25
141
  getc
@@ -1,7 +1,9 @@
1
1
  require 'ffi'
2
2
 
3
- unless FFI::Platform::ARCH =~ /i386|x86_64|powerpc64|aarch64|s390x/
4
- raise LoadError.new("native console only supported on i386, x86_64, powerpc64, aarch64 and s390x")
3
+ tested_platforms = %w[i386 x86_64 powerpc64 aarch64 s390x]
4
+
5
+ unless FFI::Platform::ARCH =~ /#{tested_platforms.join('|')}/
6
+ warn "native console only tested on #{tested_platforms.join(', ')}"
5
7
  end
6
8
 
7
9
  module IO::LibC
@@ -52,8 +52,8 @@ class IO
52
52
  end
53
53
  end
54
54
 
55
- def raw(*, **kwargs, &block)
56
- ttymode_yield(block, **kwargs, &TTY_RAW)
55
+ def raw(*, min: 1, time: nil, intr: nil, &block)
56
+ ttymode_yield(block, min:, time:, intr:, &TTY_RAW)
57
57
  end
58
58
 
59
59
  def raw!(*)
@@ -137,117 +137,4 @@ class IO
137
137
  def ioflush
138
138
  raise SystemCallError.new("tcflush(TCIOFLUSH)", FFI.errno) unless LibC.tcflush(self.fileno, LibC::TCIOFLUSH) == 0
139
139
  end
140
-
141
- def cursor
142
- raw do
143
- syswrite "\e[6n"
144
-
145
- return nil if getbyte != 0x1b
146
- return nil if getbyte != ?[.ord
147
-
148
- num = 0
149
- result = []
150
-
151
- while b = getbyte
152
- c = b.to_i
153
- if c == ?;.ord
154
- result.push num
155
- num = 0
156
- elsif c >= ?0.ord && c <= ?9.ord
157
- num = num * 10 + c - ?0.ord
158
- #elsif opt && c == opt
159
- else
160
- last = c
161
- result.push num
162
- b = last.chr
163
- return nil unless b == ?R
164
- break
165
- end
166
- end
167
-
168
- result.map(&:pred)
169
- end
170
- end
171
-
172
- def cursor=(pos)
173
- pos = pos.to_ary if !pos.kind_of?(Array)
174
-
175
- raise "expected 2D coordinates" unless pos.size == 2
176
-
177
- x, y = pos
178
- syswrite(format("\x1b[%d;%dH", x + 1, y + 1))
179
-
180
- self
181
- end
182
-
183
- def cursor_down(n)
184
- raw do
185
- syswrite "\x1b[#{n}B"
186
- end
187
-
188
- self
189
- end
190
-
191
- def cursor_right(n)
192
- raw do
193
- syswrite "\x1b[#{n}C"
194
- end
195
-
196
- self
197
- end
198
-
199
- def cursor_left(n)
200
- raw do
201
- syswrite "\x1b[#{n}D"
202
- end
203
-
204
- self
205
- end
206
-
207
- def cursor_up(n)
208
- raw do
209
- syswrite "\x1b[#{n}A"
210
- end
211
-
212
- self
213
- end
214
-
215
- # TODO: Windows version uses "conin$" and "conout$" instead of /dev/tty
216
- def self.console(sym = nil, *args)
217
- raise TypeError, "expected Symbol, got #{sym.class}" unless sym.nil? || sym.kind_of?(Symbol)
218
-
219
- # klass = self == IO ? File : self
220
- if defined?(@console) # using ivar instead of hidden const as in MRI
221
- con = @console
222
- # MRI checks IO internals : (!RB_TYPE_P(con, T_FILE) || (!(fptr = RFILE(con)->fptr) || GetReadFD(fptr) == -1))
223
- if !con.kind_of?(File) || (con.kind_of?(IO) && (con.closed? || !FileTest.readable?(con)))
224
- remove_instance_variable :@console
225
- con = nil
226
- end
227
- end
228
-
229
- if sym
230
- if sym == :close
231
- if con
232
- con.close
233
- remove_instance_variable :@console if defined?(@console)
234
- end
235
- return nil
236
- end
237
- end
238
-
239
- if !con
240
- begin
241
- con = File.open('/dev/tty', 'r+')
242
- rescue
243
- return nil
244
- end
245
-
246
- con.sync = true
247
- @console = con
248
- end
249
-
250
- return con.send(sym, *args) if sym
251
- return con
252
- end
253
140
  end
@@ -4,26 +4,39 @@ if $?.exitstatus != 0
4
4
  raise "stty command returned nonzero exit status"
5
5
  end
6
6
 
7
- warn "io/console on JRuby shells out to stty for most operations"
7
+ warn "io/console on JRuby shells out to stty for most operations" if $VERBOSE
8
8
 
9
9
  # Non-Windows assumes stty command is available
10
10
  class IO
11
11
  if RbConfig::CONFIG['host_os'].downcase =~ /linux/ && File.exists?("/proc/#{Process.pid}/fd")
12
- def stty(*args)
13
- `stty #{args.join(' ')} < /proc/#{Process.pid}/fd/#{fileno}`
12
+ protected def _io_console_stty(*args)
13
+ _io_console_stty_error { `stty #{args.join(' ')} < /proc/#{Process.pid}/fd/#{fileno}` }
14
14
  end
15
15
  else
16
- def stty(*args)
17
- `stty #{args.join(' ')}`
16
+ protected def _io_console_stty(*args)
17
+ _io_console_stty_error { `stty #{args.join(' ')}` }
18
18
  end
19
19
  end
20
20
 
21
- def raw(*)
22
- saved = stty('-g')
23
- stty('raw')
21
+ protected def _io_console_stty_error
22
+ # pre-check to catch non-tty filenos we can't stty against anyway
23
+ raise Errno::ENOTTY, inspect if !tty?
24
+
25
+ result = yield
26
+
27
+ case result
28
+ when /Inappropriate ioctl for device/
29
+ raise Errno.ENOTTY, inspect
30
+ end
31
+
32
+ result
33
+ end
34
+
35
+ def raw(*, min: 1, time: nil, intr: nil)
36
+ saved = _io_console_stty('-g raw')
24
37
  yield self
25
38
  ensure
26
- stty(saved)
39
+ _io_console_stty(saved)
27
40
  end
28
41
 
29
42
  def raw!(*)
@@ -31,31 +44,29 @@ class IO
31
44
  end
32
45
 
33
46
  def cooked(*)
34
- saved = stty('-g')
35
- stty('-raw')
47
+ saved = _io_console_stty('-g', '-raw')
36
48
  yield self
37
49
  ensure
38
- stty(saved)
50
+ _io_console_stty(saved)
39
51
  end
40
52
 
41
53
  def cooked!(*)
42
- stty('-raw')
54
+ _io_console_stty('-raw')
43
55
  end
44
56
 
45
57
  def echo=(echo)
46
- stty(echo ? 'echo' : '-echo')
58
+ _io_console_stty(echo ? 'echo' : '-echo')
47
59
  end
48
60
 
49
61
  def echo?
50
- (stty('-a') =~ / -echo /) ? false : true
62
+ (_io_console_stty('-a') =~ / -echo /) ? false : true
51
63
  end
52
64
 
53
65
  def noecho
54
- saved = stty('-g')
55
- stty('-echo')
66
+ saved = _io_console_stty('-g', '-echo')
56
67
  yield self
57
68
  ensure
58
- stty(saved)
69
+ _io_console_stty(saved)
59
70
  end
60
71
 
61
72
  # Not all systems return same format of stty -a output
@@ -63,7 +74,7 @@ class IO
63
74
  UBUNTU = 'rows (?<rows>\d+); columns (?<columns>\d+)'
64
75
 
65
76
  def winsize
66
- match = stty('-a').match(/#{IEEE_STD_1003_2}|#{UBUNTU}/)
77
+ match = _io_console_stty('-a').match(/#{IEEE_STD_1003_2}|#{UBUNTU}/)
67
78
  [match[:rows].to_i, match[:columns].to_i]
68
79
  end
69
80
 
@@ -75,13 +86,13 @@ class IO
75
86
  raise ArgumentError.new("wrong number of arguments (given #{sizelen}, expected 2 or 4)")
76
87
  end
77
88
 
78
- row, col, xpixel, ypixel = size
79
-
80
89
  if sizelen == 4
81
90
  warn "stty io/console does not support pixel winsize"
82
91
  end
83
92
 
84
- stty("rows #{row} cols #{col}")
93
+ row, col, _, _ = size
94
+
95
+ _io_console_stty("rows #{row} cols #{col}")
85
96
  end
86
97
 
87
98
  def iflush
@@ -2,7 +2,7 @@ warn "io/console not supported; tty will not be manipulated" if $VERBOSE
2
2
 
3
3
  # Windows version is always stubbed for now
4
4
  class IO
5
- def raw(*)
5
+ def raw(*, min: 1, time: nil, intr: nil)
6
6
  yield self
7
7
  end
8
8
 
@@ -1,3 +1,3 @@
1
1
  class IO::ConsoleMode
2
- VERSION = 0.7.0
2
+ VERSION = "0.7.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: io-console
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.2
5
5
  platform: java
6
6
  authors:
7
7
  - Nobu Nakada
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-12-13 00:00:00.000000000 Z
11
+ date: 2024-01-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: add console capabilities to IO instances.
14
14
  email: nobu@ruby-lang.org
@@ -34,6 +34,7 @@ licenses:
34
34
  - BSD-2-Clause
35
35
  metadata:
36
36
  source_code_url: https://github.com/ruby/io-console
37
+ changelog_uri: https://github.com/ruby/io-console/releases
37
38
  post_install_message:
38
39
  rdoc_options: []
39
40
  require_paths:
@@ -50,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
51
  - !ruby/object:Gem::Version
51
52
  version: '0'
52
53
  requirements: []
53
- rubygems_version: 3.5.0.dev
54
+ rubygems_version: 3.6.0.dev
54
55
  signing_key:
55
56
  specification_version: 4
56
57
  summary: Console interface