everyday-cli-utils 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8afc8c7cdc6d4d3adffcc15723afa531d4961533
4
- data.tar.gz: de9412ebd7b3965986e8fb66821ac94b3057a429
3
+ metadata.gz: 199206bbe82dfcfc462d6df301b006f4d2677399
4
+ data.tar.gz: 1f8214a8ba51c3b20b51da8b98459a69ca13ca78
5
5
  SHA512:
6
- metadata.gz: 2ba0851f31eee49fa57b74dc5b7e5999a34ebfccd56ca9e8b3446452d8f93ae5ae8dbd1f4e2d5a4f42186df2ce44050f30da8d5d93cffb6f43e1feadd8cd6d8e
7
- data.tar.gz: 656476dd69d7b17479d4aba60904a6d3685fac59461f73b9759a4929643e11fac61785a57f85c258b17a47950653b55953a0b2d775b90f6ace73519ba29d66c8
6
+ metadata.gz: 1a8ddc46de89d5b790c6ff6d0936b07958000a54f5c5eca3d306323b8c6f6fa35512718698f3351884a3f4178367f7afaee6dfcf24dde5c488eb5e8625125faa
7
+ data.tar.gz: 1e23c0a6cc0fa314ac6fc025b3362d313d3a8441275c144acb6f3ea68bac3d7221c5253d30af7107a8013ab6dc2e35fa006cf0f3a64f0b63c53f1351d238cfa0
data/README.md CHANGED
@@ -140,6 +140,25 @@ Here is an example:
140
140
 
141
141
  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`.
142
142
 
143
+ 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:
144
+
145
+ ```ruby
146
+ 'abc {def}(bdulfywbgr) ghi {jkl}(ulfyw) mno'.format_all
147
+ ```
148
+
149
+ which is equivalent to
150
+ ```ruby
151
+ "abc #{'def'.format_bold_underline_fg_yellow_bg_green} ghi #{'jkl'.format_underline_fg_yellow} mno"
152
+ ```
153
+
154
+ Much shorter, right?
155
+
156
+ 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.
157
+
158
+ As of version 0.5.0, there is now a `String#mycenter` method that takes a length and an optional character (defaults to `' '`) and centers the string using that character and length. This is designed to mimic the built-in `String#center` method, but add support for handling the formatting, since formatting is done by adding non-printing characters to the string. If you were to use the built-in `center` method, you would be adding too little padding.
159
+
160
+ 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.
161
+
143
162
  ###EverydayCliUtils::Histogram
144
163
 
145
164
  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.
@@ -23,4 +23,8 @@ class String
23
23
  def format_all
24
24
  EverydayCliUtils::Format::format_all(self)
25
25
  end
26
+
27
+ def mycenter(len, char = ' ')
28
+ EverydayCliUtils::Format::mycenter(self, len, char)
29
+ end
26
30
  end
@@ -117,5 +117,13 @@ module EverydayCliUtils
117
117
  format(txt, build_string(bold, underline, fg, bg))
118
118
  }
119
119
  end
120
+
121
+ def self.mycenter(str, len, char = ' ')
122
+ tlen = str.gsub(%r{\e\[.*?m}, '').length
123
+ return str if tlen >= len
124
+ b = ((len - tlen) / 2.0).floor
125
+ a = len - tlen - b
126
+ "#{char * b}#{str}#{char * a}"
127
+ end
120
128
  end
121
129
  end
@@ -1,3 +1,3 @@
1
1
  module EverydayCliUtils
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
@@ -67,4 +67,50 @@ describe EverydayCliUtils::Format do
67
67
  expected = "abc #{'def'.format_bold_underline_fg_yellow_bg_green} ghi #{'jkl'.format_underline_fg_yellow} mno"
68
68
  str.format_all.should eq expected
69
69
  end
70
+
71
+ it 'allows centering a formatted string' do
72
+ str = 'abc'
73
+ str2 = str.center(10)
74
+ strf = str.format_bold_underline_fg_yellow_bg_green
75
+ strf2 = strf.mycenter(10)
76
+ leading_whitespace = str2.length - str2.lstrip.length
77
+ trailing_whitespace = str2.length - str2.rstrip.length
78
+ leading_whitespace_f = strf2.length - strf2.lstrip.length
79
+ trailing_whitespace_f = strf2.length - strf2.rstrip.length
80
+ leading_whitespace_f.should eq leading_whitespace
81
+ trailing_whitespace_f.should eq trailing_whitespace
82
+
83
+ str = 'abcd'
84
+ str2 = str.center(10)
85
+ strf = str.format_bold_underline_fg_yellow_bg_green
86
+ strf2 = strf.mycenter(10)
87
+ leading_whitespace = str2.length - str2.lstrip.length
88
+ trailing_whitespace = str2.length - str2.rstrip.length
89
+ leading_whitespace_f = strf2.length - strf2.lstrip.length
90
+ trailing_whitespace_f = strf2.length - strf2.rstrip.length
91
+ leading_whitespace_f.should eq leading_whitespace
92
+ trailing_whitespace_f.should eq trailing_whitespace
93
+
94
+ str = 'abc'
95
+ str2 = str.center(11)
96
+ strf = str.format_bold_underline_fg_yellow_bg_green
97
+ strf2 = strf.mycenter(11)
98
+ leading_whitespace = str2.length - str2.lstrip.length
99
+ trailing_whitespace = str2.length - str2.rstrip.length
100
+ leading_whitespace_f = strf2.length - strf2.lstrip.length
101
+ trailing_whitespace_f = strf2.length - strf2.rstrip.length
102
+ leading_whitespace_f.should eq leading_whitespace
103
+ trailing_whitespace_f.should eq trailing_whitespace
104
+
105
+ str = 'abcd'
106
+ str2 = str.center(11)
107
+ strf = str.format_bold_underline_fg_yellow_bg_green
108
+ strf2 = strf.mycenter(11)
109
+ leading_whitespace = str2.length - str2.lstrip.length
110
+ trailing_whitespace = str2.length - str2.rstrip.length
111
+ leading_whitespace_f = strf2.length - strf2.lstrip.length
112
+ trailing_whitespace_f = strf2.length - strf2.rstrip.length
113
+ leading_whitespace_f.should eq leading_whitespace
114
+ trailing_whitespace_f.should eq trailing_whitespace
115
+ end
70
116
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: everyday-cli-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Henderson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-24 00:00:00.000000000 Z
11
+ date: 2014-01-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler