colorlog 0.0.2 → 0.0.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 (2) hide show
  1. data/bin/colorlog +65 -23
  2. metadata +6 -7
data/bin/colorlog CHANGED
@@ -2,32 +2,74 @@
2
2
 
3
3
  require 'optparse'
4
4
  require 'rainbow'
5
+ require 'yaml'
6
+
7
+ options = {
8
+ success_color: :green,
9
+ warning_color: :yellow,
10
+ error_color: :red,
11
+ default_color: :default,
12
+ success: /success/i,
13
+ warning: /warning/i,
14
+ error: /^(?:(?!\bTests\b).)*(?:failure|error).*$/i
15
+
16
+ }
17
+ CONFIG_FILE = File.join(ENV['HOME'], '.colorlog.rc.yaml')
18
+ if File.exists? CONFIG_FILE
19
+ config_options = YAML.load_file(CONFIG_FILE)
20
+ options.merge!(config_options)
21
+ else
22
+ File.open(CONFIG_FILE, 'w') { |file| YAML::dump(options, file) }
23
+ STDERR.puts "Initialized configuration file in #{CONFIG_FILE}"
24
+ end
5
25
 
6
- options = {}
7
26
  option_parser = OptionParser.new do |opts|
8
27
  executable_name = File.basename($PROGRAM_NAME)
9
28
  opts.banner = "Read a file and color the lines in " +
10
- "green".foreground(:green) + ", " +
11
- "yellow".foreground(:yellow) + " or " +
12
- "red".foreground(:red) + " based on the content of the line
29
+ "different color depending on the content of the line matching different Regexp.
13
30
 
14
31
  Usage: #{executable_name} [options] filename
15
32
 
16
33
  "
17
34
 
18
- options[:green] = "success"
19
- opts.on('-g', '--green RegExp', 'RegExp that matched will turn the line green') do |green|
20
- options[:green] = green
35
+ COLORS = [
36
+ :black,
37
+ :red,
38
+ :green,
39
+ :yellow,
40
+ :blue,
41
+ :magenta,
42
+ :cyan,
43
+ :white,
44
+ :default
45
+ ]
46
+
47
+ opts.on('--success-color COLOR', COLORS, 'color to use when success matches ' + COLORS.map { |color| color.id2name }.to_s) do |success_color|
48
+ options[:success_color] = success_color
49
+ end
50
+
51
+ opts.on('--warning-color COLOR', COLORS, 'color to use when warning matches ' + COLORS.map { |color| color.id2name }.to_s) do |warning_color|
52
+ options[:warning_color] = warning_color
53
+ end
54
+
55
+ opts.on('--error-color COLOR', COLORS, 'color to use when error matches ' + COLORS.map { |color| color.id2name }.to_s) do |error_color|
56
+ options[:error_color] = error_color
57
+ end
58
+
59
+ opts.on('--default-color COLOR', COLORS, 'color to use when no match is found ' + COLORS.map { |color| color.id2name }.to_s) do |default_color|
60
+ options[:default_color] = default_color
61
+ end
62
+
63
+ opts.on('-s', '--success REGEXP', Regexp, 'RegExp that matched will turn the line green (default ' + options[:success].inspect + ')') do |success|
64
+ options[:success] = success
21
65
  end
22
66
 
23
- options[:yellow] = "warning"
24
- opts.on('-y','--yellow RegExp', 'RegExp that matched will turn the line yellow') do |yellow|
25
- options[:yellow] = yellow
67
+ opts.on('-w', '--warning REGEXP', Regexp, 'RegExp that matched will turn the line yellow (default ' + options[:warning].inspect + ')') do |warning|
68
+ options[:warning] = warning
26
69
  end
27
70
 
28
- options[:red] = /^(?:(?!\bTests\b).)*(?:failure|error).*$/i
29
- opts.on('-r', '--red RedExp', 'RegExp that matched will turn the line red') do |red|
30
- options[:red] = red
71
+ opts.on('-e', '--error REGEXP', Regexp, 'RegExp that matched will turn the line red (default ' + options[:error].inspect + ')') do |error|
72
+ options[:error] = error
31
73
  end
32
74
 
33
75
  end
@@ -36,20 +78,20 @@ option_parser.parse!
36
78
 
37
79
  def color_print(line, options)
38
80
 
39
- success_check = Regexp.new(options[:green], Regexp::IGNORECASE)
40
- warning_check = Regexp.new(options[:yellow], Regexp::IGNORECASE)
41
- error_check = Regexp.new(options[:red])
81
+ success_check = Regexp.new(options[:success])
82
+ warning_check = Regexp.new(options[:warning])
83
+ error_check = Regexp.new(options[:error])
42
84
 
43
- color = :default
85
+ color = options[:default_color]
44
86
 
45
87
  if line =~ success_check
46
- color = :green
88
+ color = options[:success_color]
47
89
  end
48
90
  if line =~ warning_check
49
- color = :yellow
91
+ color = options[:warning_color]
50
92
  end
51
93
  if line =~ error_check
52
- color = :red
94
+ color = options[:error_color]
53
95
  end
54
96
  printf("%s".foreground(color), line)
55
97
 
@@ -57,19 +99,19 @@ end
57
99
 
58
100
  filename = ARGV.shift
59
101
 
60
- if (filename.nil?)
102
+ if filename.nil?
61
103
  if STDIN.tty?
62
104
  puts "You should provide a file"
63
105
  exit 1
64
106
  end
65
107
 
66
108
  ARGF.each do |line|
67
- color_print(line,options)
109
+ color_print(line, options)
68
110
  end
69
111
  else
70
112
  File.open(File.expand_path(filename), 'r') do |file|
71
113
  file.readlines.each do |line|
72
- color_print(line,options)
114
+ color_print(line, options)
73
115
  end
74
116
  end
75
117
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: colorlog
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-08-12 00:00:00.000000000 Z
12
+ date: 2012-09-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rainbow
@@ -59,10 +59,9 @@ dependencies:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
61
  version: '3.12'
62
- description: colorlog allow you to read a file with sone lines colored in red, green
62
+ description: colorlog allow you to read a file with some lines colored in red, green
63
63
  and yellow based on regexp
64
- email:
65
- - nicolas at hurion.eu
64
+ email:
66
65
  executables:
67
66
  - colorlog
68
67
  extensions: []
@@ -83,7 +82,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
83
82
  version: '0'
84
83
  segments:
85
84
  - 0
86
- hash: -2189919106220471362
85
+ hash: -1041857608372163492
87
86
  required_rubygems_version: !ruby/object:Gem::Requirement
88
87
  none: false
89
88
  requirements:
@@ -92,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
91
  version: '0'
93
92
  segments:
94
93
  - 0
95
- hash: -2189919106220471362
94
+ hash: -1041857608372163492
96
95
  requirements: []
97
96
  rubyforge_project:
98
97
  rubygems_version: 1.8.24