fmt 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -2
- data/lib/fmt/filter_groups/filter_group.rb +56 -0
- data/lib/fmt/filter_groups/rainbow_filter_group.rb +27 -0
- data/lib/fmt/filter_groups/string_filter_group.rb +28 -0
- data/lib/fmt/formatter.rb +12 -2
- data/lib/fmt/transformer.rb +6 -12
- data/lib/fmt/version.rb +1 -1
- data/lib/fmt.rb +5 -3
- metadata +4 -2
- data/lib/fmt/filters.rb +0 -76
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7aded878eeff57ab27f37b6e76bc34281d94000c95c5d0ae77dde2bee5ab5989
|
4
|
+
data.tar.gz: 0d6390579afbfa629f0e53a127c25b60c04548b831e8ec13b9ebd8ed0880a238
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1235f7508b608760ce2f984cdcb321c01ac2a1cd3d7d5b9d1b586b45614e3b79c38bf900264b569fa469c17e34d2b6e1999984715a9b51c8caa792ee7d080aa9
|
7
|
+
data.tar.gz: '012095a10cbeac34f3457bf18ebe9135513a774984139cd1c7c300cd00d774590eff1826f7af137adbb32acd77f2eb8bffa1d52b48c579422fe43a2e7c3769cf'
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
<p align="center">
|
2
2
|
<a href="http://blog.codinghorror.com/the-best-code-is-no-code-at-all/">
|
3
|
-
<img alt="Lines of Code" src="https://img.shields.io/badge/loc-
|
3
|
+
<img alt="Lines of Code" src="https://img.shields.io/badge/loc-347-47d299.svg" />
|
4
4
|
</a>
|
5
5
|
<a href="https://github.com/testdouble/standard">
|
6
6
|
<img alt="Ruby Style" src="https://img.shields.io/badge/style-standard-168AFE?logo=ruby&logoColor=FE1616" />
|
@@ -38,7 +38,7 @@ I'm currenly using this to help build beautiful CLI applications with Ruby. Plus
|
|
38
38
|
## Setup
|
39
39
|
|
40
40
|
```
|
41
|
-
bundle add rainbow # optional
|
41
|
+
bundle add rainbow # <- optional
|
42
42
|
bundle add fmt
|
43
43
|
```
|
44
44
|
|
@@ -72,6 +72,8 @@ Basic example:
|
|
72
72
|
require "rainbow"
|
73
73
|
require "fmt"
|
74
74
|
|
75
|
+
Fmt.add_rainbow_filters
|
76
|
+
|
75
77
|
template = "Hello %{name}cyan|bold"
|
76
78
|
Fmt template, name: "World"
|
77
79
|
|
@@ -86,6 +88,8 @@ Mix and match native formatting with Rainbow formatting:
|
|
86
88
|
require "rainbow"
|
87
89
|
require "fmt"
|
88
90
|
|
91
|
+
Fmt.add_rainbow_filters
|
92
|
+
|
89
93
|
template = "Date: %{date}.10s|magenta"
|
90
94
|
Fmt template, date: Time.now
|
91
95
|
|
@@ -100,6 +104,8 @@ Multiline example:
|
|
100
104
|
require "rainbow"
|
101
105
|
require "fmt"
|
102
106
|
|
107
|
+
Fmt.add_rainbow_filters
|
108
|
+
|
103
109
|
template = <<~T
|
104
110
|
Date: %{date}.10s|underline
|
105
111
|
|
@@ -124,6 +130,7 @@ The block accepts a string and should return a replacement string.
|
|
124
130
|
require "rainbow"
|
125
131
|
require "fmt"
|
126
132
|
|
133
|
+
Fmt.add_rainbow_filters
|
127
134
|
Fmt.add_filter(:ljust) { |val| "".ljust 14, val.to_s }
|
128
135
|
|
129
136
|
template = <<~T
|
@@ -148,6 +155,8 @@ Just wrap the embedded template in double curly braces: `{{EMBEDDED TEMPLATE HER
|
|
148
155
|
require "rainbow"
|
149
156
|
require "fmt"
|
150
157
|
|
158
|
+
Fmt.add_rainbow_filters
|
159
|
+
|
151
160
|
template = "%{value}lime {{%{embed_value}red|bold|underline}}"
|
152
161
|
Fmt template, value: "Outer", embed_value: "Inner"
|
153
162
|
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "monitor"
|
4
|
+
require_relative "../filter"
|
5
|
+
|
6
|
+
module Fmt
|
7
|
+
class FilterGroup
|
8
|
+
include Enumerable
|
9
|
+
include MonitorMixin
|
10
|
+
|
11
|
+
def initialize
|
12
|
+
super
|
13
|
+
@data = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def [](name)
|
17
|
+
synchronize { data[name.to_sym] }
|
18
|
+
end
|
19
|
+
|
20
|
+
def add(name, filter_proc = nil, &block)
|
21
|
+
raise ArgumentError, "filter_proc and block are mutually exclusive" if filter_proc && block
|
22
|
+
raise ArgumentError, "filter_proc must be a Proc" unless block || filter_proc.is_a?(Proc)
|
23
|
+
synchronize do
|
24
|
+
data[name.to_sym] = Fmt::Filter.new(name.to_sym, filter_proc || block)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def each(&block)
|
29
|
+
synchronize { data.each(&block) }
|
30
|
+
end
|
31
|
+
|
32
|
+
def fetch(name, default = nil)
|
33
|
+
synchronize { data.fetch name.to_sym, default }
|
34
|
+
end
|
35
|
+
|
36
|
+
def key?(name)
|
37
|
+
synchronize { data.key? name.to_sym }
|
38
|
+
end
|
39
|
+
|
40
|
+
alias_method :added?, :key?
|
41
|
+
alias_method :include?, :key?
|
42
|
+
|
43
|
+
def merge!(other)
|
44
|
+
synchronize { data.merge! other.to_h }
|
45
|
+
self
|
46
|
+
end
|
47
|
+
|
48
|
+
def to_h
|
49
|
+
data.dup
|
50
|
+
end
|
51
|
+
|
52
|
+
protected
|
53
|
+
|
54
|
+
attr_reader :data
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "filter_group"
|
4
|
+
|
5
|
+
module Fmt
|
6
|
+
class RainbowFilterGroup < FilterGroup
|
7
|
+
def initialize
|
8
|
+
super
|
9
|
+
|
10
|
+
if defined? Rainbow
|
11
|
+
methods = Rainbow::Presenter.public_instance_methods(false).select do |method|
|
12
|
+
Rainbow::Presenter.public_instance_method(method).arity == 0
|
13
|
+
end
|
14
|
+
|
15
|
+
method_names = methods
|
16
|
+
.map { |m| m.name.to_sym }
|
17
|
+
.concat(Rainbow::X11ColorNames::NAMES.keys)
|
18
|
+
|
19
|
+
method_names.each do |name|
|
20
|
+
add(name) { |string| Rainbow(string).public_send name }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
rescue => error
|
24
|
+
puts "Error adding Rainbow filters! #{error.inspect}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "filter_group"
|
4
|
+
|
5
|
+
module Fmt
|
6
|
+
class StringFilterGroup < FilterGroup
|
7
|
+
def initialize
|
8
|
+
super
|
9
|
+
|
10
|
+
# rubocop:disable Layout/ExtraSpacing AllowForAlignment
|
11
|
+
add :capitalize, ->(s) { s.capitalize }
|
12
|
+
add :chomp, ->(s) { s.chomp }
|
13
|
+
add :chop, ->(s) { s.chop }
|
14
|
+
add :downcase, ->(s) { s.downcase }
|
15
|
+
add :lstrip, ->(s) { s.lstrip }
|
16
|
+
add :reverse, ->(s) { s.reverse }
|
17
|
+
add :rstrip, ->(s) { s.rstrip }
|
18
|
+
add :shellescape, ->(s) { s.shellescape }
|
19
|
+
add :strip, ->(s) { s.strip }
|
20
|
+
add :succ, ->(s) { s.succ }
|
21
|
+
add :swapcase, ->(s) { s.swapcase }
|
22
|
+
add :undump, ->(s) { s.undump }
|
23
|
+
add :unicode_normalize, ->(s) { s.unicode_normalize }
|
24
|
+
add :upcase, ->(s) { s.upcase }
|
25
|
+
# rubocop:enable Layout/ExtraSpacing AllowForAlignment
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
data/lib/fmt/formatter.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "singleton"
|
4
|
-
require_relative "
|
4
|
+
require_relative "filter_groups/filter_group"
|
5
|
+
require_relative "filter_groups/rainbow_filter_group"
|
6
|
+
require_relative "filter_groups/string_filter_group"
|
5
7
|
require_relative "scanners"
|
6
8
|
require_relative "transformer"
|
7
9
|
|
@@ -11,6 +13,14 @@ module Fmt
|
|
11
13
|
|
12
14
|
attr_reader :filters
|
13
15
|
|
16
|
+
def add_rainbow_filters
|
17
|
+
filters.merge! Fmt::RainbowFilterGroup.new.to_h
|
18
|
+
end
|
19
|
+
|
20
|
+
def add_filter(...)
|
21
|
+
filters.add(...)
|
22
|
+
end
|
23
|
+
|
14
24
|
def format(string, **locals)
|
15
25
|
result = string.to_s
|
16
26
|
transformer = next_transformer(result)
|
@@ -27,7 +37,7 @@ module Fmt
|
|
27
37
|
|
28
38
|
def initialize
|
29
39
|
super
|
30
|
-
@filters = Fmt::
|
40
|
+
@filters = Fmt::FilterGroup.new.merge!(Fmt::StringFilterGroup.new)
|
31
41
|
end
|
32
42
|
|
33
43
|
def next_transformer(string)
|
data/lib/fmt/transformer.rb
CHANGED
@@ -22,26 +22,20 @@ module Fmt
|
|
22
22
|
if filter.string?
|
23
23
|
begin
|
24
24
|
replacement = sprintf("%#{filter.value}", replacement)
|
25
|
-
rescue
|
26
|
-
|
27
|
-
Invalid filter!
|
28
|
-
|
29
|
-
Verify it's either a valid native filter or is registered with Fmt.
|
30
|
-
Example: Fmt.add_filter(:#{filter.name}, &block)
|
31
|
-
#{error.message}
|
25
|
+
rescue
|
26
|
+
raise Fmt::Error, <<~MSG
|
27
|
+
Invalid filter! #{filter.inspect}
|
28
|
+
Verify it has been properly registered. SEE: Fmt.add_filter(:#{filter.name}, &block)
|
32
29
|
MSG
|
33
|
-
raise Fmt::Error, message
|
34
30
|
end
|
35
31
|
elsif filter.proc?
|
36
32
|
begin
|
37
33
|
replacement = filter.value.call(replacement)
|
38
34
|
rescue => error
|
39
|
-
|
40
|
-
Error in filter!
|
41
|
-
#{filter.inspect}
|
35
|
+
raise Fmt::Error, <<~MSG
|
36
|
+
Error in filter! #{filter.inspect}
|
42
37
|
#{error.message}
|
43
38
|
MSG
|
44
|
-
raise Fmt::Error, message
|
45
39
|
end
|
46
40
|
end
|
47
41
|
end
|
data/lib/fmt/version.rb
CHANGED
data/lib/fmt.rb
CHANGED
@@ -15,11 +15,13 @@ module Fmt
|
|
15
15
|
formatter.filters
|
16
16
|
end
|
17
17
|
|
18
|
-
def
|
19
|
-
formatter.
|
18
|
+
def add_rainbow_filters
|
19
|
+
formatter.add_rainbow_filters
|
20
20
|
end
|
21
21
|
|
22
|
-
|
22
|
+
def add_filter(...)
|
23
|
+
formatter.add_filter(...)
|
24
|
+
end
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fmt
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nate Hopkins (hopsoft)
|
@@ -161,7 +161,9 @@ files:
|
|
161
161
|
- lib/fmt.rb
|
162
162
|
- lib/fmt/embed.rb
|
163
163
|
- lib/fmt/filter.rb
|
164
|
-
- lib/fmt/
|
164
|
+
- lib/fmt/filter_groups/filter_group.rb
|
165
|
+
- lib/fmt/filter_groups/rainbow_filter_group.rb
|
166
|
+
- lib/fmt/filter_groups/string_filter_group.rb
|
165
167
|
- lib/fmt/formatter.rb
|
166
168
|
- lib/fmt/scanners.rb
|
167
169
|
- lib/fmt/scanners/base_scanner.rb
|
data/lib/fmt/filters.rb
DELETED
@@ -1,76 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "monitor"
|
4
|
-
require_relative "filter"
|
5
|
-
|
6
|
-
module Fmt
|
7
|
-
class Filters
|
8
|
-
include Enumerable
|
9
|
-
include MonitorMixin
|
10
|
-
|
11
|
-
NATIVE_FILTERS = %i[
|
12
|
-
capitalize
|
13
|
-
chomp
|
14
|
-
chop
|
15
|
-
downcase
|
16
|
-
lstrip
|
17
|
-
reverse
|
18
|
-
rstrip
|
19
|
-
shellescape
|
20
|
-
strip
|
21
|
-
succ
|
22
|
-
swapcase
|
23
|
-
undump
|
24
|
-
unicode_normalize
|
25
|
-
upcase
|
26
|
-
]
|
27
|
-
|
28
|
-
def initialize
|
29
|
-
super
|
30
|
-
@entries = {}
|
31
|
-
|
32
|
-
NATIVE_FILTERS.each do |name|
|
33
|
-
add(name) { |str| str.then(&:"#{name}") }
|
34
|
-
end
|
35
|
-
|
36
|
-
if defined? Rainbow
|
37
|
-
begin
|
38
|
-
Rainbow::Presenter.public_instance_methods(false).each do |name|
|
39
|
-
next unless Rainbow::Presenter.public_instance_method(name).arity == 0
|
40
|
-
add(name) { |str| Rainbow(str).public_send(name) }
|
41
|
-
end
|
42
|
-
|
43
|
-
Rainbow::X11ColorNames::NAMES.keys.each do |name|
|
44
|
-
add(name) { |str| Rainbow(str).public_send(name) }
|
45
|
-
end
|
46
|
-
rescue => error
|
47
|
-
puts "Error adding Rainbow filters! #{error.inspect}"
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
def each(&block)
|
53
|
-
entries.each(&block)
|
54
|
-
end
|
55
|
-
|
56
|
-
def add(name, filter = nil, &block)
|
57
|
-
raise ArgumentError, "filter and block are mutually exclusive" if filter && block
|
58
|
-
raise ArgumentError, "filter must be a Proc" unless block || filter.is_a?(Proc)
|
59
|
-
entries[name.to_sym] = Filter.new(name, filter || block)
|
60
|
-
end
|
61
|
-
|
62
|
-
alias_method :<<, :add
|
63
|
-
|
64
|
-
def [](name)
|
65
|
-
synchronize { entries[name.to_sym] }
|
66
|
-
end
|
67
|
-
|
68
|
-
def fetch(name, default = nil)
|
69
|
-
synchronize { entries.fetch name.to_sym, default }
|
70
|
-
end
|
71
|
-
|
72
|
-
private
|
73
|
-
|
74
|
-
attr_reader :entries
|
75
|
-
end
|
76
|
-
end
|