fmt 0.1.0 → 0.1.1
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 +33 -6
- data/lib/fmt/filters.rb +1 -2
- data/lib/fmt/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 55fc16022e0fcb4b8815d6e7242106018e5786a9d610f38f8040cbb31cadb1a0
|
4
|
+
data.tar.gz: 185f056e8450fb1facfe15c7c637e9b49d2a19a2270689b8e12953c664393e10
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 707ed8686b167ecba644d7a2358cf179670656cd9a7eb77f6d9408b7b8d5f07a9c204ca94fcd928e8630ca5633ce716c0fb600c4bca8d8e6992a1f682fcd76dc
|
7
|
+
data.tar.gz: 4b350d2ce4dd9ceeca3b7640d392b806c9698607bae33db99f18a39248898dbeef10100040f2d78dfc66f95d78839a982cf9a1cb3391129c70347f68f043bf4d
|
data/README.md
CHANGED
@@ -9,26 +9,38 @@ I'm currenly using this to help build beautiful CLI applications with Ruby. Plus
|
|
9
9
|
## Setup
|
10
10
|
|
11
11
|
```
|
12
|
-
bundle add fmt
|
13
12
|
bundle add rainbow # optional
|
13
|
+
bundle add fmt
|
14
14
|
```
|
15
15
|
|
16
16
|
## Usage
|
17
17
|
|
18
18
|
Simply create a string with embedded formatting syntax as you'd normally do with `sprintf` or `format`.
|
19
|
-
i.e. `"%{...}"`
|
20
19
|
|
21
|
-
|
22
|
-
|
20
|
+
```ruby
|
21
|
+
"%{...}"
|
22
|
+
```
|
23
|
+
|
24
|
+
Filters can be chained after the placeholder like so.
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
"%{...}FILTER|FILTER|FILTER"
|
28
|
+
```
|
29
|
+
|
30
|
+
> [!NOTE]
|
31
|
+
> Filters are processed in the order they are specified.
|
23
32
|
|
24
|
-
|
25
|
-
|
33
|
+
Filters can be [native Ruby formatting](https://docs.ruby-lang.org/en/master/format_specifications_rdoc.html) as well as String methods like `capitalize`, `downcase`, `strip`, etc.
|
34
|
+
Also, you can use Rainbow filters like `bold`, `cyan`, `underline`, et al. if you have the [Rainbow GEM](https://github.com/ku1ik/rainbow) installed.
|
35
|
+
|
36
|
+
**You can even [register your own filters](#filters).**
|
26
37
|
|
27
38
|
### Rendering
|
28
39
|
|
29
40
|
Basic example:
|
30
41
|
|
31
42
|
```ruby
|
43
|
+
require "rainbow"
|
32
44
|
require "fmt"
|
33
45
|
|
34
46
|
template = "Hello %{name}cyan|bold"
|
@@ -36,9 +48,12 @@ result = Fmt(template, name: "World")
|
|
36
48
|
#=> "Hello \e[36m\e[1mWorld\e[0m"
|
37
49
|
```
|
38
50
|
|
51
|
+

|
52
|
+
|
39
53
|
Mix and match native formatting with Rainbow formatting:
|
40
54
|
|
41
55
|
```ruby
|
56
|
+
require "rainbow"
|
42
57
|
require "fmt"
|
43
58
|
|
44
59
|
template = "Date: %{date}.10s|magenta"
|
@@ -46,9 +61,14 @@ result = Fmt(template, date: Time.now)
|
|
46
61
|
#=> "Date: \e[35m2024-07-26\e[0m"
|
47
62
|
```
|
48
63
|
|
64
|
+

|
65
|
+
|
49
66
|
Multiline example:
|
50
67
|
|
51
68
|
```ruby
|
69
|
+
require "rainbow"
|
70
|
+
require "fmt"
|
71
|
+
|
52
72
|
template = <<~T
|
53
73
|
Date: %{date}.10s|underline
|
54
74
|
|
@@ -61,12 +81,17 @@ result = Fmt(template, date: Time.now, name: "Hopsoft", message: "This is neat!"
|
|
61
81
|
#=> "Date: \e[4m2024-07-26\e[0m\n\nGreetings, \e[1mHOPSOFT\e[0m\n\n\e[32mThis is neat!\e[0m\n"
|
62
82
|
```
|
63
83
|
|
84
|
+

|
85
|
+
|
64
86
|
### Filters
|
65
87
|
|
66
88
|
You can also add your own filters to Fmt by calling `Fmt.add_filter(:name, &block)`.
|
67
89
|
The block accepts a string and should return a replacement string.
|
68
90
|
|
69
91
|
```ruby
|
92
|
+
require "rainbow"
|
93
|
+
require "fmt"
|
94
|
+
|
70
95
|
Fmt.add_filter(:repeat20) { |str| str * 20 }
|
71
96
|
|
72
97
|
template = <<~T
|
@@ -78,3 +103,5 @@ T
|
|
78
103
|
result = Fmt(template, head: "#", message: "Give it a try!", tail: "#")
|
79
104
|
#=> "\e[2m####################\e[0m\n\e[1mGive it a try!\e[0m\n\e[2m####################\e[0m\n"
|
80
105
|
```
|
106
|
+
|
107
|
+

|
data/lib/fmt/filters.rb
CHANGED
@@ -17,7 +17,6 @@ module Fmt
|
|
17
17
|
downcase
|
18
18
|
lstrip
|
19
19
|
reverse
|
20
|
-
rjust
|
21
20
|
rstrip
|
22
21
|
shellescape
|
23
22
|
strip
|
@@ -38,7 +37,7 @@ module Fmt
|
|
38
37
|
|
39
38
|
if defined? Rainbow
|
40
39
|
Rainbow::Presenter.public_instance_methods(false).each do |name|
|
41
|
-
next unless Rainbow::Presenter.
|
40
|
+
next unless Rainbow::Presenter.public_instance_method(name).arity == 0
|
42
41
|
add(name) { |str| Rainbow(str).public_send(name) }
|
43
42
|
end
|
44
43
|
|
data/lib/fmt/version.rb
CHANGED