Skip to content

Commit

Permalink
add String#format_all and Format::format_all methods
Browse files Browse the repository at this point in the history
  • Loading branch information
henderea committed Jan 24, 2014
1 parent afaa71a commit 6346e9a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@ Here is an example:

This will return a string that uses ANSI escape sequences to make the word 'hi' bold and underlined, with a foreground color of yellow and a background color of green. You can use any color and make any format supported by the `EverydayCliUtils::Format` module. All 4 parts of the method (not counting the required `format` start) are optional, so you can do something like `'hi'.format_underline_bg_white`, but you have to keep the same order, so you can't do `'hi'.format_underline_bold_bg_white_fg_black`.

As of version 0.4.0, there is now a `String#format_all` method that will allow you to put formatting in the string instead of having to call methods in the middle of a string. Here is an example:

```ruby
'abc {def}(bdulfywbgr) ghi {jkl}(ulfyw) mno'.format_all
```

which is equivalent to
```ruby
"abc #{'def'.format_bold_underline_fg_yellow_bg_green} ghi #{'jkl'.format_underline_fg_yellow} mno"
```

Much shorter, right?

There is also a static version of the method in `EverydayCliUtils::Format` that takes the string as a parameter, for those of you that use the "safe" version.

###EverydayCliUtils::Histogram

Create a text-based histogram. This is an extension to the `Enumerable` module, unless you import `:histogram_safe`, in which case you can use the static methods in `EverydayCliUtils::Histogram`, with an added first parameter of the collection.
Expand Down
4 changes: 4 additions & 0 deletions lib/everyday-cli-utils/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ def respond_to?(method)
colors = 'black|red|green|yellow|blue|purple|cyan|white|none'
(!(name =~ /format(_bold)?(_underline)?(?:_fg_(#{colors}))?(?:_bg_(#{colors}))?/).nil?) || old_respond_to?(method)
end

def format_all
EverydayCliUtils::Format::format_all(self)
end
end
14 changes: 14 additions & 0 deletions lib/everyday-cli-utils/safe/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,19 @@ def self::underline(text, fgcolor = nil, bgcolor = nil)
def self::boldunderline(text, fgcolor = nil, bgcolor = nil)
self::format(text, self::build_string(true, true, fgcolor, bgcolor))
end

def self::format_all(text)
colors = 'bk|rd|gr|yw|bl|pu|cy|wh|no'
color_map = { 'bk' => :black, 'rd' => :red, 'gr' => :green, 'yw' => :yellow, 'bl' => :blue, 'pu' => :purple, 'cy' => :cyan, 'wh' => :white, 'no' => :none }
regex = /\{(.+?)\}\((bd)?(ul)?(?:f(#{colors}))?(?:b(#{colors}))?\)/
text.gsub(regex) { |m|
txt = $1
bold = !$2.nil?
underline = !$3.nil?
fg = $4.nil? ? nil : color_map[$4]
bg = $5.nil? ? nil : color_map[$5]
format(txt, build_string(bold, underline, fg, bg))
}
end
end
end
2 changes: 1 addition & 1 deletion lib/everyday-cli-utils/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module EverydayCliUtils
VERSION = '0.3.0'
VERSION = '0.4.0'
end
6 changes: 6 additions & 0 deletions spec/everyday-cli-utils/format_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,10 @@ def extract_format(text)
str.respond_to?(:hi).should be_false
expect { str.hi }.to raise_error(NameError)
end

it 'allows shorthand for formatting in-string' do
str = 'abc {def}(bdulfywbgr) ghi {jkl}(ulfyw) mno'
expected = "abc #{'def'.format_bold_underline_fg_yellow_bg_green} ghi #{'jkl'.format_underline_fg_yellow} mno"
str.format_all.should eq expected
end
end

0 comments on commit 6346e9a

Please sign in to comment.