tty-screen 0.8.1 → 0.8.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +21 -0
- data/LICENSE.txt +1 -1
- data/README.md +36 -18
- data/lib/tty/screen/version.rb +2 -2
- data/lib/tty/screen.rb +219 -73
- data/lib/tty-screen.rb +1 -1
- metadata +11 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0710be23ea8be823cbb7cd583bbdc9606bdc0a8bb2efec10412c05c6895de6c4
|
4
|
+
data.tar.gz: eaa2498f0cba8ad0f2ef5c69a16a43f3ed631a2c5bf564ac63d3902f193ef051
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 20e76a0a0a5ab4d72b88b4bf8c3ccc341c249cdd54bcf0ca270eda89b6633cadf6e52f71e57127b45e339cc2a168a509fafead0f198e5cd9ea04ade10659d175
|
7
|
+
data.tar.gz: a995dccca4d956bf1675862149770b2f5164586df97f8250278652dcaf63c5905d60b385f3eb1d9c197554e56cd4dc884a5c9cbdca97f8bc765fe7aac1db769f
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,25 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## [v0.8.2] - 2023-12-17
|
4
|
+
|
5
|
+
### Changed
|
6
|
+
* Change the ioctl system call support check to use the output method
|
7
|
+
* Change size from ioctl to extract window buffer format and length to constants
|
8
|
+
* Change size from ioctl to allocate and read window buffer with fewer objects
|
9
|
+
* Change size detection from tput to check TTY on the output method
|
10
|
+
* Change size detection from stty to check TTY on the output method
|
11
|
+
* Change size detection from the environment variables to use the env method
|
12
|
+
* Change size from the ansicon environment variable to use the env method
|
13
|
+
* Change size from ansicon to use regexp last match to lookup pattern matches
|
14
|
+
* Change size from ansicon to allocate window dimensions with fewer objects
|
15
|
+
|
16
|
+
### Fixed
|
17
|
+
* Fix ioctl call test to stub terminal size encoding for big-endian systems
|
18
|
+
* Fix to skip terminal size detection from readline on non-TTY output
|
19
|
+
* Fix size detection from IO to skip loading io-console on non-TTY output
|
20
|
+
* Fix size detection from tput to check for non-zero columns instead of lines
|
21
|
+
* Fix JRuby and Windows system detection to check Ruby configuration only once
|
22
|
+
|
3
23
|
## [v0.8.1] - 2020-07-17
|
4
24
|
|
5
25
|
### Fixed
|
@@ -136,6 +156,7 @@
|
|
136
156
|
### Fixed
|
137
157
|
* Fix bug with screen detection from_io_console by @luxflux
|
138
158
|
|
159
|
+
[v0.8.2]: https://github.com/piotrmurach/tty-screen/compare/v0.8.1...v0.8.2
|
139
160
|
[v0.8.1]: https://github.com/piotrmurach/tty-screen/compare/v0.8.0...v0.8.1
|
140
161
|
[v0.8.0]: https://github.com/piotrmurach/tty-screen/compare/v0.7.1...v0.8.0
|
141
162
|
[v0.7.1]: https://github.com/piotrmurach/tty-screen/compare/v0.7.0...v0.7.1
|
data/LICENSE.txt
CHANGED
data/README.md
CHANGED
@@ -1,34 +1,34 @@
|
|
1
1
|
<div align="center">
|
2
|
-
<a href="https://
|
2
|
+
<a href="https://ttytoolkit.org"><img width="130" src="https://github.com/piotrmurach/tty/raw/master/images/tty.png" alt="TTY Toolkit logo"/></a>
|
3
3
|
</div>
|
4
4
|
|
5
|
-
# TTY::Screen
|
5
|
+
# TTY::Screen
|
6
6
|
|
7
7
|
[][gem]
|
8
|
-
[][gh_actions_ci]
|
9
9
|
[][appveyor]
|
10
10
|
[][codeclimate]
|
11
11
|
[][coverage]
|
12
|
-
[][inchpages]
|
13
12
|
|
14
13
|
[gitter]: https://gitter.im/piotrmurach/tty
|
15
14
|
[gem]: http://badge.fury.io/rb/tty-screen
|
16
|
-
[
|
15
|
+
[gh_actions_ci]: https://github.com/piotrmurach/tty-screen/actions/workflows/ci.yml
|
17
16
|
[appveyor]: https://ci.appveyor.com/project/piotrmurach/tty-screen
|
18
17
|
[codeclimate]: https://codeclimate.com/github/piotrmurach/tty-screen
|
19
18
|
[coverage]: https://coveralls.io/r/piotrmurach/tty-screen
|
20
|
-
[inchpages]: http://inch-ci.org/github/piotrmurach/tty-screen
|
21
19
|
|
22
|
-
> Terminal screen size detection
|
20
|
+
> Terminal screen size detection that works on Linux, macOS and Windows systems
|
21
|
+
and supports Ruby MRI, JRuby, TruffleRuby and Rubinius interpreters.
|
23
22
|
|
24
|
-
**TTY::Screen** provides
|
23
|
+
**TTY::Screen** provides a terminal screen size detection component for the
|
24
|
+
[TTY](https://github.com/piotrmurach/tty) toolkit.
|
25
25
|
|
26
26
|
## Installation
|
27
27
|
|
28
28
|
Add this line to your application's Gemfile:
|
29
29
|
|
30
30
|
```ruby
|
31
|
-
gem
|
31
|
+
gem "tty-screen"
|
32
32
|
```
|
33
33
|
|
34
34
|
And then execute:
|
@@ -41,13 +41,14 @@ Or install it yourself as:
|
|
41
41
|
|
42
42
|
## 1. Usage
|
43
43
|
|
44
|
-
|
44
|
+
Use the `size` method to detect terminal screen size. It will result in
|
45
|
+
a `[height, width]` array:
|
45
46
|
|
46
47
|
```ruby
|
47
|
-
TTY::Screen.size
|
48
|
+
TTY::Screen.size # => [51, 280]
|
48
49
|
```
|
49
50
|
|
50
|
-
|
51
|
+
Use the `width`, `columns` or `cols` method to detect terminal screen width:
|
51
52
|
|
52
53
|
```ruby
|
53
54
|
TTY::Screen.width # => 280
|
@@ -55,14 +56,20 @@ TTY::Screen.columns # => 280
|
|
55
56
|
TTY::Screen.cols # => 280
|
56
57
|
```
|
57
58
|
|
58
|
-
|
59
|
+
Use the `height`, `lines` or `rows` method to detect terminal screen height:
|
59
60
|
|
60
61
|
```ruby
|
61
|
-
TTY::Screen.height
|
62
|
-
TTY::Screen.
|
63
|
-
TTY::Screen.
|
62
|
+
TTY::Screen.height # => 51
|
63
|
+
TTY::Screen.lines # => 51
|
64
|
+
TTY::Screen.rows # => 51
|
64
65
|
```
|
65
66
|
|
67
|
+
## Development
|
68
|
+
|
69
|
+
After checking out the repo, run `bin/setup` to install dependencies.
|
70
|
+
Then, run `rake spec` to run the tests. You can also run `bin/console`
|
71
|
+
for an interactive prompt that will allow you to experiment.
|
72
|
+
|
66
73
|
## Contributing
|
67
74
|
|
68
75
|
1. Fork it ( https://github.com/piotrmurach/tty-screen/fork )
|
@@ -71,8 +78,19 @@ TTY::Screen.lines # => 51
|
|
71
78
|
4. Push to the branch (`git push origin my-new-feature`)
|
72
79
|
5. Create a new Pull Request
|
73
80
|
|
74
|
-
|
81
|
+
## License
|
82
|
+
|
83
|
+
The gem is available as open source under the terms of the
|
84
|
+
[MIT License](https://opensource.org/licenses/MIT).
|
85
|
+
|
86
|
+
## Code of Conduct
|
87
|
+
|
88
|
+
Everyone interacting in the TTY::Screen project's codebases, issue trackers,
|
89
|
+
chat rooms and mailing lists is expected to follow the
|
90
|
+
[code of conduct](https://github.com/piotrmurach/tty-screen/blob/master/CODE_OF_CONDUCT.md).
|
75
91
|
|
76
92
|
## Copyright
|
77
93
|
|
78
|
-
Copyright (c) 2014 Piotr Murach. See
|
94
|
+
Copyright (c) 2014 Piotr Murach. See
|
95
|
+
[LICENSE.txt](https://github.com/piotrmurach/tty-screen/blob/master/LICENSE.txt)
|
96
|
+
for further details.
|
data/lib/tty/screen/version.rb
CHANGED
data/lib/tty/screen.rb
CHANGED
@@ -8,54 +8,99 @@ end
|
|
8
8
|
require_relative "screen/version"
|
9
9
|
|
10
10
|
module TTY
|
11
|
-
#
|
11
|
+
# Responsible for detecting terminal screen size
|
12
12
|
#
|
13
13
|
# @api public
|
14
14
|
module Screen
|
15
|
-
#
|
15
|
+
# The Ruby configuration
|
16
|
+
#
|
17
|
+
# @return [Hash]
|
18
|
+
#
|
19
|
+
# @api private
|
20
|
+
RUBY_CONFIG = defined?(::RbConfig) ? ::RbConfig::CONFIG : {}
|
21
|
+
private_constant :RUBY_CONFIG
|
22
|
+
|
23
|
+
# Define module method as private
|
24
|
+
#
|
25
|
+
# @return [void]
|
26
|
+
#
|
27
|
+
# @api private
|
16
28
|
def self.private_module_function(name)
|
17
29
|
module_function(name)
|
18
30
|
private_class_method(name)
|
19
31
|
end
|
20
32
|
|
21
|
-
case
|
33
|
+
case RUBY_CONFIG["host_os"] || ::RUBY_PLATFORM
|
22
34
|
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
|
35
|
+
# Detect Windows system
|
36
|
+
#
|
37
|
+
# @return [Boolean]
|
38
|
+
#
|
39
|
+
# @api private
|
23
40
|
def windows?; true end
|
24
41
|
else
|
25
42
|
def windows?; false end
|
26
43
|
end
|
27
44
|
module_function :windows?
|
28
45
|
|
29
|
-
case
|
46
|
+
case RUBY_CONFIG["ruby_install_name"] || ::RUBY_ENGINE
|
30
47
|
when /jruby/
|
48
|
+
# Detect JRuby
|
49
|
+
#
|
50
|
+
# @return [Boolean]
|
51
|
+
#
|
52
|
+
# @api private
|
31
53
|
def jruby?; true end
|
32
54
|
else
|
33
55
|
def jruby?; false end
|
34
56
|
end
|
35
57
|
module_function :jruby?
|
36
58
|
|
37
|
-
#
|
59
|
+
# The default terminal screen size
|
38
60
|
#
|
39
|
-
# @
|
61
|
+
# @return [Array(Integer, Integer)]
|
62
|
+
#
|
63
|
+
# @api private
|
40
64
|
DEFAULT_SIZE = [27, 80].freeze
|
41
65
|
|
42
66
|
@env = ENV
|
43
67
|
@output = $stderr
|
44
68
|
|
45
69
|
class << self
|
46
|
-
#
|
70
|
+
# The environment variables
|
71
|
+
#
|
72
|
+
# @example
|
73
|
+
# TTY::Screen.env
|
74
|
+
#
|
75
|
+
# @example
|
76
|
+
# TTY::Screen.env = {"ROWS" => "51", "COLUMNS" => "211"}
|
77
|
+
#
|
78
|
+
# @return [Enumerable]
|
79
|
+
#
|
47
80
|
# @api public
|
48
81
|
attr_accessor :env
|
49
82
|
|
50
|
-
#
|
83
|
+
# The output stream with standard error as default
|
84
|
+
#
|
85
|
+
# @example
|
86
|
+
# TTY::Screen.output
|
87
|
+
#
|
88
|
+
# @example
|
89
|
+
# TTY::Screen.output = $stdout
|
90
|
+
#
|
91
|
+
# @return [IO]
|
92
|
+
#
|
51
93
|
# @api public
|
52
94
|
attr_accessor :output
|
53
95
|
end
|
54
96
|
|
55
|
-
#
|
97
|
+
# Detect terminal screen size
|
56
98
|
#
|
57
|
-
# @
|
58
|
-
#
|
99
|
+
# @example
|
100
|
+
# TTY::Screen.size # => [51, 211]
|
101
|
+
#
|
102
|
+
# @return [Array(Integer, Integer)]
|
103
|
+
# the terminal screen size
|
59
104
|
#
|
60
105
|
# @api public
|
61
106
|
def size(verbose: false)
|
@@ -72,6 +117,14 @@ module TTY
|
|
72
117
|
end
|
73
118
|
module_function :size
|
74
119
|
|
120
|
+
# Detect terminal screen width
|
121
|
+
#
|
122
|
+
# @example
|
123
|
+
# TTY::Screen.width # => 211
|
124
|
+
#
|
125
|
+
# @return [Integer]
|
126
|
+
#
|
127
|
+
# @api public
|
75
128
|
def width
|
76
129
|
size[1]
|
77
130
|
end
|
@@ -82,6 +135,14 @@ module TTY
|
|
82
135
|
module_function :columns
|
83
136
|
module_function :cols
|
84
137
|
|
138
|
+
# Detect terminal screen height
|
139
|
+
#
|
140
|
+
# @example
|
141
|
+
# TTY::Screen.height # => 51
|
142
|
+
#
|
143
|
+
# @return [Integer]
|
144
|
+
#
|
145
|
+
# @api public
|
85
146
|
def height
|
86
147
|
size[0]
|
87
148
|
end
|
@@ -92,9 +153,9 @@ module TTY
|
|
92
153
|
module_function :rows
|
93
154
|
module_function :lines
|
94
155
|
|
95
|
-
#
|
156
|
+
# Detect terminal screen size from default
|
96
157
|
#
|
97
|
-
# @return [Array
|
158
|
+
# @return [Array(Integer, Integer)]
|
98
159
|
#
|
99
160
|
# @api private
|
100
161
|
def size_from_default
|
@@ -102,32 +163,41 @@ module TTY
|
|
102
163
|
end
|
103
164
|
module_function :size_from_default
|
104
165
|
|
105
|
-
# Determine terminal size with a Windows native API
|
106
|
-
#
|
107
|
-
# @return [nil, Array[Integer, Integer]]
|
108
|
-
#
|
109
|
-
# @api private
|
110
166
|
if windows?
|
167
|
+
# The standard output handle
|
168
|
+
#
|
169
|
+
# @return [Integer]
|
170
|
+
#
|
171
|
+
# @api private
|
111
172
|
STDOUT_HANDLE = 0xFFFFFFF5
|
112
173
|
|
174
|
+
# Detect terminal screen size from Windows API
|
175
|
+
#
|
176
|
+
# @param [Boolean] verbose
|
177
|
+
# the verbose mode, by default false
|
178
|
+
#
|
179
|
+
# @return [Array(Integer, Integer), nil]
|
180
|
+
# the terminal screen size or nil
|
181
|
+
#
|
182
|
+
# @api private
|
113
183
|
def size_from_win_api(verbose: false)
|
114
184
|
require "fiddle" unless defined?(Fiddle)
|
115
185
|
|
116
186
|
kernel32 = Fiddle::Handle.new("kernel32")
|
117
|
-
get_std_handle = Fiddle::Function.new(
|
118
|
-
|
187
|
+
get_std_handle = Fiddle::Function.new(
|
188
|
+
kernel32["GetStdHandle"], [-Fiddle::TYPE_INT], Fiddle::TYPE_INT)
|
119
189
|
get_console_buffer_info = Fiddle::Function.new(
|
120
190
|
kernel32["GetConsoleScreenBufferInfo"],
|
121
191
|
[Fiddle::TYPE_LONG, Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT)
|
122
192
|
|
123
|
-
format
|
124
|
-
buffer
|
193
|
+
format = "SSSSSssssSS"
|
194
|
+
buffer = ([0] * format.size).pack(format)
|
125
195
|
stdout_handle = get_std_handle.(STDOUT_HANDLE)
|
126
196
|
|
127
197
|
get_console_buffer_info.(stdout_handle, buffer)
|
128
198
|
_, _, _, _, _, left, top, right, bottom, = buffer.unpack(format)
|
129
199
|
size = [bottom - top + 1, right - left + 1]
|
130
|
-
|
200
|
+
size if nonzero_column?(size[1] - 1)
|
131
201
|
rescue LoadError
|
132
202
|
warn "no native fiddle module found" if verbose
|
133
203
|
rescue Fiddle::DLError
|
@@ -138,19 +208,23 @@ module TTY
|
|
138
208
|
end
|
139
209
|
module_function :size_from_win_api
|
140
210
|
|
141
|
-
# Determine terminal size on jruby using native Java libs
|
142
|
-
#
|
143
|
-
# @return [nil, Array[Integer, Integer]]
|
144
|
-
#
|
145
|
-
# @api private
|
146
211
|
if jruby?
|
212
|
+
# Detect terminal screen size from Java
|
213
|
+
#
|
214
|
+
# @param [Boolean] verbose
|
215
|
+
# the verbose mode, by default false
|
216
|
+
#
|
217
|
+
# @return [Array(Integer, Integer), nil]
|
218
|
+
# the terminal screen size or nil
|
219
|
+
#
|
220
|
+
# @api private
|
147
221
|
def size_from_java(verbose: false)
|
148
222
|
require "java"
|
149
223
|
|
150
224
|
java_import "jline.TerminalFactory"
|
151
225
|
terminal = TerminalFactory.get
|
152
226
|
size = [terminal.get_height, terminal.get_width]
|
153
|
-
|
227
|
+
size if nonzero_column?(size[1])
|
154
228
|
rescue
|
155
229
|
warn "failed to import java terminal package" if verbose
|
156
230
|
end
|
@@ -159,21 +233,26 @@ module TTY
|
|
159
233
|
end
|
160
234
|
module_function :size_from_java
|
161
235
|
|
162
|
-
# Detect screen size
|
236
|
+
# Detect terminal screen size from io-console
|
237
|
+
#
|
238
|
+
# On Windows, the io-console falls back to reading environment
|
239
|
+
# variables. This means any user changes to the terminal screen
|
240
|
+
# size will not be reflected in the runtime of the Ruby application.
|
163
241
|
#
|
164
|
-
#
|
165
|
-
#
|
166
|
-
# size won't be reflected in the runtime of the Ruby app.
|
242
|
+
# @param [Boolean] verbose
|
243
|
+
# the verbose mode, by default false
|
167
244
|
#
|
168
|
-
# @return [
|
245
|
+
# @return [Array(Integer, Integer), nil]
|
246
|
+
# the terminal screen size or nil
|
169
247
|
#
|
170
248
|
# @api private
|
171
249
|
def size_from_io_console(verbose: false)
|
172
|
-
|
250
|
+
return unless output.tty?
|
173
251
|
|
174
|
-
|
252
|
+
require "io/console" unless IO.method_defined?(:winsize)
|
253
|
+
return unless output.respond_to?(:winsize)
|
175
254
|
|
176
|
-
size =
|
255
|
+
size = output.winsize
|
177
256
|
size if nonzero_column?(size[1])
|
178
257
|
rescue Errno::EOPNOTSUPP
|
179
258
|
# no support for winsize on output
|
@@ -182,34 +261,71 @@ module TTY
|
|
182
261
|
end
|
183
262
|
module_function :size_from_io_console
|
184
263
|
|
185
|
-
if !jruby? &&
|
186
|
-
|
187
|
-
|
188
|
-
|
264
|
+
if !jruby? && output.respond_to?(:ioctl)
|
265
|
+
# The get window size control code for Linux
|
266
|
+
#
|
267
|
+
# @return [Integer]
|
268
|
+
#
|
269
|
+
# @api private
|
270
|
+
TIOCGWINSZ = 0x5413
|
271
|
+
|
272
|
+
# The get window size control code for FreeBSD and macOS
|
273
|
+
#
|
274
|
+
# @return [Integer]
|
275
|
+
#
|
276
|
+
# @api private
|
277
|
+
TIOCGWINSZ_PPC = 0x40087468
|
278
|
+
|
279
|
+
# The get window size control code for Solaris
|
280
|
+
#
|
281
|
+
# @return [Integer]
|
282
|
+
#
|
283
|
+
# @api private
|
284
|
+
TIOCGWINSZ_SOL = 0x5468
|
285
|
+
|
286
|
+
# The ioctl window size buffer format
|
287
|
+
#
|
288
|
+
# @return [String]
|
289
|
+
#
|
290
|
+
# @api private
|
291
|
+
TIOCGWINSZ_BUF_FMT = "SSSS"
|
292
|
+
private_constant :TIOCGWINSZ_BUF_FMT
|
189
293
|
|
190
|
-
#
|
294
|
+
# The ioctl window size buffer length
|
191
295
|
#
|
192
|
-
# @return [
|
296
|
+
# @return [Integer]
|
297
|
+
#
|
298
|
+
# @api private
|
299
|
+
TIOCGWINSZ_BUF_LEN = TIOCGWINSZ_BUF_FMT.length
|
300
|
+
private_constant :TIOCGWINSZ_BUF_LEN
|
301
|
+
|
302
|
+
# Detect terminal screen size from ioctl
|
303
|
+
#
|
304
|
+
# @return [Array(Integer, Integer), nil]
|
305
|
+
# the terminal screen size or nil
|
193
306
|
#
|
194
307
|
# @api private
|
195
308
|
def size_from_ioctl
|
196
|
-
|
197
|
-
buffer = ([0] * format.size).pack(format)
|
309
|
+
buffer = Array.new(TIOCGWINSZ_BUF_LEN, 0).pack(TIOCGWINSZ_BUF_FMT)
|
198
310
|
|
199
311
|
if ioctl?(TIOCGWINSZ, buffer) ||
|
200
312
|
ioctl?(TIOCGWINSZ_PPC, buffer) ||
|
201
313
|
ioctl?(TIOCGWINSZ_SOL, buffer)
|
202
314
|
|
203
|
-
rows, cols, = buffer.unpack(
|
204
|
-
|
315
|
+
rows, cols, = buffer.unpack(TIOCGWINSZ_BUF_FMT)
|
316
|
+
[rows, cols] if nonzero_column?(cols)
|
205
317
|
end
|
206
318
|
end
|
207
319
|
|
208
|
-
# Check if ioctl
|
209
|
-
#
|
320
|
+
# Check if the ioctl call gets window size on any standard stream
|
321
|
+
#
|
322
|
+
# @param [Integer] control
|
323
|
+
# the control code
|
324
|
+
# @param [String] buf
|
325
|
+
# the window size buffer
|
210
326
|
#
|
211
327
|
# @return [Boolean]
|
212
|
-
#
|
328
|
+
# true if the ioctl call gets window size, false otherwise
|
213
329
|
#
|
214
330
|
# @api private
|
215
331
|
def ioctl?(control, buf)
|
@@ -225,12 +341,19 @@ module TTY
|
|
225
341
|
end
|
226
342
|
module_function :size_from_ioctl
|
227
343
|
|
228
|
-
# Detect screen size
|
344
|
+
# Detect terminal screen size from readline
|
345
|
+
#
|
346
|
+
# @param [Boolean] verbose
|
347
|
+
# the verbose mode, by default false
|
348
|
+
#
|
349
|
+
# @return [Array(Integer, Integer), nil]
|
350
|
+
# the terminal screen size or nil
|
229
351
|
#
|
230
352
|
# @api private
|
231
353
|
def size_from_readline(verbose: false)
|
232
|
-
|
354
|
+
return unless output.tty?
|
233
355
|
|
356
|
+
require "readline" unless defined?(::Readline)
|
234
357
|
return unless ::Readline.respond_to?(:get_screen_size)
|
235
358
|
|
236
359
|
size = ::Readline.get_screen_size
|
@@ -241,25 +364,31 @@ module TTY
|
|
241
364
|
end
|
242
365
|
module_function :size_from_readline
|
243
366
|
|
244
|
-
# Detect terminal size from tput
|
367
|
+
# Detect terminal screen size from tput
|
368
|
+
#
|
369
|
+
# @return [Array(Integer, Integer), nil]
|
370
|
+
# the terminal screen size or nil
|
245
371
|
#
|
246
372
|
# @api private
|
247
373
|
def size_from_tput
|
248
|
-
return unless
|
374
|
+
return unless output.tty? && command_exist?("tput")
|
249
375
|
|
250
376
|
lines = run_command("tput", "lines")
|
251
377
|
return unless lines
|
252
378
|
|
253
379
|
cols = run_command("tput", "cols")
|
254
|
-
[lines.to_i, cols.to_i] if nonzero_column?(
|
380
|
+
[lines.to_i, cols.to_i] if nonzero_column?(cols)
|
255
381
|
end
|
256
382
|
module_function :size_from_tput
|
257
383
|
|
258
|
-
# Detect terminal size from stty
|
384
|
+
# Detect terminal screen size from stty
|
385
|
+
#
|
386
|
+
# @return [Array(Integer, Integer), nil]
|
387
|
+
# the terminal screen size or nil
|
259
388
|
#
|
260
389
|
# @api private
|
261
390
|
def size_from_stty
|
262
|
-
return unless
|
391
|
+
return unless output.tty? && command_exist?("stty")
|
263
392
|
|
264
393
|
out = run_command("stty", "size")
|
265
394
|
return unless out
|
@@ -269,36 +398,43 @@ module TTY
|
|
269
398
|
end
|
270
399
|
module_function :size_from_stty
|
271
400
|
|
272
|
-
# Detect terminal size from environment
|
401
|
+
# Detect terminal screen size from environment variables
|
273
402
|
#
|
274
|
-
# After executing Ruby code
|
275
|
-
#
|
276
|
-
# and hence
|
277
|
-
# of LINES and COLUMNS environment variables.
|
403
|
+
# After executing Ruby code, when the user changes terminal
|
404
|
+
# screen size during code runtime, the code will not be
|
405
|
+
# notified, and hence will not see the new size reflected
|
406
|
+
# in its copy of LINES and COLUMNS environment variables.
|
278
407
|
#
|
279
|
-
# @return [
|
408
|
+
# @return [Array(Integer, Integer), nil]
|
409
|
+
# the terminal screen size or nil
|
280
410
|
#
|
281
411
|
# @api private
|
282
412
|
def size_from_env
|
283
|
-
return unless
|
413
|
+
return unless env["COLUMNS"] =~ /^\d+$/
|
284
414
|
|
285
|
-
size = [(
|
415
|
+
size = [(env["LINES"] || env["ROWS"]).to_i, env["COLUMNS"].to_i]
|
286
416
|
size if nonzero_column?(size[1])
|
287
417
|
end
|
288
418
|
module_function :size_from_env
|
289
419
|
|
290
|
-
# Detect terminal size from
|
420
|
+
# Detect terminal screen size from the ANSICON environment variable
|
421
|
+
#
|
422
|
+
# @return [Array(Integer, Integer), nil]
|
423
|
+
# the terminal screen size or nil
|
291
424
|
#
|
292
425
|
# @api private
|
293
426
|
def size_from_ansicon
|
294
|
-
return unless
|
427
|
+
return unless env["ANSICON"] =~ /\((.*)x(.*)\)/
|
295
428
|
|
296
|
-
size = [
|
429
|
+
size = [::Regexp.last_match(2).to_i, ::Regexp.last_match(1).to_i]
|
297
430
|
size if nonzero_column?(size[1])
|
298
431
|
end
|
299
432
|
module_function :size_from_ansicon
|
300
433
|
|
301
|
-
# Check if command exists
|
434
|
+
# Check if a command exists
|
435
|
+
#
|
436
|
+
# @param [String] command
|
437
|
+
# the command to check
|
302
438
|
#
|
303
439
|
# @return [Boolean]
|
304
440
|
#
|
@@ -307,12 +443,19 @@ module TTY
|
|
307
443
|
exts = env.fetch("PATHEXT", "").split(::File::PATH_SEPARATOR)
|
308
444
|
env.fetch("PATH", "").split(::File::PATH_SEPARATOR).any? do |dir|
|
309
445
|
file = ::File.join(dir, command)
|
310
|
-
::File.exist?(file) ||
|
446
|
+
::File.exist?(file) ||
|
447
|
+
exts.any? { |ext| ::File.exist?("#{file}#{ext}") }
|
311
448
|
end
|
312
449
|
end
|
313
450
|
private_module_function :command_exist?
|
314
451
|
|
315
|
-
#
|
452
|
+
# Run command capturing the standard output
|
453
|
+
#
|
454
|
+
# @param [Array<String>] args
|
455
|
+
# the command arguments
|
456
|
+
#
|
457
|
+
# @return [String, nil]
|
458
|
+
# the command output or nil
|
316
459
|
#
|
317
460
|
# @api private
|
318
461
|
def run_command(*args)
|
@@ -322,9 +465,12 @@ module TTY
|
|
322
465
|
end
|
323
466
|
private_module_function :run_command
|
324
467
|
|
325
|
-
# Check if number is non
|
468
|
+
# Check if a number is non-zero
|
326
469
|
#
|
327
|
-
#
|
470
|
+
# @param [Integer, String] column
|
471
|
+
# the column to check
|
472
|
+
#
|
473
|
+
# @return [Boolean]
|
328
474
|
#
|
329
475
|
# @api private
|
330
476
|
def nonzero_column?(column)
|
data/lib/tty-screen.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require_relative
|
1
|
+
require_relative "tty/screen"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tty-screen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Murach
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -38,8 +38,8 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '3.0'
|
41
|
-
description: Terminal screen size detection
|
42
|
-
|
41
|
+
description: Terminal screen size detection that works on Linux, macOS and Windows
|
42
|
+
systems and supports Ruby MRI, JRuby, TruffleRuby and Rubinius interpreters.
|
43
43
|
email:
|
44
44
|
- piotr@piotrmurach.com
|
45
45
|
executables: []
|
@@ -63,9 +63,11 @@ metadata:
|
|
63
63
|
bug_tracker_uri: https://github.com/piotrmurach/tty-screen/issues
|
64
64
|
changelog_uri: https://github.com/piotrmurach/tty-screen/blob/master/CHANGELOG.md
|
65
65
|
documentation_uri: https://www.rubydoc.info/gems/tty-screen
|
66
|
+
funding_uri: https://github.com/sponsors/piotrmurach
|
66
67
|
homepage_uri: https://ttytoolkit.org
|
68
|
+
rubygems_mfa_required: 'true'
|
67
69
|
source_code_uri: https://github.com/piotrmurach/tty-screen
|
68
|
-
post_install_message:
|
70
|
+
post_install_message:
|
69
71
|
rdoc_options: []
|
70
72
|
require_paths:
|
71
73
|
- lib
|
@@ -80,9 +82,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
80
82
|
- !ruby/object:Gem::Version
|
81
83
|
version: '0'
|
82
84
|
requirements: []
|
83
|
-
rubygems_version: 3.
|
84
|
-
signing_key:
|
85
|
+
rubygems_version: 3.4.10
|
86
|
+
signing_key:
|
85
87
|
specification_version: 4
|
86
|
-
summary: Terminal screen size detection
|
87
|
-
platforms and supports MRI, JRuby, TruffleRuby and Rubinius interpreters.
|
88
|
+
summary: Terminal screen size detection.
|
88
89
|
test_files: []
|