colsole 0.3.2 → 0.3.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 +10 -5
- data/lib/colsole/version.rb +1 -1
- data/lib/colsole.rb +124 -123
- metadata +10 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b6de4cbd3ede2c98135cbe010b74a600f8dc5e85
|
4
|
+
data.tar.gz: 1982bb0999e283100ad40997c096331e6005ee16
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b066d600d0d33f8ad0114ce350cf401a5f86d45bb6ac78fa734269a49a70d4823bb914692edbfca6ab895de4ea69dac271654a7adb623434b3e130d9f4bb1807
|
7
|
+
data.tar.gz: 18544d16a2ed90c759003b22b38083a744b69117e6fc4305734147dd14c5262a3ca6f4d699fb49390ee7d9ca5ad3569f3b7cef1ec14a28fde802796712c9c2ab
|
data/README.md
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
Colsole
|
2
|
-
|
2
|
+
==================================================
|
3
3
|
|
4
4
|
[![Gem Version](https://badge.fury.io/rb/colsole.svg)](http://badge.fury.io/rb/colsole)
|
5
5
|
[![Build Status](https://travis-ci.org/DannyBen/colsole.svg?branch=master)](https://travis-ci.org/DannyBen/colsole)
|
@@ -9,11 +9,15 @@ Colsole
|
|
9
9
|
|
10
10
|
Utility functions for colorful console applications.
|
11
11
|
|
12
|
-
|
12
|
+
Install
|
13
|
+
--------------------------------------------------
|
13
14
|
|
14
|
-
|
15
|
+
```
|
16
|
+
$ gem install colsole
|
17
|
+
```
|
15
18
|
|
16
|
-
|
19
|
+
Primary Functions
|
20
|
+
--------------------------------------------------
|
17
21
|
|
18
22
|
### `say "anything"`
|
19
23
|
|
@@ -70,7 +74,8 @@ Use say! to output to stderr with color markers:
|
|
70
74
|
say! "!txtred!Error!txtrst!: This just did not work"
|
71
75
|
```
|
72
76
|
|
73
|
-
|
77
|
+
Utility / Support Functions
|
78
|
+
--------------------------------------------------
|
74
79
|
|
75
80
|
### `colorize "!txtred!Hello"`
|
76
81
|
|
data/lib/colsole/version.rb
CHANGED
data/lib/colsole.rb
CHANGED
@@ -7,6 +7,7 @@ require "colsole/version"
|
|
7
7
|
#
|
8
8
|
# - #colorize string - return a colorized strings
|
9
9
|
# - #say string - print a string with colors
|
10
|
+
# - #say! string - print a string with colors to stderr
|
10
11
|
# - #resay string - same as say, but overwrite current line
|
11
12
|
# - #word_wrap string - wrap a string and maintain indentation
|
12
13
|
# - #detect_terminal_size
|
@@ -15,129 +16,129 @@ require "colsole/version"
|
|
15
16
|
# terminal width detection by Gabrial Horner https://github.com/cldwalker
|
16
17
|
|
17
18
|
module Colsole
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
19
|
+
# Prints a color-flagged string.
|
20
|
+
# Use color flags (like !txtred!) to change color in the string.
|
21
|
+
# Space terminated strings will leave the cursor at the same line.
|
22
|
+
def say(text, force_color=false)
|
23
|
+
last = text[-1, 1]
|
24
|
+
if last == ' ' or last == '\t'
|
25
|
+
print colorize(text, force_color)
|
26
|
+
else
|
27
|
+
print colorize("#{text}\n", force_color)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Prints a color-flagged string to STDERR
|
32
|
+
# Use color flags (like !txtred!) to change color in the string.
|
33
|
+
def say!(text, force_color=false)
|
34
|
+
$stderr.puts colorize(text, force_color, :stderr)
|
35
|
+
end
|
36
|
+
|
37
|
+
# Erase the current output line, and say a new string.
|
38
|
+
# This should be used after a space terminated say().
|
39
|
+
def resay(text, force_color=false)
|
40
|
+
terminal? and text = "\033[2K\r#{text}"
|
41
|
+
say text, force_color
|
42
|
+
end
|
43
|
+
|
44
|
+
# Returns true if stdout/stderr is interactive terminal
|
45
|
+
def terminal?(stream=:stdout)
|
46
|
+
stream == :stdout ? out_terminal? : err_terminal?
|
47
|
+
end
|
48
|
+
|
49
|
+
# Returns true if stdout is interactive terminal
|
50
|
+
def out_terminal?
|
51
|
+
STDOUT.tty?
|
52
|
+
end
|
53
|
+
|
54
|
+
# Returns true if stderr is interactive terminal
|
55
|
+
def err_terminal?
|
56
|
+
STDERR.tty?
|
57
|
+
end
|
58
|
+
|
59
|
+
# Determines if a shell command exists.
|
60
|
+
def command_exist?(command)
|
61
|
+
ENV['PATH'].split(File::PATH_SEPARATOR).any? {|d| File.exist? File.join(d, command) }
|
62
|
+
end
|
63
|
+
|
64
|
+
# Returns [width, height] of terminal when detected, or a default
|
65
|
+
# value otherwise.
|
66
|
+
def detect_terminal_size(default=80)
|
67
|
+
if (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/)
|
68
|
+
[ENV['COLUMNS'].to_i, ENV['LINES'].to_i]
|
69
|
+
elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV['TERM'])) && command_exist?('tput')
|
70
|
+
[`tput cols`.to_i, `tput lines`.to_i]
|
71
|
+
elsif STDIN.tty? && command_exist?('stty')
|
72
|
+
`stty size`.scan(/\d+/).map { |s| s.to_i }.reverse
|
73
|
+
else
|
74
|
+
default
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Converts a long string to be wrapped keeping words in tact.
|
79
|
+
# If the string starts with one or more spaces, they will be
|
80
|
+
# preserved in all subsequent lines (i.e., remain indented).
|
81
|
+
def word_wrap(text, length=78)
|
82
|
+
lead = text[/^\s*/]
|
83
|
+
length -= lead.size
|
84
|
+
text.gsub(/(.{1,#{length}}\n?)(\s+|\Z)/, "\\1\n#{lead}").rstrip
|
85
|
+
end
|
86
|
+
|
87
|
+
# Parses and returns a color-flagged string.
|
88
|
+
# Respects pipe and auto terminates colored strings.
|
89
|
+
# Call without text to see a list/demo of all available colors.
|
90
|
+
def colorize(text=nil, force_color=false, stream=:stdout)
|
91
|
+
colors = prepare_colors
|
92
|
+
|
93
|
+
if text.nil? # Demo
|
94
|
+
i=33;
|
95
|
+
colors.each do |k,v|
|
96
|
+
puts colorize "#{k} = !#{k}! #{i} bottles of beer on the wall !txtrst!"
|
97
|
+
i -= 1
|
98
|
+
end
|
99
|
+
return
|
100
|
+
end
|
101
|
+
|
102
|
+
reset = colors['txtrst']
|
103
|
+
if terminal?(stream) or force_color
|
104
|
+
reset_called_last = true
|
105
|
+
|
106
|
+
out = text.gsub(/\!([a-z]{6})\!/) do |m|
|
107
|
+
reset_called_last = $1 == "txtrst";
|
108
|
+
colors[$1];
|
109
|
+
end
|
110
|
+
reset_called_last or out = "#{out}#{reset}";
|
111
|
+
else
|
112
|
+
out = text.gsub(/\!([a-z]{6})\!/, '')
|
113
|
+
end
|
114
|
+
|
115
|
+
return out
|
116
|
+
end
|
117
|
+
|
118
|
+
private
|
119
|
+
|
120
|
+
# Create a colors array with keys such as :green and :bld_green
|
121
|
+
# and values which are the escape codes for the colors.
|
122
|
+
def prepare_colors
|
123
|
+
esc = 27.chr
|
124
|
+
# pattern_full = "#{esc}[%{decor};%{fg};%{bg}m"
|
125
|
+
pattern_fg = "#{esc}[%{decor};%{fg}m"
|
126
|
+
pattern_reset = "#{esc}[0m"
|
127
|
+
|
128
|
+
decors = { txt: 0, bld: 1, und: 4, rev: 7 }
|
129
|
+
color_codes = { blk: 0, red: 1, grn: 2, ylw: 3, blu: 4, pur: 5, cyn: 6, wht: 7 }
|
130
|
+
colors = {}
|
131
|
+
|
132
|
+
decors.each do |dk, dv|
|
133
|
+
color_codes.each do |ck, cv|
|
134
|
+
key = "#{dk}#{ck}"
|
135
|
+
val = pattern_fg % { decor: dv, fg: "3#{cv}" }
|
136
|
+
colors[key] = val
|
137
|
+
end
|
138
|
+
end
|
139
|
+
colors['txtrst'] = pattern_reset
|
140
|
+
colors
|
141
|
+
end
|
141
142
|
end
|
142
143
|
|
143
144
|
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.3.
|
4
|
+
version: 0.3.3
|
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:
|
11
|
+
date: 2016-02-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: runfile
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '0.
|
19
|
+
version: '0.6'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '0.
|
26
|
+
version: '0.6'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: runfile-tasks
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0.
|
33
|
+
version: '0.4'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0.
|
40
|
+
version: '0.4'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: minitest
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -66,34 +66,20 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '1.1'
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: rake
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - "~>"
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '10.4'
|
76
|
-
type: :development
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - "~>"
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '10.4'
|
83
69
|
- !ruby/object:Gem::Dependency
|
84
70
|
name: simplecov
|
85
71
|
requirement: !ruby/object:Gem::Requirement
|
86
72
|
requirements:
|
87
73
|
- - "~>"
|
88
74
|
- !ruby/object:Gem::Version
|
89
|
-
version: '0.
|
75
|
+
version: '0.11'
|
90
76
|
type: :development
|
91
77
|
prerelease: false
|
92
78
|
version_requirements: !ruby/object:Gem::Requirement
|
93
79
|
requirements:
|
94
80
|
- - "~>"
|
95
81
|
- !ruby/object:Gem::Version
|
96
|
-
version: '0.
|
82
|
+
version: '0.11'
|
97
83
|
description: Utility functions for making colorful console applications
|
98
84
|
email: db@dannyben.com
|
99
85
|
executables: []
|
@@ -115,7 +101,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
115
101
|
requirements:
|
116
102
|
- - ">="
|
117
103
|
- !ruby/object:Gem::Version
|
118
|
-
version:
|
104
|
+
version: 2.0.0
|
119
105
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
106
|
requirements:
|
121
107
|
- - ">="
|