colsole 0.8.1 → 1.0.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 +37 -25
- data/lib/colsole/version.rb +1 -1
- data/lib/colsole.rb +4 -11
- metadata +4 -5
- data/lib/colsole/compat.rb +0 -59
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 832d466f34ab604a7a9cc7b4e92b94aea79fc4fd52e6383c767bf17483be5d7b
|
4
|
+
data.tar.gz: ceb677bb2f0b6125e2f80717709bbb65d4c9b43bc9bf00d994f4c337a0317536
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5aa0974a9282c9c3140ae0e16e2b8dfaec4b846cea832a2c46f5d35cd5200ce76d93ebb3bad5f24e785489af1143b3da20acb4f9e8f09e2442b01c90a52fbe54
|
7
|
+
data.tar.gz: 957dc4bf62fd4fcecc0d11e7a2bee220b03e75d0283bdc6d123026fc117b305c2d36b66a9a83b81fbe5f44547aade7cd0ac6dd5cb45ea4bd221cb02c10a0c446
|
data/README.md
CHANGED
@@ -9,10 +9,10 @@
|
|
9
9
|
Utility functions for colorful console applications.
|
10
10
|
|
11
11
|
> **Upgrade Note**
|
12
|
-
>
|
13
|
-
>
|
14
|
-
>
|
15
|
-
>
|
12
|
+
>
|
13
|
+
> - Version 1.0.x is not compatible with older versions
|
14
|
+
> - Version 0.8.x is compatible with both the old syntax and new syntax
|
15
|
+
>
|
16
16
|
> See [Uprading](#upgrading) below.
|
17
17
|
|
18
18
|
## Install
|
@@ -23,6 +23,21 @@ Add to your Gemfile:
|
|
23
23
|
$ gem 'colsole', '>= 0.8.1', '< 2.0'
|
24
24
|
```
|
25
25
|
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
require 'colsole'
|
30
|
+
include Colsole
|
31
|
+
say 'b`Blue` Man Group'
|
32
|
+
```
|
33
|
+
|
34
|
+
All the methods described below can also be called directly on the `Colsole` module. This is useful when you want to use it at the top level of your project, without namespace contamination:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
require 'colsole'
|
38
|
+
Colsole.say 'b`Blue` Man Group'
|
39
|
+
```
|
40
|
+
|
26
41
|
## Examples
|
27
42
|
|
28
43
|
See the [Examples file](https://github.com/DannyBen/colsole/blob/master/example.rb).
|
@@ -60,7 +75,6 @@ say "downloading data... "
|
|
60
75
|
say "download complete.", replace: true
|
61
76
|
```
|
62
77
|
|
63
|
-
|
64
78
|
### `word_wrap " string" [, length]`
|
65
79
|
|
66
80
|
Wrap long lines while keeping words intact, and keeping indentation based on the
|
@@ -111,7 +125,6 @@ fallback if it is unable to detect.
|
|
111
125
|
Returns only the terminal width or height. This is a shortcut to
|
112
126
|
`terminal_size[0]` / terminal_size[1].
|
113
127
|
|
114
|
-
|
115
128
|
## Colors
|
116
129
|
|
117
130
|
Strings that are surrounded by backticks, and preceded by a color code and
|
@@ -123,24 +136,24 @@ say "this is b`blue` and ru`this is red underlined`"
|
|
123
136
|
|
124
137
|
The one letter color code is required, followed by up to 3 style code.
|
125
138
|
|
126
|
-
| Color Code | Color
|
127
|
-
|
128
|
-
| `n` | no color
|
129
|
-
| `k` | black
|
130
|
-
| `r` | red
|
131
|
-
| `g` | green
|
132
|
-
| `y` | yellow
|
133
|
-
| `b` | blue
|
134
|
-
| `m` | magenta
|
135
|
-
| `c` | cyan
|
136
|
-
| `w` | white
|
137
|
-
|
138
|
-
| Style Code | Style
|
139
|
-
|
140
|
-
| `b` | bold
|
141
|
-
| `u` | underlined
|
142
|
-
| `i` | inverted
|
143
|
-
| `z` | terminate
|
139
|
+
| Color Code | Color |
|
140
|
+
| ---------- | -------- |
|
141
|
+
| `n` | no color |
|
142
|
+
| `k` | black |
|
143
|
+
| `r` | red |
|
144
|
+
| `g` | green |
|
145
|
+
| `y` | yellow |
|
146
|
+
| `b` | blue |
|
147
|
+
| `m` | magenta |
|
148
|
+
| `c` | cyan |
|
149
|
+
| `w` | white |
|
150
|
+
|
151
|
+
| Style Code | Style |
|
152
|
+
| ---------- | ---------- |
|
153
|
+
| `b` | bold |
|
154
|
+
| `u` | underlined |
|
155
|
+
| `i` | inverted |
|
156
|
+
| `z` | terminate |
|
144
157
|
|
145
158
|
## Upgrading
|
146
159
|
|
@@ -150,7 +163,6 @@ markers. For easy transition, it is compatible with older versions.
|
|
150
163
|
Follow these steps to upgrade:
|
151
164
|
|
152
165
|
```ruby
|
153
|
-
|
154
166
|
# => Require a more flexible version
|
155
167
|
# change this
|
156
168
|
gem 'colsole'
|
data/lib/colsole/version.rb
CHANGED
data/lib/colsole.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'io/console'
|
2
|
-
require 'colsole/compat'
|
3
2
|
|
4
3
|
# Utility functions for colorful console applications.
|
5
4
|
module Colsole
|
@@ -22,6 +21,8 @@ module Colsole
|
|
22
21
|
'z' => "\e[0m", # terminate
|
23
22
|
}
|
24
23
|
|
24
|
+
module_function
|
25
|
+
|
25
26
|
# Output a string with optional color markers to stdout.
|
26
27
|
# If text is ended with a white space, you can call again with replace: true
|
27
28
|
# to replace that line
|
@@ -103,24 +104,16 @@ module Colsole
|
|
103
104
|
|
104
105
|
# Convert color markers to ansi colors.
|
105
106
|
def colorize(string)
|
106
|
-
|
107
|
-
compat_string = old_colorize string
|
108
|
-
|
109
|
-
process_color_markers compat_string do |color, styles, text|
|
107
|
+
process_color_markers string do |color, styles, text|
|
110
108
|
"#{styles}#{color}#{text}#{ANSI_STYLES['z']}"
|
111
109
|
end
|
112
110
|
end
|
113
111
|
|
114
112
|
# Remove color markers.
|
115
113
|
def strip_colors(string)
|
116
|
-
|
117
|
-
compat_string = old_strip_colors string
|
118
|
-
|
119
|
-
process_color_markers(compat_string) { |_color, _styles, text| text }
|
114
|
+
process_color_markers(string) { |_color, _styles, text| text }
|
120
115
|
end
|
121
116
|
|
122
|
-
private
|
123
|
-
|
124
117
|
def process_color_markers(string)
|
125
118
|
string.gsub(/([rgybmcn])([ubi]{0,3})`([^`]*)`/) do
|
126
119
|
color = ANSI_COLORS[$1]
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: colsole
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Danny Ben Shitrit
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-01-
|
11
|
+
date: 2023-01-30 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Utility functions for making colorful console applications
|
14
14
|
email: db@dannyben.com
|
@@ -18,7 +18,6 @@ extra_rdoc_files: []
|
|
18
18
|
files:
|
19
19
|
- README.md
|
20
20
|
- lib/colsole.rb
|
21
|
-
- lib/colsole/compat.rb
|
22
21
|
- lib/colsole/version.rb
|
23
22
|
homepage: https://github.com/DannyBen/colsole
|
24
23
|
licenses:
|
@@ -33,14 +32,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
33
32
|
requirements:
|
34
33
|
- - ">="
|
35
34
|
- !ruby/object:Gem::Version
|
36
|
-
version: 2.
|
35
|
+
version: '2.7'
|
37
36
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
37
|
requirements:
|
39
38
|
- - ">="
|
40
39
|
- !ruby/object:Gem::Version
|
41
40
|
version: '0'
|
42
41
|
requirements: []
|
43
|
-
rubygems_version: 3.4.
|
42
|
+
rubygems_version: 3.4.5
|
44
43
|
signing_key:
|
45
44
|
specification_version: 4
|
46
45
|
summary: Colorful Console Applications
|
data/lib/colsole/compat.rb
DELETED
@@ -1,59 +0,0 @@
|
|
1
|
-
# This file contains methods that are called by the main Colsole module
|
2
|
-
# for compatibility with older versions of colsole.
|
3
|
-
# Do not use these methods directly.
|
4
|
-
module Colsole
|
5
|
-
def detect_terminal_size(*args)
|
6
|
-
terminal_size(*args)
|
7
|
-
end
|
8
|
-
|
9
|
-
def old_colorize(text)
|
10
|
-
reset = colors['txtrst']
|
11
|
-
reset_called_last = true
|
12
|
-
|
13
|
-
out = text.gsub(/!([a-z]{6})!/) do
|
14
|
-
reset_called_last = $1 == 'txtrst'
|
15
|
-
colors[$1]
|
16
|
-
end
|
17
|
-
|
18
|
-
reset_called_last or out = "#{out}#{reset}"
|
19
|
-
out
|
20
|
-
end
|
21
|
-
|
22
|
-
def old_strip_colors(text)
|
23
|
-
text.gsub(/!([a-z]{6})!/, '')
|
24
|
-
end
|
25
|
-
|
26
|
-
def resay(text)
|
27
|
-
say text, replace: true
|
28
|
-
end
|
29
|
-
|
30
|
-
def say_status(status, message = nil, color = nil)
|
31
|
-
color ||= (message ? :txtgrn : :txtblu)
|
32
|
-
say "!#{color}!#{status.to_s.rjust 12} !txtrst! #{message}".strip
|
33
|
-
end
|
34
|
-
|
35
|
-
def colors
|
36
|
-
@colors ||= begin
|
37
|
-
esc = 27.chr
|
38
|
-
pattern = "#{esc}[%{decor};%{fg}m"
|
39
|
-
|
40
|
-
decors = { txt: 0, bld: 1, und: 4, rev: 7 }
|
41
|
-
color_codes = { blk: 0, red: 1, grn: 2, ylw: 3, blu: 4, pur: 5, cyn: 6, wht: 7 }
|
42
|
-
colors = {}
|
43
|
-
|
44
|
-
decors.each do |dk, dv|
|
45
|
-
color_codes.each do |ck, cv|
|
46
|
-
key = "#{dk}#{ck}"
|
47
|
-
val = pattern % { decor: dv, fg: "3#{cv}" }
|
48
|
-
colors[key] = val
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
colors['txtbld'] = "#{esc}[1m"
|
53
|
-
colors['txtund'] = "#{esc}[4m"
|
54
|
-
colors['txtrev'] = "#{esc}[7m"
|
55
|
-
colors['txtrst'] = "#{esc}[0m"
|
56
|
-
colors
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|