Skip to content

Commit fb37ebc

Browse files
committed
Merge pull request #308 from mattbrictson/formatter-options
Allow all Formatters to accept options hash
2 parents 58d9603 + 75fb82d commit fb37ebc

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ appear at the top.
1313
* display more accurate string for commands with spaces being output in `Formatter::Pretty`
1414
[PR #304](https://github.com/capistrano/sshkit/pull/304)
1515
@steved
16+
* `SSHKit::Formatter::Abstract` now accepts an optional Hash of options
17+
[PR #308](https://github.com/capistrano/sshkit/pull/308) @mattbrictson
1618

1719
## 1.8.1
1820

README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -443,24 +443,22 @@ ENV['SSHKIT_COLOR'] = 'TRUE'
443443

444444
Want custom output formatting? Here's what you have to do:
445445

446-
1. Write a new formatter class in the `SSHKit::Formatter` module. As an example, check out the default [pretty](https://github.com/capistrano/sshkit/blob/master/lib/sshkit/formatters/pretty.rb) formatter.
446+
1. Write a new formatter class in the `SSHKit::Formatter` module. Your class should subclass `SSHKit::Formatter::Abstract` to inherit conveniences and common behavior. For a basic an example, check out the [Pretty](https://github.com/capistrano/sshkit/blob/master/lib/sshkit/formatters/pretty.rb) formatter.
447447
1. Set the output format as described above. E.g. if your new formatter is called `FooBar`:
448448

449449
```ruby
450450
SSHKit.config.use_format :foobar
451451
```
452452

453-
If your formatter class takes a second `options` argument in its constructor, you can pass options to it like this:
453+
All formatters that extend from `SSHKit::Formatter::Abstract` accept an options Hash as a constructor argument. You can pass options to your formatter like this:
454454

455455
```ruby
456456
SSHKit.config.use_format :foobar, :my_option => "value"
457457
```
458458

459-
Which will call your constructor:
459+
You can then access these options using the `options` accessor within your formatter code.
460460

461-
```ruby
462-
SSHKit::Formatter::FooBar.new($stdout, :my_option => "value")
463-
```
461+
For a much more full-featured formatter example that makes use of options, check out the [Airbrussh repository](https://github.com/mattbrictson/airbrussh/).
464462

465463
## Output Verbosity
466464

lib/sshkit/formatters/abstract.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ module Formatter
77
class Abstract
88

99
extend Forwardable
10-
attr_reader :original_output
10+
attr_reader :original_output, :options
1111
def_delegators :@original_output, :read, :rewind
1212
def_delegators :@color, :colorize
1313

14-
def initialize(output)
14+
def initialize(output, options={})
1515
@original_output = output
16+
@options = options
1617
@color = SSHKit::Color.new(output)
1718
end
1819

test/unit/formatters/test_custom.rb

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ def test_double_chevron_logs_commands
4343
assert_log_output 'C 1 /usr/bin/env ls'
4444
end
4545

46+
def test_accepts_options_hash
47+
custom = CustomFormatter.new(output, :foo => 'value')
48+
assert_equal('value', custom.options[:foo])
49+
end
50+
4651
private
4752

4853
def assert_log_output(expected_output)

0 commit comments

Comments
 (0)