Skip to content

Commit

Permalink
Add ability to run command in pseudo terminal in reference to issue #33
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmurach committed Nov 5, 2017
1 parent e38f28c commit 897038e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions lib/tty/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def initialize(**options)
@dry_run = options.fetch(:dry_run) { false }
@printer = use_printer(@printer_name, color: @color, uuid: @uuid)
@cmd_options = {}
@cmd_options[:pty] = true if options[:pty]
@cmd_options[:binmode] = true if options[:binmode]
@cmd_options[:timeout] = options[:timeout] if options[:timeout]
end
Expand Down
13 changes: 10 additions & 3 deletions lib/tty/command/execute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,18 @@ module Execute
def spawn(cmd)
process_opts = normalize_redirect_options(cmd.options)
binmode = cmd.options[:binmode] || false
pty = cmd.options[:pty] || false
pipe = IO.method(:pipe)

if pty
require 'pty'
pipe = PTY.method(:open)
end

# Create pipes
in_rd, in_wr = IO.pipe # reading
out_rd, out_wr = IO.pipe # writing
err_rd, err_wr = IO.pipe # error
in_rd, in_wr = pipe.() # reading
out_rd, out_wr = pipe.() # writing
err_rd, err_wr = pipe.() # error
in_wr.sync = true

if binmode
Expand Down
23 changes: 23 additions & 0 deletions spec/unit/pty_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
RSpec.describe TTY::Command, ':pty' do
it "executes command in pseudo terminal mode as global option" do
color_cli = tmp_path('color')
output = StringIO.new
cmd = TTY::Command.new(output: output, pty: true)

out, err = cmd.run(color_cli)

expect(err).to eq('')
expect(out).to eq("\e[32mcolored\e[0m\r\n")
end

it "executes command in pseudo terminal mode as command option" do
color_cli = tmp_path('color')
output = StringIO.new
cmd = TTY::Command.new(output: output)

out, err = cmd.run(color_cli, pty: true)

expect(err).to eq('')
expect(out).to eq("\e[32mcolored\e[0m\r\n")
end
end

0 comments on commit 897038e

Please sign in to comment.