outstand-tty-command 0.10.0.pre

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TTY
4
+ class Command
5
+ # Encapsulates the information on the command executed
6
+ #
7
+ # @api public
8
+ class Result
9
+ include Enumerable
10
+
11
+ # All data written out to process's stdout stream
12
+ attr_reader :out
13
+ alias stdout out
14
+
15
+ # All data written out to process's stdin stream
16
+ attr_reader :err
17
+ alias stderr err
18
+
19
+ # Total command execution time
20
+ attr_reader :runtime
21
+
22
+ # Create a result
23
+ #
24
+ # @api public
25
+ def initialize(status, out, err, runtime = 0.0)
26
+ @status = status
27
+ @out = out
28
+ @err = err
29
+ @runtime = runtime
30
+ end
31
+
32
+ # Enumerate over output lines
33
+ #
34
+ # @param [String] separator
35
+ #
36
+ # @api public
37
+ def each(separator = nil)
38
+ sep = separator || TTY::Command.record_separator
39
+ return unless @out
40
+ elements = @out.split(sep)
41
+ if block_given?
42
+ elements.each { |line| yield(line) }
43
+ else
44
+ elements.to_enum
45
+ end
46
+ end
47
+
48
+ # Information on how the process exited
49
+ #
50
+ # @api public
51
+ def exit_status
52
+ @status
53
+ end
54
+ alias exitstatus exit_status
55
+ alias status exit_status
56
+
57
+ def to_i
58
+ @status
59
+ end
60
+
61
+ def to_s
62
+ @status.to_s
63
+ end
64
+
65
+ def to_ary
66
+ [@out, @err]
67
+ end
68
+
69
+ def exited?
70
+ @status != nil
71
+ end
72
+ alias complete? exited?
73
+
74
+ def success?
75
+ exited? ? @status.zero? : false
76
+ end
77
+
78
+ def failure?
79
+ !success?
80
+ end
81
+ alias failed? failure?
82
+
83
+ def ==(other)
84
+ return false unless other.is_a?(TTY::Command::Result)
85
+ @status == other.to_i && to_ary == other.to_ary
86
+ end
87
+ end # Result
88
+ end # Command
89
+ end # TTY
@@ -0,0 +1,106 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TTY
4
+ class Command
5
+ # Retains the first N bytes and the last N bytes from written content
6
+ #
7
+ # @api private
8
+ class Truncator
9
+ # Default maximum byte size for prefix & suffix
10
+ DEFAULT_SIZE = 32 << 10
11
+
12
+ # Create a Truncator
13
+ #
14
+ # @param [Hash] options
15
+ # @option options [Number] max_size
16
+ #
17
+ # @api public
18
+ def initialize(options = {})
19
+ @max_size = options.fetch(:max_size) { DEFAULT_SIZE }
20
+ @prefix = ''
21
+ @suffix = ''
22
+ @skipped = 0
23
+ end
24
+
25
+ # Write content
26
+ #
27
+ # @param [String] content
28
+ # the content to write
29
+ #
30
+ # @return [nil]
31
+ #
32
+ # @api public
33
+ def write(content)
34
+ content = content.to_s.dup
35
+
36
+ content, @prefix = append(content, @prefix)
37
+
38
+ if (over = (content.bytesize - @max_size)) > 0
39
+ content = content.byteslice(over..-1)
40
+ @skipped += over
41
+ end
42
+
43
+ content, @suffix = append(content, @suffix)
44
+
45
+ # suffix is full but we still have content to write
46
+ while content.bytesize > 0
47
+ content = copy(content, @suffix)
48
+ end
49
+ end
50
+ alias << write
51
+
52
+ # Truncated representation of the content
53
+ #
54
+ # @return [String]
55
+ #
56
+ # @api public
57
+ def read
58
+ return @prefix if @suffix.empty?
59
+
60
+ if @skipped.zero?
61
+ return @prefix << @suffix
62
+ end
63
+
64
+ @prefix + "\n... omitting #{@skipped} bytes ...\n" + @suffix
65
+ end
66
+ alias to_s read
67
+
68
+ private
69
+
70
+ # Copy minimum bytes from source to destination
71
+ #
72
+ # @return [String]
73
+ # the remaining content
74
+ #
75
+ # @api private
76
+ def copy(value, dest)
77
+ bytes = value.bytesize
78
+ n = bytes < dest.bytesize ? bytes : dest.bytesize
79
+
80
+ head, tail = dest.byteslice(0...n), dest.byteslice(n..-1)
81
+ dest.replace("#{tail}#{value[0...n]}")
82
+ @skipped += head.bytesize
83
+ value.byteslice(n..-1)
84
+ end
85
+
86
+ # Append value to destination
87
+ #
88
+ # @param [String] value
89
+ #
90
+ # @param [String] dst
91
+ #
92
+ # @api private
93
+ def append(value, dst)
94
+ remain = @max_size - dst.bytesize
95
+ remaining = ''
96
+ if remain > 0
97
+ value_bytes = value.to_s.bytesize
98
+ offset = value_bytes < remain ? value_bytes : remain
99
+ remaining = value.byteslice(0...offset)
100
+ value = value.byteslice(offset..-1)
101
+ end
102
+ [value, dst + remaining]
103
+ end
104
+ end # Truncator
105
+ end # Command
106
+ end # TTY
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TTY
4
+ class Command
5
+ VERSION = '0.10.0.pre'
6
+ end # Command
7
+ end # TTY
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: outstand-tty-command
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.10.0.pre
5
+ platform: ruby
6
+ authors:
7
+ - Piotr Murach
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-09-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pastel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.5.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.5.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Execute shell commands with pretty output logging and capture their stdout,
70
+ stderr and exit status. Redirect stdin, stdout and stderr of each command to a file
71
+ or a string.
72
+ email:
73
+ - piotr@piotrmurach.com
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files:
77
+ - README.md
78
+ - CHANGELOG.md
79
+ - LICENSE.txt
80
+ files:
81
+ - CHANGELOG.md
82
+ - LICENSE.txt
83
+ - README.md
84
+ - lib/tty-command.rb
85
+ - lib/tty/command.rb
86
+ - lib/tty/command/child_process.rb
87
+ - lib/tty/command/cmd.rb
88
+ - lib/tty/command/dry_runner.rb
89
+ - lib/tty/command/exit_error.rb
90
+ - lib/tty/command/printers/abstract.rb
91
+ - lib/tty/command/printers/null.rb
92
+ - lib/tty/command/printers/pretty.rb
93
+ - lib/tty/command/printers/progress.rb
94
+ - lib/tty/command/printers/quiet.rb
95
+ - lib/tty/command/process_runner.rb
96
+ - lib/tty/command/result.rb
97
+ - lib/tty/command/truncator.rb
98
+ - lib/tty/command/version.rb
99
+ homepage: https://piotrmurach.github.io/tty
100
+ licenses:
101
+ - MIT
102
+ metadata:
103
+ allowed_push_host: https://rubygems.org
104
+ bug_tracker_uri: https://github.com/piotrmurach/tty-command/issues
105
+ changelog_uri: https://github.com/piotrmurach/tty-command/blob/master/CHANGELOG.md
106
+ documentation_uri: https://www.rubydoc.info/gems/tty-command
107
+ homepage_uri: https://piotrmurach.github.io/tty
108
+ source_code_uri: https://github.com/piotrmurach/tty-command
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: 2.0.0
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">"
121
+ - !ruby/object:Gem::Version
122
+ version: 1.3.1
123
+ requirements: []
124
+ rubygems_version: 3.1.2
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Execute shell commands with pretty output logging and capture their stdout,
128
+ stderr and exit status.
129
+ test_files: []