color_debug_messages 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +4 -0
- data/LICENSE +20 -0
- data/README.rdoc +60 -0
- data/Rakefile +28 -0
- data/VERSION +1 -0
- data/color_debug_messages.gemspec +54 -0
- data/lib/color_debug_messages.rb +147 -0
- data/test/helper.rb +10 -0
- data/test/test_color_debug_messages.rb +7 -0
- metadata +75 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Brent Sanders
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
= NAME
|
2
|
+
|
3
|
+
Color Debug Messages - A way to add nicer output to your script.
|
4
|
+
|
5
|
+
= DESCRIPTION
|
6
|
+
|
7
|
+
I got tired of plain output on STDOUT, and wanted a nicer way
|
8
|
+
to add debug/informational messages to my scripts. This works
|
9
|
+
with the idea that it is easier for our eyes to notice changes
|
10
|
+
in color than changes in glyph/shape. A red error message is
|
11
|
+
notices more easily than a "*****" prefix or similar.
|
12
|
+
|
13
|
+
To get this effect, we rely on Term::ANSIColor
|
14
|
+
|
15
|
+
== Install
|
16
|
+
|
17
|
+
gem install color_debug_messages
|
18
|
+
|
19
|
+
== Usage
|
20
|
+
|
21
|
+
The basic use of this script is very simple - you just include
|
22
|
+
the module into your class, and call the various "puts" wrappers.
|
23
|
+
|
24
|
+
class Foo
|
25
|
+
include ColorDebugMessages
|
26
|
+
def bar(baz)
|
27
|
+
debug "This is a debug message!"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
Which produces:
|
32
|
+
|
33
|
+
[Foo#bar] >>> This is a debug message!
|
34
|
+
|
35
|
+
The part in square-brackets at the beginning will be in read, which
|
36
|
+
I unfortunately am having a hard time making RDoc produce.
|
37
|
+
|
38
|
+
The available message types, by default, are:
|
39
|
+
|
40
|
+
debug(msg) # ">>>" prefix, in cyan
|
41
|
+
warn(msg) # "**>" prefix, in red
|
42
|
+
info(msg) # "-->" prefix, in green
|
43
|
+
|
44
|
+
These can be changed by adding lines like this to the top of your class:
|
45
|
+
|
46
|
+
class Foo
|
47
|
+
include ColorDebugMessages
|
48
|
+
color_debug_message_type :quux, '~', :blue
|
49
|
+
|
50
|
+
def bar
|
51
|
+
quux "Test message!"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
Which will produce yellow output, with the <tt>"~~>"</tt> prefix instead.
|
56
|
+
|
57
|
+
= COPYRIGHT
|
58
|
+
|
59
|
+
Copyright (c) 2010 Brent Sanders. See LICENSE for details.
|
60
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "color_debug_messages"
|
8
|
+
gem.summary = "Easy to read output on STDOUT"
|
9
|
+
gem.description = "Module to add colorful debug messages to a class."
|
10
|
+
gem.email = "gem-color_debug_messages@thoughtnoise.net"
|
11
|
+
gem.homepage = "http://github.com/pdkl95/color_debug_messages"
|
12
|
+
gem.authors = ["Brent Sanders"]
|
13
|
+
gem.add_development_dependency "term-ansicolor", ">= 1.0.4"
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rake/rdoctask'
|
21
|
+
Rake::RDocTask.new do |rdoc|
|
22
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
23
|
+
|
24
|
+
rdoc.rdoc_dir = 'rdoc'
|
25
|
+
rdoc.title = "color_debug_messages #{version}"
|
26
|
+
rdoc.rdoc_files.include('README*')
|
27
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
28
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.1.0
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{color_debug_messages}
|
8
|
+
s.version = "1.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Brent Sanders"]
|
12
|
+
s.date = %q{2010-01-02}
|
13
|
+
s.description = %q{Module to add colorful debug messages to a class.}
|
14
|
+
s.email = %q{gem-color_debug_messages@thoughtnoise.net}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"color_debug_messages.gemspec",
|
27
|
+
"lib/color_debug_messages.rb",
|
28
|
+
"test/helper.rb",
|
29
|
+
"test/test_color_debug_messages.rb"
|
30
|
+
]
|
31
|
+
s.homepage = %q{http://github.com/pdkl95/color_debug_messages}
|
32
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
33
|
+
s.require_paths = ["lib"]
|
34
|
+
s.rubygems_version = %q{1.3.5}
|
35
|
+
s.summary = %q{Easy to read output on STDOUT}
|
36
|
+
s.test_files = [
|
37
|
+
"test/helper.rb",
|
38
|
+
"test/test_color_debug_messages.rb"
|
39
|
+
]
|
40
|
+
|
41
|
+
if s.respond_to? :specification_version then
|
42
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
|
+
s.specification_version = 3
|
44
|
+
|
45
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
46
|
+
s.add_development_dependency(%q<term-ansicolor>, [">= 1.0.4"])
|
47
|
+
else
|
48
|
+
s.add_dependency(%q<term-ansicolor>, [">= 1.0.4"])
|
49
|
+
end
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<term-ansicolor>, [">= 1.0.4"])
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
@@ -0,0 +1,147 @@
|
|
1
|
+
require 'term/ansicolor'
|
2
|
+
|
3
|
+
# Include this module into your class to get access to the
|
4
|
+
# debug helper functions.
|
5
|
+
module ColorDebugMessages
|
6
|
+
# if you specify nothing else, you get full-messages, nothing hidden
|
7
|
+
DEFAULT_DEBUG_FLAGS = {
|
8
|
+
:info => true,
|
9
|
+
:debug => true,
|
10
|
+
:warn => true,
|
11
|
+
:class_only => false,
|
12
|
+
:prefix_only => false
|
13
|
+
}
|
14
|
+
|
15
|
+
# You can change the option flags globally by setting this to a hash
|
16
|
+
# of flags. For example, to only have 'info' and 'warn' level
|
17
|
+
# messages, with only class names and no function names, you could do:
|
18
|
+
#
|
19
|
+
# ColorDebugMessages.debug_flags = {
|
20
|
+
# :debug => false,
|
21
|
+
# :class_only => true
|
22
|
+
# }
|
23
|
+
#
|
24
|
+
# The hash is merged with the existing one, so it is not necessary
|
25
|
+
# to name things that are already set the way you want.
|
26
|
+
def self.debug_flags=(opts)
|
27
|
+
debug_flags.merge! opts
|
28
|
+
end
|
29
|
+
|
30
|
+
# returns the global flags
|
31
|
+
def self.debug_flags
|
32
|
+
@debug_flags ||= DEFAULT_DEBUG_FLAGS
|
33
|
+
end
|
34
|
+
|
35
|
+
# This is the same as ColorDebugMessages#debug_flags but on a
|
36
|
+
# per-instance level, if you need to locally change things for some
|
37
|
+
# reason. This hash starts out blank, and any missing entries are
|
38
|
+
# looked up in the global hash.
|
39
|
+
def debug_flags=(opts)
|
40
|
+
debug_flags.merge! opts
|
41
|
+
end
|
42
|
+
|
43
|
+
# returns the per-instance flag overrides
|
44
|
+
def debug_flags
|
45
|
+
@color_debug_message_options ||= Hash.new
|
46
|
+
end
|
47
|
+
|
48
|
+
# returns +true+ if the flag is true in either the local instance
|
49
|
+
# or the global hash.
|
50
|
+
def debug_flag_active?(name)
|
51
|
+
if debug_flags.has_key?(name)
|
52
|
+
debug_flags
|
53
|
+
else
|
54
|
+
ColorDebugMessages.debug_flags
|
55
|
+
end[name]
|
56
|
+
end
|
57
|
+
|
58
|
+
# returns the prefix to tag on debug messages for a class.
|
59
|
+
# This normally reflects the class name itself, and appends
|
60
|
+
# the function name (caller[1]) of what cause this message.
|
61
|
+
# It can be overridden in your class to whatever you want,
|
62
|
+
# if necessary. Just return a string.
|
63
|
+
def debug_message_prefix
|
64
|
+
return self.class.to_s if debug_flag_active?(:class_only)
|
65
|
+
|
66
|
+
name = self.class.to_s + '#'
|
67
|
+
if /`(.*)'/ =~ caller[1]
|
68
|
+
name + $1
|
69
|
+
else
|
70
|
+
name + "[unknown]"
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class ColorDebugMsg # :nodoc:all
|
75
|
+
include Term::ANSIColor
|
76
|
+
attr_reader :color, :mark, :calling_method_name, :msg
|
77
|
+
|
78
|
+
def initialize(msg, prefix=nil, mark=nil, color=nil)
|
79
|
+
@color = color
|
80
|
+
@mark = mark
|
81
|
+
@prefix = prefix
|
82
|
+
@msg = msg
|
83
|
+
end
|
84
|
+
|
85
|
+
def to_s
|
86
|
+
str = ''
|
87
|
+
if @prefix
|
88
|
+
if @color
|
89
|
+
str += send @color, bold('[')
|
90
|
+
str += send @color, @prefix
|
91
|
+
str += send @color, bold('] ')
|
92
|
+
else
|
93
|
+
str += "[#{@prefix}] "
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
if @mark
|
98
|
+
mark_str = @mark + @mark + '> '
|
99
|
+
if @color
|
100
|
+
str += send @color, bold(mark_str)
|
101
|
+
else
|
102
|
+
str += mark_str
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
str += reset if @color
|
107
|
+
str += @msg
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
# returns a string with the ANSI *bold* flag set, for usage in
|
112
|
+
# the middle of debug statements.
|
113
|
+
#
|
114
|
+
# debug "This is a " + bold("fancy") + "message!"
|
115
|
+
def bold(string_to_bold)
|
116
|
+
Term::ANSIColor.bold string_to_bold
|
117
|
+
end
|
118
|
+
|
119
|
+
# Meta-generator for the actual debug message calls. At the core,
|
120
|
+
# these are all just a fancy wrapper around Kernel#puts with
|
121
|
+
# the color output. They always add the newline "\n" character to
|
122
|
+
# the end as a convenience, and usually prefix the line with an
|
123
|
+
# identifying mark, so we know what generated the message.
|
124
|
+
#
|
125
|
+
# === Options:
|
126
|
+
# <tt>:name</tt>:: (Symbol) The name of the function to
|
127
|
+
# create. Existing functions can be
|
128
|
+
# overwritten to change their color/prefix.
|
129
|
+
# <tt>:prefix_char</tt>:: (String) A character to use as a separator
|
130
|
+
# for messages of this type.
|
131
|
+
# <tt>:color</tt>:: (Symbol) The color to use for the
|
132
|
+
# prefix. This must be a valid function in
|
133
|
+
# the Term::ANSIColor library.
|
134
|
+
def self.color_debug_message_type(name, prefix_char, color)
|
135
|
+
define_method(name) do |msg|
|
136
|
+
if debug_flag_active?(name)
|
137
|
+
caller_prefix = debug_message_prefix
|
138
|
+
caller_prefix = nil if debug_flag_active?(:prefix_only)
|
139
|
+
puts ColorDebugMsg.new(msg, caller_prefix, prefix_char, color)
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
color_debug_message_type :debug, '>', :cyan
|
145
|
+
color_debug_message_type :warn, '*', :red
|
146
|
+
color_debug_message_type :info, '-', :green
|
147
|
+
end
|
data/test/helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: color_debug_messages
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brent Sanders
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-01-02 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: term-ansicolor
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.0.4
|
24
|
+
version:
|
25
|
+
description: Module to add colorful debug messages to a class.
|
26
|
+
email: gem-color_debug_messages@thoughtnoise.net
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- color_debug_messages.gemspec
|
42
|
+
- lib/color_debug_messages.rb
|
43
|
+
- test/helper.rb
|
44
|
+
- test/test_color_debug_messages.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://github.com/pdkl95/color_debug_messages
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options:
|
51
|
+
- --charset=UTF-8
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.5
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Easy to read output on STDOUT
|
73
|
+
test_files:
|
74
|
+
- test/helper.rb
|
75
|
+
- test/test_color_debug_messages.rb
|