io-pipe 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9a88bbfa83b7c2313a08e8e2cbcf41cb706025159bf1f7095657aa5da0dd5e1d
4
+ data.tar.gz: 8429d494b63a70b5fe3f318bc15711bcee07c09cbac180a4c56603e239347c39
5
+ SHA512:
6
+ metadata.gz: 62ce181ef05bc5fe05bbe1829a011eaf47ce049192757efd4d61c9ab208279f2cc0dc405e397ab88d177352788e74410ba6416788e12c38518679d823f924448
7
+ data.tar.gz: 5c19f484755bcf217b6ea76521472d346c5b9ac398c0ccf164f9ea339b391567d443ebaf03827c0028c461ce2622f51a0d148584d5027cedeba6119a1364184c
data/.document ADDED
@@ -0,0 +1,3 @@
1
+ README.md
2
+ lib
3
+ io-pipe.rdoc
data/.rdoc_options ADDED
@@ -0,0 +1,3 @@
1
+ ---
2
+ main_page: README.md
3
+ title: IO::Pipe Documentation
data/LICENSE.md ADDED
@@ -0,0 +1,24 @@
1
+ Copyright (c) 2023, Nobuyoshi Nakada <nobu@ruby-lang.org>.
2
+
3
+ Ruby License.
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions
7
+ are met:
8
+ 1. Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+ 2. Redistributions in binary form must reproduce the above copyright
11
+ notice, this list of conditions and the following disclaimer in the
12
+ documentation and/or other materials provided with the distribution.
13
+
14
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
+ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
+ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
+ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
+ SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # IO::Pipe
2
+
3
+ This library provides extended `` Kernel#` ``.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add io-pipe
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install io-pipe
14
+
15
+ ## Usage
16
+
17
+ With redirection.
18
+
19
+ ```shell-session
20
+ $ ruby -I./lib -rio/pipe -e 'using IO::Pipe' -e 'p *`cp`.redirect(err: %i[child out])'
21
+ "cp: missing file operand\n"
22
+ "Try 'cp --help' for more information.\n"
23
+ ```
24
+
25
+ With chomping.
26
+
27
+ ```shell-session
28
+ $ ruby -I./lib -rio/pipe -e 'using IO::Pipe' -e 'p *`ls *.md`.each(chomp: true)'
29
+ "README.md"
30
+ ```
31
+
32
+ With different line separator and chomping.
33
+
34
+ ```shell-session
35
+ $ ruby -I./lib -rio/pipe -e 'using IO::Pipe' -e 'p *`git ls-files -z *.md`.each("\0", chomp: true)'
36
+ "LICENSE.md"
37
+ "README.md"
38
+ ```
39
+
40
+ ## Development
41
+
42
+ After checking out the repo, run `bundle install` to install
43
+ dependencies
44
+
45
+ ## Contributing
46
+
47
+ Bug reports and pull requests are welcome on GitHub at https://github.com/nobu/io-pipe.
48
+ ```
data/lib/io/pipe.rb ADDED
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ class IO; end
4
+
5
+ # Module to refine <tt>Kernel#`</tt> method.
6
+ module IO::Pipe
7
+ # The version number
8
+ VERSION = "0.0.1"
9
+
10
+ #:stopdoc:
11
+ Command = Data.define(:command, :each_args, :each_kwds, :open_args)
12
+ #:startdoc:
13
+
14
+ ##
15
+ # Wrapper of command and options.
16
+ class Command
17
+ include Enumerable
18
+
19
+ ##
20
+ # :attr_reader: command
21
+ #
22
+ # \Command string or array to start a process.
23
+
24
+ ##
25
+ # :attr_reader: each_args
26
+ #
27
+ # Positional arguments to be passed to +#each+ method.
28
+
29
+ ##
30
+ # :attr_reader: each_kwds
31
+ #
32
+ # Keyword arguments to be passed to +#each+ method.
33
+
34
+ ##
35
+ # :attr_reader: open_args
36
+ #
37
+ # Arguments to be passed to +#open+ method.
38
+
39
+ # Creates instance.
40
+ def initialize(command:, each_args: nil, each_kwds: nil, open_args: {})
41
+ super
42
+ end
43
+
44
+ # Starts the command with +open_args+.
45
+ def open(&block)
46
+ IO.popen(command, **open_args, &block)
47
+ end
48
+
49
+ code = []
50
+
51
+ %i[
52
+ read read_nonblock readline readlines readpartial
53
+ readchar readbyte ungetbyte ungetc getbyte getc gets
54
+ ].map do |m|
55
+ code << "def #{m}(...) = open {|f| f.#{m}(...)}\n"
56
+ end
57
+
58
+ # Iterates command outputs.
59
+ def each(*args, **kwds, &block)
60
+ if block_given?
61
+ open {|f| f.each(*each_args, *args, **each_kwds, **kwds, &block)}
62
+ else
63
+ with(each_args: args, each_kwds: kwds)
64
+ end
65
+ end
66
+
67
+ %i[
68
+ each_line each_byte each_char each_codepoint
69
+ ].map do |m|
70
+ code << "def #{m}(*args, **kwds, &block) = open {|f| f.#{m}(*each_args, *args, **each_kwds, **kwds, &block)}\n"
71
+ end
72
+
73
+ eval code.join("")
74
+
75
+ # Redirects outputs.
76
+ def redirect(**to)
77
+ open_args.update(to)
78
+ self
79
+ end
80
+
81
+ alias to_str read
82
+ alias to_s read
83
+ end
84
+
85
+ ##
86
+ # Creates IO::Pipe::Command with +str+.
87
+ def `(str) = Command.new(str)
88
+
89
+ refine(::Kernel) {
90
+ ##
91
+ # Extended <tt>Kernel#`</tt> that creates IO::Pipe::Command with +str+.
92
+ def `(str) = Command.new(str)
93
+ }
94
+ end
data/sig/io/pipe.rbs ADDED
@@ -0,0 +1,2 @@
1
+ module IO::Pipe
2
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: io-pipe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nobuyoshi Nakada
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-02-17 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: An extension to the backtick method to allow additional optionsto be
14
+ passed to the command and IO#each method.
15
+ email:
16
+ - nobu@ruby-lang.org
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".document"
22
+ - ".rdoc_options"
23
+ - LICENSE.md
24
+ - README.md
25
+ - lib/io/pipe.rb
26
+ - sig/io/pipe.rbs
27
+ homepage: https://github.com/nobu/io-pipe
28
+ licenses:
29
+ - Ruby
30
+ - BSD-2-Clause
31
+ metadata:
32
+ source_code_uri: https://github.com/nobu/io-pipe
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 3.2.0
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubygems_version: 3.5.3
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Extended pipe command
52
+ test_files: []