tty-screen-0.7.1/0000755000175000017500000000000013621162451013455 5ustar gabstergabstertty-screen-0.7.1/tty-screen.gemspec0000644000175000017500000000337013621162451017122 0ustar gabstergabster######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- # stub: tty-screen 0.7.1 ruby lib Gem::Specification.new do |s| s.name = "tty-screen".freeze s.version = "0.7.1" s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.metadata = { "allowed_push_host" => "https://rubygems.org", "bug_tracker_uri" => "https://github.com/piotrmurach/tty-screen/issues", "changelog_uri" => "https://github.com/piotrmurach/tty-screen/blob/master/CHANGELOG.md", "documentation_uri" => "https://www.rubydoc.info/gems/tty-screen", "homepage_uri" => "https://ttytoolkit.org", "source_code_uri" => "https://github.com/piotrmurach/tty-screen" } if s.respond_to? :metadata= s.require_paths = ["lib".freeze] s.authors = ["Piotr Murach".freeze] s.bindir = "exe".freeze s.date = "2020-02-02" s.description = "Terminal screen size detection which works on Linux, OS X and Windows/Cygwin platforms and supports MRI, JRuby and Rubinius interpreters.".freeze s.email = ["piotr@piotrmurach.com".freeze] s.extra_rdoc_files = ["CHANGELOG.md".freeze, "README.md".freeze] s.files = ["CHANGELOG.md".freeze, "LICENSE.txt".freeze, "README.md".freeze, "lib/tty-screen.rb".freeze, "lib/tty/screen.rb".freeze, "lib/tty/screen/version.rb".freeze] s.homepage = "https://ttytoolkit.org".freeze s.licenses = ["MIT".freeze] s.required_ruby_version = Gem::Requirement.new(">= 2.0.0".freeze) s.rubygems_version = "2.7.6.2".freeze s.summary = "Terminal screen size detection which works on Linux, OS X and Windows/Cygwin platforms and supports MRI, JRuby and Rubinius interpreters.".freeze end tty-screen-0.7.1/lib/0000755000175000017500000000000013621162451014223 5ustar gabstergabstertty-screen-0.7.1/lib/tty/0000755000175000017500000000000013621162451015043 5ustar gabstergabstertty-screen-0.7.1/lib/tty/screen/0000755000175000017500000000000013621162451016322 5ustar gabstergabstertty-screen-0.7.1/lib/tty/screen/version.rb0000644000175000017500000000015213621162451020332 0ustar gabstergabster# fronzen_string_literal: true module TTY module Screen VERSION = "0.7.1" end # Screen end # TTY tty-screen-0.7.1/lib/tty/screen.rb0000644000175000017500000001616313621162451016656 0ustar gabstergabster# fronzen_string_literal: true 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 # Default terminal size # # @api public DEFAULT_SIZE = [27, 80].freeze @env = ENV @output = $stderr class << self attr_accessor :env # Specifies an output stream # # @api public attr_accessor :output end # Get terminal rows and columns # # @return [Array[Integer, Integer]] # return rows & columns # # @api public def size size = size_from_java size ||= size_from_win_api size ||= size_from_ioctl size ||= size_from_io_console size ||= size_from_readline size ||= size_from_tput size ||= size_from_stty size ||= size_from_env size ||= size_from_ansicon size || DEFAULT_SIZE 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 STDOUT_HANDLE = 0xFFFFFFF5 # Determine terminal size with a Windows native API # # @return [nil, Array[Integer, Integer]] # # @api private def size_from_win_api(verbose: nil) require '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 module_function :size_from_win_api # Determine terminal size on jruby using native Java libs # # @return [nil, Array[Integer, Integer]] # # @api private def size_from_java(verbose: nil) return unless jruby? 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 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: nil) return if jruby? require 'io/console' begin if @output.tty? && IO.method_defined?(:winsize) size = @output.winsize size if nonzero_column?(size[1]) end rescue Errno::EOPNOTSUPP # no support for winsize on output end rescue LoadError warn 'no native io/console support or io-console gem' if verbose end module_function :size_from_io_console TIOCGWINSZ = 0x5413 TIOCGWINSZ_PPC = 0x40087468 # Read terminal size from Unix ioctl # # @return [nil, Array[Integer, Integer]] # # @api private def size_from_ioctl return if jruby? return unless @output.respond_to?(:ioctl) format = 'SSSS' buffer = ([0] * format.size).pack(format) if ioctl?(TIOCGWINSZ, buffer) || ioctl?(TIOCGWINSZ_PPC, buffer) rows, cols, = buffer.unpack(format)[0..1] return [rows, cols] if nonzero_column?(cols) end end module_function :size_from_ioctl # Check if ioctl can be called and the device is attached to terminal # # @api private def ioctl?(control, buf) @output.ioctl(control, buf) >= 0 rescue SystemCallError false end module_function :ioctl? # Detect screen size using Readline # # @api private def size_from_readline if defined?(Readline) && Readline.respond_to?(:get_screen_size) size = Readline.get_screen_size size if nonzero_column?(size[1]) end rescue NotImplementedError end module_function :size_from_readline # Detect terminal size from tput utility # # @api private def size_from_tput return unless @output.tty? lines = run_command('tput', 'lines').to_i cols = run_command('tput', 'cols').to_i [lines, cols] if nonzero_column?(lines) rescue IOError, SystemCallError end module_function :size_from_tput # Detect terminal size from stty utility # # @api private def size_from_stty return unless @output.tty? out = run_command('stty', 'size') return unless out size = out.split.map(&:to_i) size if nonzero_column?(size[1]) rescue IOError, SystemCallError 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 # Runs command silently capturing the output # # @api private def run_command(*args) require 'tempfile' out = Tempfile.new('tty-screen') result = system(*args, out: out.path, err: File::NULL) return if result.nil? out.rewind out.read ensure out.close if out 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? def jruby? RbConfig::CONFIG['ruby_install_name'] == 'jruby' end private_module_function :jruby? end # Screen end # TTY tty-screen-0.7.1/lib/tty-screen.rb0000644000175000017500000000003613621162451016644 0ustar gabstergabsterrequire_relative 'tty/screen' tty-screen-0.7.1/README.md0000644000175000017500000000521413621162451014736 0ustar gabstergabster
tty logo
# TTY::Screen [![Gitter](https://badges.gitter.im/Join%20Chat.svg)][gitter] [![Gem Version](https://badge.fury.io/rb/tty-screen.svg)][gem] [![Build Status](https://secure.travis-ci.org/piotrmurach/tty-screen.svg?branch=master)][travis] [![Build status](https://ci.appveyor.com/api/projects/status/myjv8kahk1iwrlha?svg=true)][appveyor] [![Code Climate](https://codeclimate.com/github/piotrmurach/tty-screen/badges/gpa.svg)][codeclimate] [![Coverage Status](https://coveralls.io/repos/piotrmurach/tty-screen/badge.svg)][coverage] [![Inline docs](http://inch-ci.org/github/piotrmurach/tty-screen.svg?branch=master)][inchpages] [gitter]: https://gitter.im/piotrmurach/tty [gem]: http://badge.fury.io/rb/tty-screen [travis]: http://travis-ci.org/piotrmurach/tty-screen [appveyor]: https://ci.appveyor.com/project/piotrmurach/tty-screen [codeclimate]: https://codeclimate.com/github/piotrmurach/tty-screen [coverage]: https://coveralls.io/r/piotrmurach/tty-screen [inchpages]: http://inch-ci.org/github/piotrmurach/tty-screen > Terminal screen size detection which works on Linux, OS X and Windows/Cygwin platforms and supports MRI, JRuby and Rubinius interpreters. **TTY::Screen** provides independent terminal screen size detection component for [TTY](https://github.com/piotrmurach/tty) toolkit. ## Installation Add this line to your application's Gemfile: ```ruby gem 'tty-screen' ``` And then execute: $ bundle Or install it yourself as: $ gem install tty-screen ## 1. Usage **TTY::Screen** allows you to detect terminal screen size by calling `size` method which returns [height, width] tuple. ```ruby TTY::Screen.size # => [51, 280] ``` To read terminal width do: ```ruby TTY::Screen.width # => 280 TTY::Screen.columns # => 280 TTY::Screen.cols # => 280 ``` Similarly, to read terminal height do: ```ruby TTY::Screen.height # => 51 TTY::Screen.rows # => 51 TTY::Screen.lines # => 51 ``` ## Contributing 1. Fork it ( https://github.com/piotrmurach/tty-screen/fork ) 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create a new Pull Request This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. ## Copyright Copyright (c) 2014 Piotr Murach. See LICENSE for further details. tty-screen-0.7.1/LICENSE.txt0000644000175000017500000000205513621162451015302 0ustar gabstergabsterCopyright (c) 2014 Piotr Murach MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. tty-screen-0.7.1/CHANGELOG.md0000644000175000017500000001004613621162451015267 0ustar gabstergabster# Change log ## [v0.7.1] - 2020-02-02 ### Changed * Change gemspec to add metadata, remove test artifacts and load version directly ## [v0.7.0] - 2019-05-19 ### Changed * Change gemspec to load files directly without using git * Change to relax development dependencies ## [v0.6.5] - 2018-07-13 ### Changed * Change to namespace version file to allow for direct vendoring ## [v0.6.4] - 2017-12-22 ### Fixed * Fix to suppress stderr output from run_command by Tero Marttila(@SpComb) ## [v0.6.3] - 2017-11-22 ### Changed * Change #size_from_tput & #size_from_stty to capture generic IO and command execution errors to make the calls more robust ### Fixed * Fix #size_from_ioctl to handle Errno errors and deal with Errno::EOPNOTSUPP ## [v0.6.2] - 2017-11-04 ### Fixed * Fix #size_from_java to provide size only for non-zero values * Fix #size_from_ioctl to provide size only for non-zero values ## [v0.6.1] - 2017-10-29 ### Fixed * Fix #size_from_win_api to provide size if non zero to avoid [1,1] size ## [v0.6.0] - 2017-10-29 ### Added * Add #size_from_ioctl check for reading terminal size with Unix ioctl * Add #size_from_java check for reading terminal size from Java on JRuby * Add #size_from_win_api check for reading terminal size from Windows C API ### Changed * Change TTY::Screen to a module without any state * Change to prefix all checks with `size` keyword * Change gemspec to require ruby >= 2.0.0 * Remove #try_io_console and inline with io-console check * Remove #default_size and replace with constant * Remove TTY::Screen::Size class ## [v0.5.1] - 2017-10-26 ### Changed * Change #from_io_console to return nil when no size present * Change #run_command to silently ignore any errors ### Fixed * Fix #from_readline check to prevent from failing on missing api call * Fix #from_stty to only extract size when stty command returns output * Fix #run_command to correctly capture command output and fix #from_tput check ## [v0.5.0] - 2016-01-03 ### Changed * Change size to accept environment as input * Remove Color detection, available as tty-color gem dependency ## [v0.4.3] - 2015-11-01 ### Added * Add NoValue to Color class to mark failure of reading color value ### Changed * Change Color class supports? to recognize lack of color value ### Fixed * Fix issue with #from_curses method and remove ensure block ## [v0.4.2] - 2015-10-31 ### Changed * Change visibility of output to prevent warnings ## [v0.4.1] - 2015-10-31 ### Changed * Change to switch off verbose mode by default ## [v0.4.0] - 2015-09-12 ### Added * Add terminal color support detection ## [v0.3.0] - 2015-09-11 ### Fixed * Fix bug loading standard library ## [v0.2.0] - 2015-05-11 ### Changed * Change to stop memoization of screen class instance method ### Fixed * Fix bug with screen detection from_io_console by @luxflux [v0.7.1]: https://github.com/piotrmurach/tty-screen/compare/v0.7.0...v0.7.1 [v0.7.0]: https://github.com/piotrmurach/tty-screen/compare/v0.6.5...v0.7.0 [v0.6.5]: https://github.com/piotrmurach/tty-screen/compare/v0.6.4...v0.6.5 [v0.6.4]: https://github.com/piotrmurach/tty-screen/compare/v0.6.3...v0.6.4 [v0.6.3]: https://github.com/piotrmurach/tty-screen/compare/v0.6.2...v0.6.3 [v0.6.2]: https://github.com/piotrmurach/tty-screen/compare/v0.6.1...v0.6.2 [v0.6.1]: https://github.com/piotrmurach/tty-screen/compare/v0.6.0...v0.6.1 [v0.6.0]: https://github.com/piotrmurach/tty-screen/compare/v0.5.1...v0.6.0 [v0.5.1]: https://github.com/piotrmurach/tty-screen/compare/v0.5.0...v0.5.1 [v0.5.0]: https://github.com/piotrmurach/tty-screen/compare/v0.4.3...v0.5.0 [v0.4.3]: https://github.com/piotrmurach/tty-screen/compare/v0.4.2...v0.4.3 [v0.4.2]: https://github.com/piotrmurach/tty-screen/compare/v0.4.1...v0.4.2 [v0.4.1]: https://github.com/piotrmurach/tty-screen/compare/v0.4.0...v1.4.1 [v0.4.0]: https://github.com/piotrmurach/tty-screen/compare/v0.3.0...v0.4.0 [v0.3.0]: https://github.com/piotrmurach/tty-screen/compare/v0.2.0...v0.3.0 [v0.2.0]: https://github.com/piotrmurach/tty-screen/compare/v0.1.0...v0.2.0 [v0.1.0]: https://github.com/piotrmurach/tty-screen/compare/v0.1.0