console_util 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module ConsoleUtil
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/console_util.rb CHANGED
@@ -2,6 +2,8 @@ require 'console_util/color'
2
2
 
3
3
  module ConsoleUtil
4
4
  class << self
5
+ attr_reader :suppressed_output
6
+
5
7
  # Monkey-patch the ActiveRecord connection to output the SQL query before
6
8
  # executing it.
7
9
  def output_sql_to_console(options = {})
@@ -37,5 +39,122 @@ module ConsoleUtil
37
39
  model_match[1].classify.constantize if model_match
38
40
  end
39
41
  end
42
+
43
+ # Allows you to filter output to the console using grep
44
+ # Ex:
45
+ # def foo
46
+ # puts "Some debugging output here"
47
+ # puts "The value of x is y"
48
+ # puts "The value of foo is bar"
49
+ # end
50
+ #
51
+ # grep_stdout(/value/) { foo }
52
+ # # => The value of x is y
53
+ # # => The value of foo is bar
54
+ # # => nil
55
+ def grep_stdout(expression)
56
+ # First we need to create a ruby "pipe" which is two sets of IO subclasses
57
+ # the first is read only (which represents a fake $stdin) and the second is
58
+ # write only (which represents a fake $stdout).
59
+ placeholder_in, placeholder_out = IO.pipe
60
+
61
+ # This child process handles the grep'ing. Its done in a child process so that
62
+ # it can operate in parallel with the main process.
63
+ pid = fork {
64
+ # sync $stdout so we can report any matches asap
65
+ $stdout.sync
66
+
67
+ # replace $stdout with placeholder_out
68
+ $stdin.reopen(placeholder_in)
69
+
70
+ # we have to close both placeholder_out and placeholder_in because all instances
71
+ # of an IO stream must be closed in order for it to ever reach EOF. There's two
72
+ # in this method; one in the child process and one in the main process.
73
+ placeholder_in.close
74
+ placeholder_out.close
75
+
76
+ # loop continuously until we reach EOF (which happens when all
77
+ # instances of placeholder_out have closed)
78
+ read_buffer = ''
79
+ loop do
80
+ begin
81
+ match = nil
82
+ next_read = $stdin.readpartial(4096)
83
+
84
+ read_buffer << next_read
85
+ if line_match = read_buffer.match(/^(.*\n)(.*)$/m)
86
+ match = line_match[1].grep(expression) # grep complete lines
87
+ read_buffer = line_match[2] # save remaining partial line for the next iteration
88
+ end
89
+ rescue EOFError
90
+ match = read_buffer.grep(expression) # grep any remaining partial line at EOF
91
+ break
92
+ end
93
+
94
+ if match && !match.empty?
95
+ print match
96
+ end
97
+ end
98
+ }
99
+
100
+ # Save the original stdout out to a variable so we can use it again after this
101
+ # method is done
102
+ original_stdout = $stdout
103
+
104
+ # Redirect stdout to our pipe
105
+ $stdout = placeholder_out
106
+
107
+ # sync $stdout so that we can start operating on it as soon as possible
108
+ $stdout.sync
109
+
110
+ # allow the block to execute and save its return value
111
+ return_value = yield
112
+
113
+ # Set stdout back to the original so output will flow again
114
+ $stdout = original_stdout
115
+
116
+ # close the main instances of placeholder_in and placeholder_out
117
+ placeholder_in.close
118
+ placeholder_out.close
119
+
120
+ # Wait for the child processes to finish
121
+ Process.wait pid
122
+
123
+ # Because the connection to the database has a tendency to go away when calling this, reconnect here
124
+ # if we're using ActiveRecord
125
+ if defined?(ActiveRecord)
126
+ suppress_stdout { ActiveRecord::Base.verify_active_connections! }
127
+ end
128
+
129
+ # return the value of the block
130
+ return_value
131
+ end
132
+
133
+ # Allows you to suppress $stdout but allows you to send certain messages to $stdout
134
+ # Ex:
135
+ # def foo
136
+ # puts "lots of stuff directed to $stdout that I don't want to see."
137
+ # end
138
+ #
139
+ # suppress_stdout do |stdout|
140
+ # stdout.puts "About to call #foo"
141
+ # foo
142
+ # stdout.puts "Called foo"
143
+ # end
144
+ # # => About to call #foo
145
+ # # => Called foo
146
+ # # => <# The result of #foo >
147
+ def suppress_stdout
148
+ original_stdout = $stdout
149
+ $stdout = output_buffer = StringIO.new
150
+ begin
151
+ return_value = yield(original_stdout)
152
+ ensure
153
+ $stdout = original_stdout
154
+ @suppressed_output ||= ""
155
+ @suppressed_output << output_buffer.string
156
+ end
157
+ return_value
158
+ end
40
159
  end
41
160
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console_util
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Clyde Law
@@ -15,7 +15,8 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-06-02 00:00:00 Z
18
+ date: 2012-07-19 00:00:00 -07:00
19
+ default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
22
  name: rails
@@ -51,6 +52,7 @@ files:
51
52
  - lib/console_util.rb
52
53
  - lib/console_util/color.rb
53
54
  - lib/console_util/version.rb
55
+ has_rdoc: true
54
56
  homepage: http://github.com/FutureAdvisor/console_util
55
57
  licenses:
56
58
  - MIT
@@ -80,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
82
  requirements: []
81
83
 
82
84
  rubyforge_project: console_util
83
- rubygems_version: 1.7.2
85
+ rubygems_version: 1.6.2
84
86
  signing_key:
85
87
  specification_version: 3
86
88
  summary: Contains various utilities for working in the Rails console.