winsize 1.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,51 @@
1
+ class Readme < String
2
+ attr_reader :path
3
+
4
+ def initialize(path)
5
+ @path = path
6
+ super(File.read(self.path))
7
+ end
8
+
9
+ def summary
10
+ if self =~ /^# (?:\S+)\s+(.+?)\s{2,}/m
11
+ scrub $1
12
+ else
13
+ raise "could not find summary in #{path}"
14
+ end
15
+ end
16
+
17
+ def description
18
+ if self =~ /^## Description\s+(.+?)\s{2,}/m
19
+ scrub $1
20
+ else
21
+ raise "could not find description in #{path}"
22
+ end
23
+ end
24
+
25
+ private
26
+ def scrub(string)
27
+ string.delete("\\`").gsub(/\[([^\]]+)\]\([^)]*\)/, "\\1").tr("\n", " ").to_s
28
+ end
29
+ end
30
+
31
+ class Files < Array
32
+ def executables
33
+ grep(%r{^bin/}) { |f| File.basename(f) }
34
+ end
35
+
36
+ def requires
37
+ ["lib"]
38
+ end
39
+
40
+ def tests
41
+ grep(%r{^(test|spec|features)/})
42
+ end
43
+ end
44
+
45
+ def files
46
+ @files ||= Files.new(`git ls-files`.split($/))
47
+ end
48
+
49
+ def readme(path = File.expand_path("./README.md"))
50
+ (@readmes ||= {})[path] ||= Readme.new(path)
51
+ end
@@ -0,0 +1,5 @@
1
+ /Gemfile.lock
2
+ /lib/winsize.bundle
3
+ /lib/winsize.so
4
+ /pkg
5
+ /tmp
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 1.9.2
5
+ - 1.8.7
6
+ script: bundle exec rake test
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem "minitest", :platform => :ruby_18
data/LICENSE CHANGED
@@ -1,19 +1,22 @@
1
- Copyright (C) 2011 by Samuel Kadolph
1
+ Copyright (c) 2012 Samuel Kadolph
2
2
 
3
- Permission is hereby granted, free of charge, to any person obtaining a copy
4
- of this software and associated documentation files (the "Software"), to deal
5
- in the Software without restriction, including without limitation the rights
6
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- copies of the Software, and to permit persons to whom the Software is
8
- furnished to do so, subject to the following conditions:
3
+ MIT License
9
4
 
10
- The above copyright notice and this permission notice shall be included in
11
- all copies or substantial portions of the Software.
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
12
 
13
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- THE SOFTWARE.
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.
data/README.md CHANGED
@@ -1,72 +1,75 @@
1
- # ruby-winsize
1
+ # ruby-winsize [![Build Status](https://secure.travis-ci.org/samuelkadolph/ruby-winsize.png?branch=master)](http://travis-ci.org/samuelkadolph/ruby-winsize)
2
2
 
3
- ruby-winsize is a small library that lets you get and set the winsize of a terminal.
3
+ winsize is a small library that lets you get and set the winsize of a tty.
4
4
 
5
- ruby-winsize adds two methods (`winsize` and `winsize=`) to for use with any
6
- `IO` instance for a TTY device. The `Winsize::Winsize` class is an intermediate
7
- form that is used for `ioctl` calls to the TTY device.
5
+ ## Description
8
6
 
9
- ## Installing
7
+ winsize adds 2 methods: `winsize` and `winsize=` to `IO` that allows you to control the size of a tty window. Mainly used
8
+ with `pty` to deal with executables that word wrap (i.e. `top`). *If you are using ruby 1.9.3 this gem is not needed.
9
+ `require "io/console"` provides the same functionality.*
10
10
 
11
- ### Recommended
11
+ ## Installation
12
12
 
13
- ```
14
- gem install winsize
15
- ```
13
+ add this line your application's Gemfile:
16
14
 
17
- ### Edge
15
+ gem "winsize"
18
16
 
19
- ```
20
- git clone https://github.com/samuelkadolph/ruby-winsize
21
- cd ruby-winsize && rake install
22
- ```
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install winsize
23
24
 
24
25
  ## Usage
25
26
 
26
- To get the size of the `$stdout` terminal.
27
+ #### Get the size of the terminal
27
28
 
28
29
  ```ruby
29
30
  require "winsize"
30
31
 
31
- size = $stdout.winsize
32
- puts "Your terminal is #{size.columns}x#{size.rows}"
33
- puts "Your terminal is #{size.horizontal_pixels}px*#{size.vertical_pixels}px"
32
+ rows, cols = $stdout.winsize
33
+ puts "Your terminal is #{cols}x#{rows}"
34
34
  ```
35
35
 
36
- To set the size of a pty terminal.
37
- This is useful if you are running a subshell and want the subshell to have the
38
- same output size as the terminal you yourself are running in.
39
-
40
- You might think it would be useful to set the winsize of `$stdout` it actually
41
- isn't since the terminal is usually limited by the monitor size or the terminal
42
- application and increasing the size messes with trimming logic.
36
+ #### Set the size of a pty terminal
43
37
 
44
38
  ```ruby
45
39
  require "pty"
46
40
  require "winsize"
47
41
 
48
- size = Winsize.new(32, 160)
49
- # or
50
- # size = $stdout.winsize
51
-
42
+ size = Winsize.new(32, 180)
52
43
  input, output, pid = PTY.spawn
53
-
54
44
  output.winsize = size
55
- # or
56
- # output.winsize = 32, 160
57
45
  ```
58
46
 
59
- You may want to combine `winsize` and catching the [`SIGWINCH`](http://en.wikipedia.org/wiki/SIGWINCH) signal so you
60
- update something that your terminal has been resized.
47
+ #### Reacting to a resized terminal
48
+
49
+ You may want to combine `winsize` and catching the [`SIGWINCH`](http://en.wikipedia.org/wiki/SIGWINCH) signal so you can
50
+ react when the terminal has been resized.
61
51
 
62
52
  ```ruby
63
53
  require "winsize"
64
54
 
65
- puts "Terminal size is #{$stdout.winsize.columns}x#{$stdout.winsize.rows}"
55
+ puts "Terminal size is #{$stdout.winsize.cols}x#{$stdout.winsize.rows}"
66
56
 
67
- Signal.trap "WINCH" do
68
- puts "Terminal resized to #{$stdout.winsize.columns}x#{$stdout.winsize.rows}"
57
+ Signal.trap(:WINCH) do
58
+ puts "Terminal resized to #{$stdout.winsize.cols}x#{$stdout.winsize.rows}"
69
59
  end
70
60
 
71
61
  loop { sleep(10) }
72
- ```
62
+ ```
63
+
64
+ #### `io/console` API compatible
65
+
66
+ ```ruby
67
+ require "winsize"
68
+
69
+ $stdout.winsize.to_ary # => [32, 180]
70
+ $stdout.winsize = [32, 180]
71
+ ```
72
+
73
+ ## Contributing
74
+
75
+ Fork, branch & pull request.
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env rake
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/extensiontask"
5
+ require "rake/testtask"
6
+
7
+ Rake::ExtensionTask.new("winsize") do |task|
8
+ task.ext_dir = "ext"
9
+ end
10
+
11
+ Rake::TestTask.new do |task|
12
+ task.libs << "test"
13
+ task.test_files = Dir["test/**/*_test.rb"]
14
+ task.verbose = true
15
+ end
16
+
17
+ task :test => :compile
@@ -1,9 +1,9 @@
1
- #include "ruby.h"
2
1
  #include <sys/ioctl.h>
2
+ #include "ruby.h"
3
3
 
4
4
  void Init_winsize()
5
5
  {
6
- VALUE rb_mWinSize = rb_define_module("Winsize");
7
- rb_define_const(rb_mWinSize, "TIOCGWINSZ", ULONG2NUM(TIOCGWINSZ));
8
- rb_define_const(rb_mWinSize, "TIOCSWINSZ", ULONG2NUM(TIOCSWINSZ));
6
+ VALUE rb_cWinSize = rb_define_class("Winsize", rb_cObject);
7
+ rb_define_const(rb_cWinSize, "TIOCGWINSZ", ULONG2NUM(TIOCGWINSZ));
8
+ rb_define_const(rb_cWinSize, "TIOCSWINSZ", ULONG2NUM(TIOCSWINSZ));
9
9
  }
@@ -1,26 +1,27 @@
1
- require "winsize.so"
2
- require "winsize/version"
1
+ class Winsize
2
+ require "winsize.so"
3
+ require "winsize/version"
3
4
 
4
- module Winsize
5
- class Winsize
6
- attr_accessor :rows, :columns, :horizontal_pixels, :vertical_pixels
5
+ attr_accessor :rows, :columns, :horizontal_pixels, :vertical_pixels
6
+ alias cols columns
7
7
 
8
- def initialize(rows, columns, horizontal_pixels = 0, vertical_pixels = 0)
9
- @rows, @columns, @horizontal_pixels, @vertical_pixels = rows, columns, horizontal_pixels, vertical_pixels
10
- end
8
+ def initialize(rows, columns, horizontal_pixels = 0, vertical_pixels = 0)
9
+ @rows, @columns, @horizontal_pixels, @vertical_pixels = rows, columns, horizontal_pixels, vertical_pixels
10
+ end
11
11
 
12
- def [](index)
13
- [rows, columns][index]
14
- end
12
+ def [](index)
13
+ to_ary[index]
14
+ end
15
15
 
16
- def to_ioctl
17
- [rows, columns, horizontal_pixels, vertical_pixels].pack("SSSS")
18
- end
16
+ def to_ary
17
+ [rows, columns, horizontal_pixels, vertical_pixels]
19
18
  end
20
19
 
21
- module IOExtensions
22
- # TIOCGWINSZ & TIOCSWINSZ are defined in ext/winsize.c
20
+ def to_ioctl
21
+ [rows, columns, horizontal_pixels, vertical_pixels].pack("SSSS")
22
+ end
23
23
 
24
+ module IOExtension
24
25
  def winsize
25
26
  size = ""
26
27
  ioctl(TIOCGWINSZ, size)
@@ -28,13 +29,12 @@ module Winsize
28
29
  end
29
30
 
30
31
  def winsize=(size)
31
- size = Winsize.new(*size) unless size.respond_to?(:to_ioctl)
32
- size = size.to_ioctl
32
+ size = size.respond_to?(:to_ioctl) ? size.to_ioctl : Winsize.new(*size).to_ioctl
33
33
  ioctl(TIOCSWINSZ, size)
34
34
  end
35
35
  end
36
36
  end
37
37
 
38
38
  class IO
39
- include Winsize::IOExtensions
39
+ include Winsize::IOExtension
40
40
  end
@@ -1,3 +1,3 @@
1
- module WinSize
2
- VERSION = "1.0.1"
1
+ class Winsize
2
+ VERSION = "2.0.0"
3
3
  end
@@ -0,0 +1,3 @@
1
+ require "minitest/autorun"
2
+ require "minitest/spec"
3
+ require "winsize"
@@ -0,0 +1,31 @@
1
+ require "test_helper"
2
+ require "pty"
3
+
4
+ describe Winsize do
5
+ it "should add winsize and winsize= to IO" do
6
+ $stdout.must_respond_to(:winsize)
7
+ $stdout.must_respond_to(:winsize=)
8
+
9
+ size = $stdout.winsize
10
+ size.rows.wont_be_nil
11
+ size.columns.wont_be_nil
12
+ end
13
+
14
+ it "should not work on non tty files" do
15
+ read, _ = IO.pipe
16
+ lambda { read.winsize }.must_raise(Errno::ENOTTY, Errno::EINVAL)
17
+ lambda { read.winsize = [10, 10] }.must_raise(Errno::ENOTTY, Errno::EINVAL)
18
+ end
19
+
20
+ it "should set the winsize on a pty" do
21
+ master, slave, pid = PTY.spawn
22
+ begin
23
+ size = Winsize.new(32, 180)
24
+ master.winsize = size
25
+ master.winsize.rows.must_equal(32)
26
+ master.winsize.columns.must_equal(180)
27
+ ensure
28
+ Process.kill(:KILL, pid)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path("../.gemspec", __FILE__)
2
+ require File.expand_path("../lib/winsize/version", __FILE__)
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = "winsize"
6
+ spec.version = Winsize::VERSION
7
+ spec.authors = ["Samuel Kadolph"]
8
+ spec.email = ["samuel@kadolph.com"]
9
+ spec.description = readme.description
10
+ spec.summary = readme.summary
11
+ spec.homepage = "https://github.com/samuelkadolph/ruby-winsize"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = files
15
+ spec.executables = files.executables
16
+ spec.test_files = files.tests
17
+ spec.require_paths = files.requires
18
+
19
+ spec.required_ruby_version = ">= 1.8.7"
20
+
21
+ spec.add_development_dependency "bundler"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rake-compiler"
24
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: winsize
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,26 +9,83 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-23 00:00:00.000000000Z
13
- dependencies: []
14
- description: ruby-winsize adds two methods (winsize and winsize=) to for use with
15
- any IO instance for a TTY device. The Winsize::Winsize class is an intermediate
16
- form that is used for ioctl calls to the TTY device.
12
+ date: 2013-06-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake-compiler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: ! 'winsize adds 2 methods: winsize and winsize= to IO that allows you
63
+ to control the size of a tty window. Mainly used with pty to deal with executables
64
+ that word wrap (i.e. top). *If you are using ruby 1.9.3 this gem is not needed.
65
+ require "io/console" provides the same functionality.*'
17
66
  email:
18
67
  - samuel@kadolph.com
19
68
  executables: []
20
- extensions:
21
- - ext/extconf.rb
69
+ extensions: []
22
70
  extra_rdoc_files: []
23
71
  files:
72
+ - .gemspec.rb
73
+ - .gitignore
74
+ - .travis.yml
75
+ - Gemfile
76
+ - LICENSE
77
+ - README.md
78
+ - Rakefile
24
79
  - ext/extconf.rb
25
80
  - ext/winsize.c
26
- - lib/winsize/version.rb
27
81
  - lib/winsize.rb
28
- - LICENSE
29
- - README.md
82
+ - lib/winsize/version.rb
83
+ - test/test_helper.rb
84
+ - test/winsize_test.rb
85
+ - winsize.gemspec
30
86
  homepage: https://github.com/samuelkadolph/ruby-winsize
31
- licenses: []
87
+ licenses:
88
+ - MIT
32
89
  post_install_message:
33
90
  rdoc_options: []
34
91
  require_paths:
@@ -38,18 +95,22 @@ required_ruby_version: !ruby/object:Gem::Requirement
38
95
  requirements:
39
96
  - - ! '>='
40
97
  - !ruby/object:Gem::Version
41
- version: '0'
98
+ version: 1.8.7
42
99
  required_rubygems_version: !ruby/object:Gem::Requirement
43
100
  none: false
44
101
  requirements:
45
102
  - - ! '>='
46
103
  - !ruby/object:Gem::Version
47
104
  version: '0'
105
+ segments:
106
+ - 0
107
+ hash: 4587146050354108076
48
108
  requirements: []
49
109
  rubyforge_project:
50
- rubygems_version: 1.8.6
110
+ rubygems_version: 1.8.25
51
111
  signing_key:
52
112
  specification_version: 3
53
- summary: Small library that adds methods for getting and setting the winsize of any
54
- TTY IO object.
55
- test_files: []
113
+ summary: ! '![Build Status](http://travis-ci.org/samuelkadolph/ruby-winsize)'
114
+ test_files:
115
+ - test/test_helper.rb
116
+ - test/winsize_test.rb