colsole 0.3.2 → 0.3.3

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