Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change log

## Unreleased

### Fixed
* Add missing `class` keyword to README example for custom printers
* Pass command argument to abstract printer writer method calls

## [v0.10.1] - 2021-02-14

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ Please see [lib/tty/command/printers/abstract.rb](https://github.com/piotrmurach
At the very minimum you need to specify the `write` method that will be called during the lifecycle of command execution. The `write` accepts two arguments, first the currently run command instance and second the message to be printed:

```ruby
CustomPrinter < TTY::Command::Printers::Abstract
class CustomPrinter < TTY::Command::Printers::Abstract
def write(cmd, message)
puts cmd.to_command + message
end
Expand Down
8 changes: 4 additions & 4 deletions lib/tty/command/printers/abstract.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ def initialize(output, options = {})
end

def print_command_start(cmd, *args)
write(cmd.to_command + "#{args.join}")
write(cmd, args.join(" "))
end

def print_command_out_data(cmd, *args)
write(args.join(" "))
write(cmd, args.join(" "))
end

def print_command_err_data(cmd, *args)
write(args.join(" "))
write(cmd, args.join(" "))
end

def print_command_exit(cmd, *args)
write(args.join(" "))
write(cmd, args.join(" "))
end

def write(cmd, message)
Expand Down
12 changes: 6 additions & 6 deletions spec/unit/printers/custom_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@

before do
stub_const("CustomPrinter", Class.new(TTY::Command::Printers::Abstract) do
def write(message)
output << message
def write(cmd, message)
output << "#{cmd.to_command}#{message}"
end
end)
end

it "prints command start" do
printer = CustomPrinter.new(output)
cmd = TTY::Command::Cmd.new(:echo, "'hello world'")
cmd = TTY::Command::Cmd.new(:echo, "hello world")

printer.print_command_start(cmd)
output.rewind

expect(output.string).to eq("echo \\'hello\\ world\\'")
expect(output.string).to eq("echo hello\\ world")
end

it "prints command stdout data" do
Expand All @@ -28,7 +28,7 @@ def write(message)
printer.print_command_out_data(cmd, "data")
output.rewind

expect(output.string).to eq("data")
expect(output.string).to eq("echo hello\\ worlddata")
end

it "prints command exit" do
Expand All @@ -38,7 +38,7 @@ def write(message)
printer.print_command_exit(cmd)
output.rewind

expect(output.string).to be_empty
expect(output.string).to eq("echo hello\\ world")
end

it "accepts options" do
Expand Down