diff --git a/CHANGELOG.md b/CHANGELOG.md index c3feb3e..dd46361 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 022717f..dddd821 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/tty/command/printers/abstract.rb b/lib/tty/command/printers/abstract.rb index f590c3c..c977a76 100644 --- a/lib/tty/command/printers/abstract.rb +++ b/lib/tty/command/printers/abstract.rb @@ -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) diff --git a/spec/unit/printers/custom_spec.rb b/spec/unit/printers/custom_spec.rb index a814355..990b0ee 100644 --- a/spec/unit/printers/custom_spec.rb +++ b/spec/unit/printers/custom_spec.rb @@ -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 @@ -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 @@ -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