colsole 0.1.6 → 0.2.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/lib/colsole.rb +38 -49
- 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: d67b1de96558e878ebcca6575739a8ebf21859c1
|
4
|
+
data.tar.gz: 177aa67473468527ee9cf9130a434cf03ec71f50
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1fd6648989d636eea3ac9f665e24ca06b4bfe5654ed98ed51376cd40e3f0798563b529b179f7c0488ce6be60e53598aaeeec5235463967c55dfc889d00cc7b1
|
7
|
+
data.tar.gz: 9a398e1d8bbafea7c87f2459524791d2227664d09f1b7b35f559b8cbe605425b9cfed7ec5d4e4337519d958e0feb16ea34735e98b9b42854d66b33c8a0bb39f9
|
data/lib/colsole.rb
CHANGED
@@ -1,17 +1,16 @@
|
|
1
|
-
#
|
1
|
+
# Colsole - Coloful Console Applications
|
2
2
|
#
|
3
|
-
#
|
4
|
-
#
|
3
|
+
# This class provides several utility functions for console
|
4
|
+
# appliucation developers.
|
5
5
|
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
#
|
6
|
+
# - #colorize string - return a colorized strings
|
7
|
+
# - #say string - print a string with colors
|
8
|
+
# - #resay string - same as say, but overwrite current line
|
9
|
+
# - #word_wrap string - wrap a string and maintain indentation
|
10
|
+
# - #detect_terminal_size
|
11
11
|
#
|
12
|
-
#
|
13
|
-
#
|
14
|
-
# color mapping by ondrovic http://www.backtrack-linux.org/forums/showthread.php?t=29691&s=f681fb882b13be26ee726e5f335d089e
|
12
|
+
# Credits:
|
13
|
+
# terminal width detection by Gabrial Horner https://github.com/cldwalker
|
15
14
|
|
16
15
|
module Colsole
|
17
16
|
|
@@ -71,41 +70,7 @@ module Colsole
|
|
71
70
|
# Respects pipe and auto terminates colored strings.
|
72
71
|
# Call without text to see a list/demo of all available colors.
|
73
72
|
def colorize(text=nil, force_color=false)
|
74
|
-
colors =
|
75
|
-
'txtblk' => '[0;30m', # Black - Regular
|
76
|
-
'txtred' => '[0;31m', # Red
|
77
|
-
'txtgrn' => '[0;32m', # Green
|
78
|
-
'txtylw' => '[0;33m', # Yellow
|
79
|
-
'txtblu' => '[0;34m', # Blue
|
80
|
-
'txtpur' => '[0;35m', # Purple
|
81
|
-
'txtcyn' => '[0;36m', # Cyan
|
82
|
-
'txtwht' => '[0;37m', # White
|
83
|
-
'bldblk' => '[1;30m', # Black - Bold
|
84
|
-
'bldred' => '[1;31m', # Red
|
85
|
-
'bldgrn' => '[1;32m', # Green
|
86
|
-
'bldylw' => '[1;33m', # Yellow
|
87
|
-
'bldblu' => '[1;34m', # Blue
|
88
|
-
'bldpur' => '[1;35m', # Purple
|
89
|
-
'bldcyn' => '[1;36m', # Cyan
|
90
|
-
'bldwht' => '[1;37m', # White
|
91
|
-
'unkblk' => '[4;30m', # Black - Underline
|
92
|
-
'undred' => '[4;31m', # Red
|
93
|
-
'undgrn' => '[4;32m', # Green
|
94
|
-
'undylw' => '[4;33m', # Yellow
|
95
|
-
'undblu' => '[4;34m', # Blue
|
96
|
-
'undpur' => '[4;35m', # Purple
|
97
|
-
'undcyn' => '[4;36m', # Cyan
|
98
|
-
'undwht' => '[4;37m', # White
|
99
|
-
'bakblk' => '[40m' , # Black - Background
|
100
|
-
'bakred' => '[41m' , # Red
|
101
|
-
'bakgrn' => '[42m' , # Green
|
102
|
-
'bakylw' => '[43m' , # Yellow
|
103
|
-
'bakblu' => '[44m' , # Blue
|
104
|
-
'bakpur' => '[45m' , # Purple
|
105
|
-
'bakcyn' => '[46m' , # Cyan
|
106
|
-
'bakwht' => '[47m' , # White
|
107
|
-
'txtrst' => '[0m' , # Text Reset
|
108
|
-
}
|
73
|
+
colors = prepare_colors
|
109
74
|
|
110
75
|
if text.nil? # Demo
|
111
76
|
i=33;
|
@@ -116,14 +81,13 @@ module Colsole
|
|
116
81
|
return
|
117
82
|
end
|
118
83
|
|
119
|
-
|
120
|
-
reset = esc + colors['txtrst']
|
84
|
+
reset = colors['txtrst']
|
121
85
|
if is_terminal or force_color
|
122
86
|
reset_called_last = true
|
123
87
|
|
124
88
|
out = text.gsub(/\!([a-z]{6})\!/) do |m|
|
125
89
|
reset_called_last = $1 == "txtrst";
|
126
|
-
|
90
|
+
colors[$1];
|
127
91
|
end
|
128
92
|
reset_called_last or out = "#{out}#{reset}";
|
129
93
|
else
|
@@ -132,6 +96,31 @@ module Colsole
|
|
132
96
|
|
133
97
|
return out
|
134
98
|
end
|
99
|
+
|
100
|
+
private
|
101
|
+
|
102
|
+
# Create a colors array with keys such as :green and :bld_green
|
103
|
+
# and values which are the escape codes for the colors.
|
104
|
+
def prepare_colors
|
105
|
+
esc = 27.chr
|
106
|
+
# pattern_full = "#{esc}[%{decor};%{fg};%{bg}m"
|
107
|
+
pattern_fg = "#{esc}[%{decor};%{fg}m"
|
108
|
+
pattern_reset = "#{esc}[0m"
|
109
|
+
|
110
|
+
decors = { txt: 0, bld: 1, und: 4, rev: 7 }
|
111
|
+
color_codes = { blk: 0, red: 1, grn: 2, ylw: 3, blu: 4, pur: 5, cyn: 6, wht: 7 }
|
112
|
+
colors = {}
|
113
|
+
|
114
|
+
decors.each do |dk, dv|
|
115
|
+
color_codes.each do |ck, cv|
|
116
|
+
key = "#{dk}#{ck}"
|
117
|
+
val = pattern_fg % { decor: dv, fg: "3#{cv}" }
|
118
|
+
colors[key] = val
|
119
|
+
end
|
120
|
+
end
|
121
|
+
colors['txtrst'] = pattern_reset
|
122
|
+
colors
|
123
|
+
end
|
135
124
|
end
|
136
125
|
|
137
126
|
self.extend Colsole
|
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: 0.2.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: 2015-05-
|
11
|
+
date: 2015-05-17 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Utility functions for making colorful console applications
|
14
14
|
email: db@dannyben.com
|