everyday-cli-utils 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +19 -0
- data/lib/everyday-cli-utils/format.rb +4 -0
- data/lib/everyday-cli-utils/safe/format.rb +8 -0
- data/lib/everyday-cli-utils/version.rb +1 -1
- data/spec/everyday-cli-utils/format_spec.rb +46 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 199206bbe82dfcfc462d6df301b006f4d2677399
|
4
|
+
data.tar.gz: 1f8214a8ba51c3b20b51da8b98459a69ca13ca78
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
@@ -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
|
@@ -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
|
+
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-
|
11
|
+
date: 2014-01-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|