winsize 1.0.1 → 2.0.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.
- data/.gemspec.rb +51 -0
- data/.gitignore +5 -0
- data/.travis.yml +6 -0
- data/Gemfile +5 -0
- data/LICENSE +19 -16
- data/README.md +42 -39
- data/Rakefile +17 -0
- data/ext/winsize.c +4 -4
- data/lib/winsize.rb +19 -19
- data/lib/winsize/version.rb +2 -2
- data/test/test_helper.rb +3 -0
- data/test/winsize_test.rb +31 -0
- data/winsize.gemspec +24 -0
- metadata +78 -17
data/.gemspec.rb
ADDED
@@ -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
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
CHANGED
@@ -1,19 +1,22 @@
|
|
1
|
-
Copyright (
|
1
|
+
Copyright (c) 2012 Samuel Kadolph
|
2
2
|
|
3
|
-
|
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
|
-
|
11
|
-
|
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
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
THE
|
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 [](http://travis-ci.org/samuelkadolph/ruby-winsize)
|
2
2
|
|
3
|
-
|
3
|
+
winsize is a small library that lets you get and set the winsize of a tty.
|
4
4
|
|
5
|
-
|
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
|
-
|
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
|
-
|
11
|
+
## Installation
|
12
12
|
|
13
|
-
|
14
|
-
gem install winsize
|
15
|
-
```
|
13
|
+
add this line your application's Gemfile:
|
16
14
|
|
17
|
-
|
15
|
+
gem "winsize"
|
18
16
|
|
19
|
-
|
20
|
-
|
21
|
-
|
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
|
-
|
27
|
+
#### Get the size of the terminal
|
27
28
|
|
28
29
|
```ruby
|
29
30
|
require "winsize"
|
30
31
|
|
31
|
-
|
32
|
-
puts "Your terminal is #{
|
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
|
-
|
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,
|
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
|
-
|
60
|
-
|
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.
|
55
|
+
puts "Terminal size is #{$stdout.winsize.cols}x#{$stdout.winsize.rows}"
|
66
56
|
|
67
|
-
Signal.trap
|
68
|
-
puts "Terminal resized to #{$stdout.winsize.
|
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.
|
data/Rakefile
ADDED
@@ -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
|
data/ext/winsize.c
CHANGED
@@ -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
|
7
|
-
rb_define_const(
|
8
|
-
rb_define_const(
|
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
|
}
|
data/lib/winsize.rb
CHANGED
@@ -1,26 +1,27 @@
|
|
1
|
-
|
2
|
-
require "winsize
|
1
|
+
class Winsize
|
2
|
+
require "winsize.so"
|
3
|
+
require "winsize/version"
|
3
4
|
|
4
|
-
|
5
|
-
|
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
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
13
|
-
|
14
|
-
|
12
|
+
def [](index)
|
13
|
+
to_ary[index]
|
14
|
+
end
|
15
15
|
|
16
|
-
|
17
|
-
|
18
|
-
end
|
16
|
+
def to_ary
|
17
|
+
[rows, columns, horizontal_pixels, vertical_pixels]
|
19
18
|
end
|
20
19
|
|
21
|
-
|
22
|
-
|
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)
|
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::
|
39
|
+
include Winsize::IOExtension
|
40
40
|
end
|
data/lib/winsize/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
|
2
|
-
VERSION = "
|
1
|
+
class Winsize
|
2
|
+
VERSION = "2.0.0"
|
3
3
|
end
|
data/test/test_helper.rb
ADDED
@@ -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
|
data/winsize.gemspec
ADDED
@@ -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:
|
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:
|
13
|
-
dependencies:
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
-
-
|
29
|
-
-
|
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:
|
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.
|
110
|
+
rubygems_version: 1.8.25
|
51
111
|
signing_key:
|
52
112
|
specification_version: 3
|
53
|
-
summary:
|
54
|
-
|
55
|
-
|
113
|
+
summary: ! ''
|
114
|
+
test_files:
|
115
|
+
- test/test_helper.rb
|
116
|
+
- test/winsize_test.rb
|