console_util 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.rdoc +6 -0
- data/Rakefile +2 -0
- data/console_util.gemspec +24 -0
- data/lib/console_util/color.rb +88 -0
- data/lib/console_util/version.rb +3 -0
- data/lib/console_util.rb +32 -0
- metadata +88 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.rdoc
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "console_util/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "console_util"
|
7
|
+
s.version = ConsoleUtil::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Clyde Law"]
|
10
|
+
s.email = ["clyde@futureadvisor.com"]
|
11
|
+
s.homepage = %q{http://github.com/FutureAdvisor/console_util}
|
12
|
+
s.summary = %q{Contains various utilities for working in the Rails console.}
|
13
|
+
s.description = %q{Contains various utilities for working in the Rails console.}
|
14
|
+
s.license = 'MIT'
|
15
|
+
|
16
|
+
s.add_dependency('rails', '>= 2.1.0')
|
17
|
+
|
18
|
+
s.rubyforge_project = "console_util"
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
|
+
s.require_paths = ["lib"]
|
24
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
module ConsoleUtil
|
2
|
+
class Color
|
3
|
+
@@colors = [:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]
|
4
|
+
|
5
|
+
@@color_codes = {
|
6
|
+
:reset => 0, # Reset all current console color attributes.
|
7
|
+
:bright => 1, # Set intensity of current console foreground color to bright.
|
8
|
+
:underline => 4, # Underline console output.
|
9
|
+
:blink => 5, # Blink console output.
|
10
|
+
:negative => 7, # Invert the current console foreground and background colors.
|
11
|
+
:normal => 22, # Set intensity of current console foreground color to normal.
|
12
|
+
:no_underline => 24, # Do not underline console output.
|
13
|
+
:no_blink => 25, # Do not blink console output.
|
14
|
+
:positive => 27 # Do not invert the current console foreground and background colors.
|
15
|
+
}
|
16
|
+
|
17
|
+
@@fg_color_base = 30
|
18
|
+
@@bg_color_base = 40
|
19
|
+
|
20
|
+
# Define constants that return the ANSI escape codes corresponding to each
|
21
|
+
# of the predefined color attributes.
|
22
|
+
@@color_codes.each do |color_attr, code|
|
23
|
+
color_const = color_attr.to_s.upcase
|
24
|
+
self.class_eval <<-COLOR_CONST, __FILE__, __LINE__ + 1
|
25
|
+
#{color_const} = "\e[#{code}m"
|
26
|
+
COLOR_CONST
|
27
|
+
end
|
28
|
+
|
29
|
+
class << self
|
30
|
+
# Used to create magic constants for every supported color combination.
|
31
|
+
def const_missing(const)
|
32
|
+
# Attempt to parse the method name as a color string.
|
33
|
+
color_escape_code = convert_color_string(const.to_s)
|
34
|
+
|
35
|
+
if color_escape_code
|
36
|
+
# Define a constant for the ANSI escape code that corresponds to the
|
37
|
+
# specified color and return it.
|
38
|
+
self.class_eval <<-COLOR_CONST, __FILE__, __LINE__ + 1
|
39
|
+
#{const} = "#{color_escape_code}"
|
40
|
+
COLOR_CONST
|
41
|
+
else
|
42
|
+
super
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def valid_escape_code?(color_escape_code)
|
47
|
+
!color_escape_code.match(/^\e\[[0-9;]*m$/).nil?
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
def convert_color_string(color_string)
|
52
|
+
bright = nil
|
53
|
+
fg = nil
|
54
|
+
bg = nil
|
55
|
+
|
56
|
+
# Parse the color string; supported formats are:
|
57
|
+
# - FG
|
58
|
+
# - FG_ON_BG
|
59
|
+
# - ON_BG
|
60
|
+
# - BRIGHT_FG
|
61
|
+
# - BRIGHT_FG_ON_BG
|
62
|
+
case color_string
|
63
|
+
when /^ON_([A-Z]+)$/
|
64
|
+
bg = @@colors.index($1.downcase.to_sym)
|
65
|
+
return nil if bg.nil?
|
66
|
+
when /^(BRIGHT_)?([A-Z]+)(_ON_([A-Z]+))?$/
|
67
|
+
bright = !$1.nil?
|
68
|
+
fg = @@colors.index($2.downcase.to_sym)
|
69
|
+
return nil if fg.nil?
|
70
|
+
if $4
|
71
|
+
bg = @@colors.index($4.downcase.to_sym)
|
72
|
+
return nil if bg.nil?
|
73
|
+
end
|
74
|
+
else
|
75
|
+
return nil
|
76
|
+
end
|
77
|
+
|
78
|
+
# Convert the color to an ANSI escape code sequence that sets the current
|
79
|
+
# console color.
|
80
|
+
color_codes = []
|
81
|
+
color_codes << (bright ? @@color_codes[:bright] : @@color_codes[:normal]) unless bright.nil?
|
82
|
+
color_codes << (fg + @@fg_color_base).to_s if fg
|
83
|
+
color_codes << (bg + @@bg_color_base).to_s if bg
|
84
|
+
"\e[#{color_codes.join(';')}m"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
data/lib/console_util.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'console_util/color'
|
2
|
+
|
3
|
+
module ConsoleUtil
|
4
|
+
class << self
|
5
|
+
# Monkey-patch the ActiveRecord connection to output the SQL query before
|
6
|
+
# executing it.
|
7
|
+
def output_sql_to_console(options = {})
|
8
|
+
color = Color::CYAN
|
9
|
+
if options[:color]
|
10
|
+
raise ArgumentError.new("invalid color option: #{options[:color].inspect}") unless Color.valid_escape_code?(options[:color])
|
11
|
+
color = options[:color]
|
12
|
+
end
|
13
|
+
|
14
|
+
ActiveRecord::Base.connection.class_eval do
|
15
|
+
define_method(:execute) do |sql, *name|
|
16
|
+
puts "#{color}#{sql}#{Color::RESET}"
|
17
|
+
super
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
true
|
22
|
+
end
|
23
|
+
|
24
|
+
# Load all models into the environment
|
25
|
+
def load_all_models
|
26
|
+
Rails.root.join('app/models').each_entry do |model_file|
|
27
|
+
model_match = model_file.to_s.match(/^(.*)\.rb$/)
|
28
|
+
model_match[1].classify.constantize if model_match
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: console_util
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Clyde Law
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-04-20 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rails
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 11
|
29
|
+
segments:
|
30
|
+
- 2
|
31
|
+
- 1
|
32
|
+
- 0
|
33
|
+
version: 2.1.0
|
34
|
+
type: :runtime
|
35
|
+
version_requirements: *id001
|
36
|
+
description: Contains various utilities for working in the Rails console.
|
37
|
+
email:
|
38
|
+
- clyde@futureadvisor.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files: []
|
44
|
+
|
45
|
+
files:
|
46
|
+
- .gitignore
|
47
|
+
- Gemfile
|
48
|
+
- README.rdoc
|
49
|
+
- Rakefile
|
50
|
+
- console_util.gemspec
|
51
|
+
- lib/console_util.rb
|
52
|
+
- lib/console_util/color.rb
|
53
|
+
- lib/console_util/version.rb
|
54
|
+
homepage: http://github.com/FutureAdvisor/console_util
|
55
|
+
licenses:
|
56
|
+
- MIT
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
hash: 3
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
hash: 3
|
77
|
+
segments:
|
78
|
+
- 0
|
79
|
+
version: "0"
|
80
|
+
requirements: []
|
81
|
+
|
82
|
+
rubyforge_project: console_util
|
83
|
+
rubygems_version: 1.7.2
|
84
|
+
signing_key:
|
85
|
+
specification_version: 3
|
86
|
+
summary: Contains various utilities for working in the Rails console.
|
87
|
+
test_files: []
|
88
|
+
|