tty-screen 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 163de431d1fd75a5a29b5d24ef67d3b40ec6a5a4
4
+ data.tar.gz: 868f17e7ac67424a1b6dd28b8f432401035cbf72
5
+ SHA512:
6
+ metadata.gz: d60be14e99f7a97bb5abc5635e873e43f53713d3e286bb02fce9f810acccbb1089d94e9b64c138945fa97e38d20516e6d7ca3f9c08453573a9fe4712b364cd1a
7
+ data.tar.gz: a777e4b0edc978eaa22616d00baeb26643c594714ceeb79ebc755aa4fd5a24e9eed60a420118599db286105addd00e703beaca1e9b08ff9d3b71e859da40daf6
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --warnings
@@ -0,0 +1 @@
1
+ 2.0.0
@@ -0,0 +1,23 @@
1
+ language: ruby
2
+ bundler_args: --without yard benchmarks
3
+ script: "bundle exec rake ci"
4
+ rvm:
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - 2.1.0
8
+ - ruby-head
9
+ matrix:
10
+ include:
11
+ - rvm: jruby-19mode
12
+ - rvm: jruby-20mode
13
+ - rvm: jruby-21mode
14
+ - rvm: jruby-head
15
+ - rvm: rbx-2
16
+ allow_failures:
17
+ - rvm: ruby-head
18
+ - rvm: jruby-head
19
+ - rvm: jruby-20mode
20
+ - rvm: jruby-21mode
21
+ fast_finish: true
22
+ branches:
23
+ only: master
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :development do
6
+ gem 'rake', '~> 10.3.2'
7
+ gem 'rspec', '~> 3.1.0'
8
+ gem 'yard', '~> 0.8.7'
9
+ gem 'timecop', '~> 0.7.1'
10
+ gem 'pastel', '~> 0.3.0'
11
+ end
12
+
13
+ group :metrics do
14
+ gem 'coveralls', '~> 0.7.0'
15
+ gem 'simplecov', '~> 0.8.2'
16
+ gem 'yardstick', '~> 0.9.9'
17
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Piotr Murach
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,60 @@
1
+ # TTY::Screen
2
+ [![Gem Version](https://badge.fury.io/rb/tty-screen.png)][gem]
3
+ [![Build Status](https://secure.travis-ci.org/peter-murach/tty-screen.png?branch=master)][travis]
4
+ [![Code Climate](https://codeclimate.com/github/peter-murach/tty-screen.png)][codeclimate]
5
+ [![Coverage Status](https://coveralls.io/repos/peter-murach/tty-screen/badge.png)][coverage]
6
+
7
+ [gem]: http://badge.fury.io/rb/tty-screen
8
+ [travis]: http://travis-ci.org/peter-murach/tty-screen
9
+ [codeclimate]: https://codeclimate.com/github/peter-murach/tty-screen
10
+ [coverage]: https://coveralls.io/r/peter-murach/tty-screen
11
+
12
+ > Terminal screen size detection which works both on Linux, OS X and Windows/Cygwin platforms and supports MRI, JRuby and Rubinius interpreters.
13
+
14
+ **TTY::Screen** provides independent screen size detection component for [TTY](https://github.com/peter-murach/tty) toolkit.
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'tty-screen'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install tty-screen
31
+
32
+ ## 1. Usage
33
+
34
+ **TTY::Screen** allows you to detect terminal screen size by calling `size` method which returns [height, width] tuple.
35
+
36
+ ```ruby
37
+ screen = TTY::Screen.new
38
+
39
+ screen.size # => [51, 280]
40
+ screen.width # => 280
41
+ screen.height # => 51
42
+ ```
43
+
44
+ You can also use above methods as class instance methods:
45
+
46
+ ```ruby
47
+ TTY::Screen.size # => [51, 280]
48
+ ```
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it ( https://github.com/[my-github-username]/tty-screen/fork )
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create a new Pull Request
57
+
58
+ ## Copyright
59
+
60
+ Copyright (c) 2014 Piotr Murach. See LICENSE for further details.
@@ -0,0 +1,8 @@
1
+ # coding: utf-8
2
+
3
+ require 'bundler/gem_tasks'
4
+
5
+ FileList['tasks/**/*.rake'].each(&method(:import))
6
+
7
+ desc 'Run all specs'
8
+ task ci: %w[ spec ]
@@ -0,0 +1,3 @@
1
+ # coding: utf-8
2
+
3
+ require 'tty/screen'
@@ -0,0 +1,154 @@
1
+ # coding: utf-8
2
+
3
+ require 'tty/screen/version'
4
+
5
+ module TTY
6
+ # Used for detecting screen properties
7
+ #
8
+ # @api public
9
+ class Screen
10
+ # Specifies an output stream object
11
+ #
12
+ # @api public
13
+ attr_reader :output
14
+
15
+ # Create terminal screen
16
+ #
17
+ # @param [Hash] options
18
+ # @option options [Object] :output
19
+ # the object that responds to print call defaulting to stderr
20
+ #
21
+ # @api public
22
+ def initialize(options = {})
23
+ @output = options.fetch(:output) { $stderr }
24
+ end
25
+
26
+ def self.size
27
+ @size ||= new.size
28
+ end
29
+
30
+ def self.width
31
+ size[1]
32
+ end
33
+
34
+ def self.height
35
+ size[0]
36
+ end
37
+
38
+ # Get terminal rows and columns
39
+ #
40
+ # @return [Array[Integer, Integer]]
41
+ # return rows & columns
42
+ #
43
+ # @api public
44
+ def size
45
+ @size ||= begin
46
+ size = from_io_console
47
+ size ||= from_readline
48
+ size ||= from_tput
49
+ size ||= from_stty
50
+ size ||= from_env
51
+ size ||= from_ansicon
52
+ size || default_size
53
+ end
54
+ end
55
+
56
+ # Default terminal size
57
+ #
58
+ # @api public
59
+ def default_size
60
+ [27, 80]
61
+ end
62
+
63
+ def height
64
+ size[0]
65
+ end
66
+ alias_method :rows, :height
67
+
68
+ def width
69
+ size[1]
70
+ end
71
+ alias_method :columns, :width
72
+
73
+ # Detect screen size by loading io/console lib
74
+ #
75
+ # @return [Array[Integer, Integer]]
76
+ #
77
+ # @api private
78
+ def from_io_console
79
+ return if jruby?
80
+ Kernel.require 'io/console'
81
+ size = IO.console.winsize
82
+ size if nonzero_column?(size[1])
83
+ rescue LoadError
84
+ # Didn't find io/console in stdlib
85
+ end
86
+
87
+ # Detect screen size using Readline
88
+ #
89
+ #
90
+ # @api private
91
+ def from_readline
92
+ return unless defined?(Readline)
93
+ size = Readline.get_screen_size
94
+ size if nonzero_column?(size[1])
95
+ rescue NotImplementedError
96
+ end
97
+
98
+ # Detect terminal size from tput utility
99
+ #
100
+ # @api public
101
+ def from_tput
102
+ return unless output.tty?
103
+ lines = run_command('tput', 'lines').to_i
104
+ cols = run_command('tput', 'cols').to_i
105
+ [lines, cols] if nonzero_column?(lines)
106
+ rescue Errno::ENOENT
107
+ end
108
+
109
+ # Detect terminal size from stty utility
110
+ #
111
+ # @api public
112
+ def from_stty
113
+ return unless output.tty?
114
+ size = run_command('stty', 'size').split.map(&:to_i)
115
+ size if nonzero_column?(size[1])
116
+ rescue Errno::ENOENT
117
+ end
118
+
119
+ # Detect terminal size from environment
120
+ #
121
+ # @api private
122
+ def from_env
123
+ return unless ENV['COLUMNS'] =~ /^\d+$/
124
+ size = [(ENV['LINES'] || ENV['ROWS']).to_i, ENV['COLUMNS'].to_i]
125
+ size if nonzero_column?(size[1])
126
+ end
127
+
128
+ # Detect terminal size on windows
129
+ #
130
+ # @api private
131
+ def from_ansicon
132
+ return unless ENV['ANSICON'] =~ /\((.*)x(.*)\)/
133
+ size = [$2, $1].map(&:to_i)
134
+ size if nonzero_column?(size[1])
135
+ end
136
+
137
+ # Runs command in subprocess
138
+ #
139
+ # @api public
140
+ def run_command(command, name)
141
+ `#{command} #{name} 2>/dev/null`
142
+ end
143
+
144
+ private
145
+
146
+ def nonzero_column?(column)
147
+ column.to_i > 0
148
+ end
149
+
150
+ def jruby?
151
+ RbConfig::CONFIG['ruby_install_name'] == 'jruby'
152
+ end
153
+ end # Screen
154
+ end # TTY
@@ -0,0 +1,7 @@
1
+ # coding: utf-8
2
+
3
+ module TTY
4
+ class Screen
5
+ VERSION = "0.1.0"
6
+ end # Screen
7
+ end # TTY
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+
3
+ if RUBY_VERSION > '1.9' and (ENV['COVERAGE'] || ENV['TRAVIS'])
4
+ require 'simplecov'
5
+ require 'coveralls'
6
+
7
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
8
+ SimpleCov::Formatter::HTMLFormatter,
9
+ Coveralls::SimpleCov::Formatter
10
+ ]
11
+
12
+ SimpleCov.start do
13
+ command_name 'spec'
14
+ add_filter 'spec'
15
+ end
16
+ end
17
+
18
+ require 'tty-screen'
19
+
20
+ RSpec.configure do |config|
21
+ config.expect_with :rspec do |expectations|
22
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
23
+ end
24
+
25
+ config.mock_with :rspec do |mocks|
26
+ mocks.verify_partial_doubles = true
27
+ end
28
+
29
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
30
+ config.disable_monkey_patching!
31
+
32
+ # This setting enables warnings. It's recommended, but in some cases may
33
+ # be too noisy due to issues in dependencies.
34
+ config.warnings = true
35
+
36
+ if config.files_to_run.one?
37
+ config.default_formatter = 'doc'
38
+ end
39
+
40
+ config.profile_examples = 2
41
+
42
+ config.order = :random
43
+
44
+ Kernel.srand config.seed
45
+ end
@@ -0,0 +1,117 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe TTY::Screen, '.size' do
4
+ let(:output) { StringIO.new('', 'w+') }
5
+
6
+ subject(:screen) { described_class.new(output: output) }
7
+
8
+ context 'size' do
9
+ it "correctly falls through choices" do
10
+ allow(screen).to receive(:from_io_console).and_return([51, 280])
11
+ allow(screen).to receive(:from_readline).and_return(nil)
12
+ expect(screen.size).to eq([51, 280])
13
+ expect(screen).to_not have_received(:from_readline)
14
+ expect(screen.width).to eq(280)
15
+ expect(screen.height).to eq(51)
16
+ end
17
+
18
+ it "allows to call size as class instance" do
19
+ screen = double(:screen, size: [51, 280])
20
+ allow(TTY::Screen).to receive(:new).and_return(screen)
21
+ expect(TTY::Screen.size).to eq([51, 280])
22
+ expect(TTY::Screen.size).to eq([51, 280])
23
+ expect(TTY::Screen.width).to eq(280)
24
+ expect(TTY::Screen.height).to eq(51)
25
+ expect(TTY::Screen).to have_received(:new).once
26
+ end
27
+ end
28
+
29
+ context 'from io console' do
30
+ it "doesn't calculate size if jruby " do
31
+ allow(screen).to receive(:jruby?).and_return(true)
32
+ expect(screen.from_io_console).to eq(nil)
33
+ end
34
+
35
+ it "doesn't calculate size if io/console not available" do
36
+ allow(screen).to receive(:jruby?).and_return(false)
37
+ allow(Kernel).to receive(:require).with('io/console').and_raise(LoadError)
38
+ expect(screen.from_io_console).to eq(nil)
39
+ end
40
+ end
41
+
42
+ context 'from tput' do
43
+ it "doesn't run command if outside of terminal" do
44
+ allow(output).to receive(:tty?).and_return(false)
45
+ expect(screen.from_tput).to eq(nil)
46
+ end
47
+
48
+ it "runs tput commands" do
49
+ allow(output).to receive(:tty?).and_return(true)
50
+ allow(screen).to receive(:run_command).with('tput', 'lines').and_return(51)
51
+ allow(screen).to receive(:run_command).with('tput', 'cols').and_return(280)
52
+ expect(screen.from_tput).to eq([51, 280])
53
+ end
54
+
55
+ it "doesn't return zero size" do
56
+ allow(output).to receive(:tty?).and_return(true)
57
+ allow(screen).to receive(:run_command).with('tput', 'lines').and_return(0)
58
+ allow(screen).to receive(:run_command).with('tput', 'cols').and_return(0)
59
+ expect(screen.from_tput).to eq(nil)
60
+ end
61
+ end
62
+
63
+ context 'from stty' do
64
+ it "doesn't run command if outside of terminal" do
65
+ allow(output).to receive(:tty?).and_return(false)
66
+ expect(screen.from_stty).to eq(nil)
67
+ end
68
+
69
+ it "runs stty commands" do
70
+ allow(output).to receive(:tty?).and_return(true)
71
+ allow(screen).to receive(:run_command).with('stty', 'size').and_return("51 280")
72
+ expect(screen.from_stty).to eq([51, 280])
73
+ end
74
+
75
+ it "doesn't return zero size" do
76
+ allow(output).to receive(:tty?).and_return(true)
77
+ allow(screen).to receive(:run_command).with('stty', 'size').and_return("0 0")
78
+ expect(screen.from_stty).to eq(nil)
79
+ end
80
+ end
81
+
82
+ context 'from env' do
83
+ it "doesn't calculate size without COLUMNS key" do
84
+ allow(ENV).to receive(:[]).with('COLUMNS').and_return nil
85
+ expect(screen.from_env).to eq(nil)
86
+ end
87
+
88
+ it "extracts lines and columns from environment" do
89
+ allow(ENV).to receive(:[]).with('COLUMNS').and_return("280")
90
+ allow(ENV).to receive(:[]).with('LINES').and_return("51")
91
+ expect(screen.from_env).to eq([51, 280])
92
+ end
93
+
94
+ it "doesn't return zero size" do
95
+ allow(ENV).to receive(:[]).with('COLUMNS').and_return("0")
96
+ allow(ENV).to receive(:[]).with('LINES').and_return("0")
97
+ expect(screen.from_env).to eq(nil)
98
+ end
99
+ end
100
+
101
+ context 'from ansicon' do
102
+ it "doesn't calculate size without ANSICON key" do
103
+ allow(ENV).to receive(:[]).with('ANSICON').and_return nil
104
+ expect(screen.from_ansicon).to eq(nil)
105
+ end
106
+
107
+ it "extracts lines and columns from environment" do
108
+ allow(ENV).to receive(:[]).with('ANSICON').and_return("(280x51)")
109
+ expect(screen.from_ansicon).to eq([51, 280])
110
+ end
111
+
112
+ it "doesn't return zero size" do
113
+ allow(ENV).to receive(:[]).with('ANSICON').and_return("(0x0)")
114
+ expect(screen.from_ansicon).to eq(nil)
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+
3
+ desc 'Load gem inside irb console'
4
+ task :console do
5
+ require 'irb'
6
+ require 'irb/completion'
7
+ require File.join(__FILE__, '../../lib/tty-screen')
8
+ ARGV.clear
9
+ IRB.start
10
+ end
@@ -0,0 +1,11 @@
1
+ # encoding: utf-8
2
+
3
+ desc 'Measure code coverage'
4
+ task :coverage do
5
+ begin
6
+ original, ENV['COVERAGE'] = ENV['COVERAGE'], 'true'
7
+ Rake::Task['spec'].invoke
8
+ ensure
9
+ ENV['COVERAGE'] = original
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+
3
+ begin
4
+ require 'rspec/core/rake_task'
5
+
6
+ desc 'Run all specs'
7
+ RSpec::Core::RakeTask.new(:spec) do |task|
8
+ task.pattern = 'spec/{unit,integration}{,/*/**}/*_spec.rb'
9
+ end
10
+
11
+ namespace :spec do
12
+ desc 'Run unit specs'
13
+ RSpec::Core::RakeTask.new(:unit) do |task|
14
+ task.pattern = 'spec/unit{,/*/**}/*_spec.rb'
15
+ end
16
+
17
+ desc 'Run integration specs'
18
+ RSpec::Core::RakeTask.new(:integration) do |task|
19
+ task.pattern = 'spec/integration{,/*/**}/*_spec.rb'
20
+ end
21
+ end
22
+
23
+ rescue LoadError
24
+ %w[spec spec:unit spec:integration].each do |name|
25
+ task name do
26
+ $stderr.puts "In order to run #{name}, do `gem install rspec`"
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'tty/screen/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "tty-screen"
8
+ spec.version = TTY::Screen::VERSION
9
+ spec.authors = ["Piotr Murach"]
10
+ spec.email = [""]
11
+ spec.summary = %q{Terminal screen size detection which works both on Linux, OS X and Windows/Cygwin platforms and supports MRI, JRuby and Rubinius interpreters.}
12
+ spec.description = %q{Terminal screen size detection which works both on Linux, OS X and Windows/Cygwin platforms and supports MRI, JRuby and Rubinius interpreters.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tty-screen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Piotr Murach
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ description: Terminal screen size detection which works both on Linux, OS X and Windows/Cygwin
28
+ platforms and supports MRI, JRuby and Rubinius interpreters.
29
+ email:
30
+ - ''
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - .gitignore
36
+ - .rspec
37
+ - .ruby-version
38
+ - .travis.yml
39
+ - Gemfile
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - lib/tty-screen.rb
44
+ - lib/tty/screen.rb
45
+ - lib/tty/screen/version.rb
46
+ - spec/spec_helper.rb
47
+ - spec/unit/size_spec.rb
48
+ - tasks/console.rake
49
+ - tasks/coverage.rake
50
+ - tasks/spec.rake
51
+ - tty-screen.gemspec
52
+ homepage: ''
53
+ licenses:
54
+ - MIT
55
+ metadata: {}
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 2.0.3
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: Terminal screen size detection which works both on Linux, OS X and Windows/Cygwin
76
+ platforms and supports MRI, JRuby and Rubinius interpreters.
77
+ test_files:
78
+ - spec/spec_helper.rb
79
+ - spec/unit/size_spec.rb
80
+ has_rdoc: