File: screen.rb

package info (click to toggle)
ruby-tty-screen 0.8.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 220 kB
  • sloc: ruby: 483; makefile: 4; sh: 4
file content (335 lines) | stat: -rw-r--r-- 9,026 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# frozen_string_literal: true

begin
  require "rbconfig"
rescue LoadError
end

require_relative "screen/version"

module TTY
  # Used for detecting screen properties
  #
  # @api public
  module Screen
    # Helper to define private functions
    def self.private_module_function(name)
      module_function(name)
      private_class_method(name)
    end

    case (defined?(::RbConfig) ? ::RbConfig::CONFIG["host_os"] : ::RUBY_PLATFORM)
    when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
      def windows?; true end
    else
      def windows?; false end
    end
    module_function :windows?

    case (defined?(::RbConfig) ? ::RbConfig::CONFIG["ruby_install_name"] : ::RUBY_ENGINE)
    when /jruby/
      def jruby?; true end
    else
      def jruby?; false end
    end
    module_function :jruby?

    # Default terminal size
    #
    # @api public
    DEFAULT_SIZE = [27, 80].freeze

    @env = ENV
    @output = $stderr

    class << self
      # Holds the environment variables
      # @api public
      attr_accessor :env

      # Specifies an output stream
      # @api public
      attr_accessor :output
    end

    # Get terminal rows and columns
    #
    # @return [Array[Integer, Integer]]
    #   return rows and columns
    #
    # @api public
    def size(verbose: false)
      size_from_java(verbose: verbose) ||
      size_from_win_api(verbose: verbose) ||
      size_from_ioctl ||
      size_from_io_console(verbose: verbose) ||
      size_from_readline(verbose: verbose) ||
      size_from_tput ||
      size_from_stty ||
      size_from_env ||
      size_from_ansicon ||
      size_from_default
    end
    module_function :size

    def width
      size[1]
    end
    module_function :width

    alias columns width
    alias cols width
    module_function :columns
    module_function :cols

    def height
      size[0]
    end
    module_function :height

    alias rows height
    alias lines height
    module_function :rows
    module_function :lines

    # Default size for the terminal
    #
    # @return [Array[Integer, Integer]]
    #
    # @api private
    def size_from_default
      DEFAULT_SIZE
    end
    module_function :size_from_default

    # Determine terminal size with a Windows native API
    #
    # @return [nil, Array[Integer, Integer]]
    #
    # @api private
    if windows?
      STDOUT_HANDLE = 0xFFFFFFF5

      def size_from_win_api(verbose: false)
        require "fiddle" unless defined?(Fiddle)

        kernel32 = Fiddle::Handle.new("kernel32")
        get_std_handle = Fiddle::Function.new(kernel32["GetStdHandle"],
                          [-Fiddle::TYPE_INT], Fiddle::TYPE_INT)
        get_console_buffer_info = Fiddle::Function.new(
          kernel32["GetConsoleScreenBufferInfo"],
          [Fiddle::TYPE_LONG, Fiddle::TYPE_VOIDP], Fiddle::TYPE_INT)

        format        = "SSSSSssssSS"
        buffer        = ([0] * format.size).pack(format)
        stdout_handle = get_std_handle.(STDOUT_HANDLE)

        get_console_buffer_info.(stdout_handle, buffer)
        _, _, _, _, _, left, top, right, bottom, = buffer.unpack(format)
        size = [bottom - top + 1, right - left + 1]
        return size if nonzero_column?(size[1] - 1)
      rescue LoadError
        warn "no native fiddle module found" if verbose
      rescue Fiddle::DLError
        # non windows platform or no kernel32 lib
      end
    else
      def size_from_win_api(verbose: false); nil end
    end
    module_function :size_from_win_api

    # Determine terminal size on jruby using native Java libs
    #
    # @return [nil, Array[Integer, Integer]]
    #
    # @api private
    if jruby?
      def size_from_java(verbose: false)
        require "java"

        java_import "jline.TerminalFactory"
        terminal = TerminalFactory.get
        size = [terminal.get_height, terminal.get_width]
        return size if nonzero_column?(size[1])
      rescue
        warn "failed to import java terminal package" if verbose
      end
    else
      def size_from_java(verbose: false); nil end
    end
    module_function :size_from_java

    # Detect screen size by loading io/console lib
    #
    # On Windows io_console falls back to reading environment
    # variables. This means any user changes to the terminal
    # size won't be reflected in the runtime of the Ruby app.
    #
    # @return [nil, Array[Integer, Integer]]
    #
    # @api private
    def size_from_io_console(verbose: false)
      require "io/console" unless IO.method_defined?(:winsize)

      return unless @output.tty? && @output.respond_to?(:winsize)

      size = @output.winsize
      size if nonzero_column?(size[1])
    rescue Errno::EOPNOTSUPP
      # no support for winsize on output
    rescue LoadError
      warn "no native io/console support or io-console gem" if verbose
    end
    module_function :size_from_io_console

    if !jruby? && @output.respond_to?(:ioctl)
      TIOCGWINSZ = 0x5413 # linux
      TIOCGWINSZ_PPC = 0x40087468 # macos, freedbsd, netbsd, openbsd
      TIOCGWINSZ_SOL = 0x5468 # solaris

      # Read terminal size from Unix ioctl
      #
      # @return [nil, Array[Integer, Integer]]
      #
      # @api private
      def size_from_ioctl
        format = "SSSS"
        buffer = ([0] * format.size).pack(format)

        if ioctl?(TIOCGWINSZ, buffer) ||
           ioctl?(TIOCGWINSZ_PPC, buffer) ||
           ioctl?(TIOCGWINSZ_SOL, buffer)

          rows, cols, = buffer.unpack(format)[0..1]
          return [rows, cols] if nonzero_column?(cols)
        end
      end

      # Check if ioctl can be called and any of the streams is
      # attached to a terminal.
      #
      # @return [Boolean]
      #   True if any of the streams is attached to a terminal, false otherwise.
      #
      # @api private
      def ioctl?(control, buf)
        ($stdout.ioctl(control, buf) >= 0) ||
          ($stdin.ioctl(control, buf) >= 0) ||
          ($stderr.ioctl(control, buf) >= 0)
      rescue SystemCallError
        false
      end
      module_function :ioctl?
    else
      def size_from_ioctl; nil end
    end
    module_function :size_from_ioctl

    # Detect screen size using Readline
    #
    # @api private
    def size_from_readline(verbose: false)
      require "readline" unless defined?(::Readline)

      return unless ::Readline.respond_to?(:get_screen_size)

      size = ::Readline.get_screen_size
      size if nonzero_column?(size[1])
    rescue LoadError
      warn "no readline gem" if verbose
    rescue NotImplementedError
    end
    module_function :size_from_readline

    # Detect terminal size from tput utility
    #
    # @api private
    def size_from_tput
      return unless @output.tty? && command_exist?("tput")

      lines = run_command("tput", "lines")
      return unless lines

      cols = run_command("tput", "cols")
      [lines.to_i, cols.to_i] if nonzero_column?(lines)
    end
    module_function :size_from_tput

    # Detect terminal size from stty utility
    #
    # @api private
    def size_from_stty
      return unless @output.tty? && command_exist?("stty")

      out = run_command("stty", "size")
      return unless out

      size = out.split.map(&:to_i)
      size if nonzero_column?(size[1])
    end
    module_function :size_from_stty

    # Detect terminal size from environment
    #
    # After executing Ruby code if the user changes terminal
    # dimensions during code runtime, the code won't be notified,
    # and hence won't see the new dimensions reflected in its copy
    # of LINES and COLUMNS environment variables.
    #
    # @return [nil, Array[Integer, Integer]]
    #
    # @api private
    def size_from_env
      return unless @env["COLUMNS"] =~ /^\d+$/

      size = [(@env["LINES"] || @env["ROWS"]).to_i, @env["COLUMNS"].to_i]
      size if nonzero_column?(size[1])
    end
    module_function :size_from_env

    # Detect terminal size from Windows ANSICON
    #
    # @api private
    def size_from_ansicon
      return unless @env["ANSICON"] =~ /\((.*)x(.*)\)/

      size = [$2, $1].map(&:to_i)
      size if nonzero_column?(size[1])
    end
    module_function :size_from_ansicon

    # Check if command exists
    #
    # @return [Boolean]
    #
    # @api private
    def command_exist?(command)
      exts = env.fetch("PATHEXT", "").split(::File::PATH_SEPARATOR)
      env.fetch("PATH", "").split(::File::PATH_SEPARATOR).any? do |dir|
        file = ::File.join(dir, command)
        ::File.exist?(file) || exts.any? { |ext| ::File.exist?("#{file}#{ext}") }
      end
    end
    private_module_function :command_exist?

    # Runs command silently capturing the output
    #
    # @api private
    def run_command(*args)
      %x(#{args.join(" ")})
    rescue IOError, SystemCallError
      nil
    end
    private_module_function :run_command

    # Check if number is non zero
    #
    # return [Boolean]
    #
    # @api private
    def nonzero_column?(column)
      column.to_i > 0
    end
    private_module_function :nonzero_column?
  end # Screen
end # TTY