Skip to content

Commit

Permalink
Merge 7520ba5 into 961573b
Browse files Browse the repository at this point in the history
  • Loading branch information
thisismydesign committed Apr 24, 2018
2 parents 961573b + 7520ba5 commit 51db12b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -54,6 +54,7 @@ Or install it yourself as:
* [2.3.2. UUID](#232-uuid)
* [2.3.3. Only output on error](#233-only-output-on-error)
* [2.3.4. Verbose](#234-verbose)
* [2.3.5 Separate commands with newline](#235-separate-commands-with-newline)
* [2.4. Dry run](#24-dry-run)
* [2.5. Wait](#25-wait)
* [2.6. Test](#26-test)
Expand Down Expand Up @@ -245,6 +246,22 @@ By default commands will produce warnings when, for example `pty` option is not
cmd.run("echo '\e[32mColors!\e[0m'", pty: true, verbose: false)
```

#### 2.3.5 Separate commands with newline

Setting `:separate_commands_with_newline` to `true` prints an empty line between `:pretty` command outputs:

```ruby
cmd = TTY::Command.new(separate_commands_with_newline: true)
cmd.run('rm -f file_1') && cmd.run('rm -f file_2')
# =>
# [5d90db5d] Running rm -f file_1
# [5d90db5d] Finished in 0.013 seconds with exit status 0 (successful)
#
# [e8114d68] Running rm -f file_2
# [e8114d68] Finished in 0.007 seconds with exit status 0 (successful)
#
```

### 2.4 Dry run

Sometimes it can be useful to put your script into a "dry run" mode that prints commands without actually running them. To simulate execution of the command use the `:dry_run` option:
Expand Down
3 changes: 2 additions & 1 deletion lib/tty/command.rb
Expand Up @@ -55,9 +55,10 @@ def initialize(**options)
@output = options.fetch(:output) { $stdout }
@color = options.fetch(:color) { true }
@uuid = options.fetch(:uuid) { true }
@separate_commands_with_newline = options.fetch(:separate_commands_with_newline) { false }
@printer_name = options.fetch(:printer) { :pretty }
@dry_run = options.fetch(:dry_run) { false }
@printer = use_printer(@printer_name, color: @color, uuid: @uuid)
@printer = use_printer(@printer_name, color: @color, uuid: @uuid, separate_commands_with_newline: @separate_commands_with_newline)
@cmd_options = {}
@cmd_options[:verbose] = options.fetch(:verbose, true)
@cmd_options[:pty] = true if options[:pty]
Expand Down
1 change: 1 addition & 0 deletions lib/tty/command/printers/pretty.rb
Expand Up @@ -38,6 +38,7 @@ def print_command_exit(cmd, status, runtime, *args)
message << " with exit status #{status}" if status
message << " (#{success_or_failure(status)})"
write(cmd, message.join, cmd.uuid)
output << "\n" if options.fetch(:separate_commands_with_newline) { false }
end

# Write message out to output
Expand Down
11 changes: 11 additions & 0 deletions spec/unit/printers/pretty_spec.rb
Expand Up @@ -169,4 +169,15 @@
"[\e[32maaaaaa\e[0m] Finished in x seconds with exit status 1 (\e[31;1mfailed\e[0m)\n"
])
end

it "prints new line after command output if separate_commands_with_newline is true" do
allow(SecureRandom).to receive(:uuid).and_return(uuid)
printer = TTY::Command::Printers::Pretty.new(output, separate_commands_with_newline: true)
cmd = TTY::Command::Cmd.new(:echo, 'hello')

printer.print_command_exit(cmd, 0, 5.321)
output.rewind

expect(output.string).to end_with("\n\n")
end
end

0 comments on commit 51db12b

Please sign in to comment.