winsize 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,19 @@
1
+ Copyright (C) 2011 by Samuel Kadolph
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:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
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.
@@ -0,0 +1,70 @@
1
+ # ruby-winsize
2
+
3
+ ruby-winsize is a small library that lets you get and set the winsize of a terminal.
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.
8
+
9
+ ## Installing
10
+
11
+ ### Recommended
12
+
13
+ ```
14
+ gem install winsize
15
+ ```
16
+
17
+ ### Edge
18
+
19
+ ```
20
+ git clone https://github.com/samuelkadolph/ruby-winsize
21
+ cd ruby-winsize && rake install
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ To get the size of the `$stdout` terminal.
27
+
28
+ ```ruby
29
+ require "winsize"
30
+
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"
34
+ ```
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.
43
+
44
+ ```ruby
45
+ require "pty"
46
+ require "winsize"
47
+
48
+ size = Winsize.new(32, 160)
49
+ # or
50
+ # size = $stdout.winsize
51
+
52
+ in, out, pid = PTY.spawn
53
+
54
+ out.winsize = winsize
55
+ # or
56
+ # out.winsize = 32, 160
57
+ ```
58
+
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.
61
+
62
+ ```ruby
63
+ require "winsize"
64
+
65
+ puts "Terminal size is #{$stdout.winsize.columns}x#{$stdout.winsize.rows}"
66
+
67
+ Signal.trap "WINCH" do
68
+ puts "Terminal resized to #{$stdout.winsize.columns}x#{$stdout.winsize.rows}"
69
+ end
70
+ ```
@@ -0,0 +1,5 @@
1
+ require "mkmf"
2
+
3
+ abort("ioctl is missing.\n\n") unless have_header("sys/ioctl.h")
4
+
5
+ create_makefile("winsize")
@@ -0,0 +1,9 @@
1
+ #include "ruby.h"
2
+ #include <sys/ioctl.h>
3
+
4
+ void Init_winsize()
5
+ {
6
+ VALUE rb_mWinSize = rb_define_module("Winsize");
7
+ rb_const_set(rb_mWinSize, rb_intern("TIOCGWINSZ"), ULONG2NUM(TIOCGWINSZ));
8
+ rb_const_set(rb_mWinSize, rb_intern("TIOCSWINSZ"), ULONG2NUM(TIOCSWINSZ));
9
+ }
@@ -0,0 +1,36 @@
1
+ require "winsize.so"
2
+ require "winsize/version"
3
+
4
+ module Winsize
5
+ class Winsize
6
+ attr_accessor :rows, :columns, :horizontal_pixels, :vertical_pixels
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
11
+
12
+ def to_ioctl
13
+ [rows, columns, horizontal_pixels, vertical_pixels].pack("SSSS")
14
+ end
15
+ end
16
+
17
+ module IOExtensions
18
+ # TIOCGWINSZ & TIOCSWINSZ are defined in ext/winsize.c
19
+
20
+ def winsize
21
+ size = ""
22
+ ioctl(TIOCGWINSZ, size)
23
+ Winsize.new(*size.unpack("SSSS"))
24
+ end
25
+
26
+ def winsize=(size)
27
+ size = Winsize.new(*size) unless size.respond_to?(:to_ioctl)
28
+ size = size.to_ioctl
29
+ ioctl(TIOCSWINSZ, size)
30
+ end
31
+ end
32
+ end
33
+
34
+ class IO
35
+ include Winsize::IOExtensions
36
+ end
@@ -0,0 +1,3 @@
1
+ module WinSize
2
+ VERSION = "1.0.0"
3
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: winsize
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Samuel Kadolph
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-24 00:00:00 Z
14
+ dependencies: []
15
+
16
+ description: ruby-winsize adds two methods (winsize and winsize=) to for use with any IO instance for a TTY device. The Winsize::Winsize class is an intermediate form that is used for ioctl calls to the TTY device.
17
+ email:
18
+ - samuel@kadolph.com
19
+ executables: []
20
+
21
+ extensions:
22
+ - ext/extconf.rb
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - ext/extconf.rb
27
+ - ext/winsize.c
28
+ - lib/winsize/version.rb
29
+ - lib/winsize.rb
30
+ - LICENSE
31
+ - README.md
32
+ homepage: https://github.com/samuelkadolph/ruby-winsize
33
+ licenses: []
34
+
35
+ post_install_message:
36
+ rdoc_options: []
37
+
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ requirements: []
53
+
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.1
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Small library that adds methods for getting and setting the winsize of any TTY IO object.
59
+ test_files: []
60
+