-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path.pryrc
More file actions
228 lines (189 loc) · 6.28 KB
/
Copy path.pryrc
File metadata and controls
228 lines (189 loc) · 6.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# Using these pry gems -- copy to your Gemfile
# group :development, :test do
# gem 'awesome_print' # pretty print ruby objects
# gem 'pry' # Console with powerful introspection capabilities
# gem 'pry-byebug' # Integrates pry with byebug
# gem 'pry-doc' # Provide MRI Core documentation
# gem 'pry-rails' # Causes rails console to open pry. `DISABLE_PRY_RAILS=1 rails c` can still open with IRB
# gem 'pry-rescue' # Start a pry session whenever something goes wrong.
# gem 'pry-theme' # An easy way to customize Pry colors via theme files
# end
begin
require 'awesome_print'
AwesomePrint.pry!
rescue LoadError => e
puts 'awesome_print gem not found!'
end
def load_helpers
helpers_path = Rails.root.join('.bundle/pry_helpers')
return unless Dir.exist?(helpers_path)
Dir[helpers_path.join('**/*.rb')].sort.each do |file|
puts "🔄 Loading: #{file}"
load file
end
puts "✅ Pry helpers loaded."
rescue LoadError, StandardError => e
puts "❌ Load error: #{e.class} - #{e.message}"
end
load_helpers if defined?(Rails)
class Object
def local_methods(obj = self) # list methods which aren't in superclass
(obj.methods - obj.class.superclass.instance_methods).sort
end
end
def logger
ActiveRecord::Base.logger = Logger.new(STDOUT)
end
if defined?(Rails::Console)
ActiveRecord::Base.logger = Logger.new(STDOUT)
include Rails.application.routes.url_helpers # For testing routes in console
end
if Object.const_defined?('PryRails') && PryRails::Prompt.project_name == 'agent'
def td
Apartment::Tenant.switch! 'djungle'
end
def tp
Apartment::Tenant.switch! 'public'
end
def tc
Apartment::Tenant.current
end
td unless Tenant.count.zero?
end
# Custom prompt for Apartment MultiTenant Apps
if Pry::Prompt[:rails] && Object.const_defined?('Apartment')
begin
current_tenant_name = proc { Pry::Helpers::Text.magenta(Apartment::Tenant.current) }
name = 'multi_tenant'
desc = 'rails prompt with apartment tenant name'
separators = %w[> *]
Pry::Prompt.add(name, desc, separators) do |target_self, nest_level, pry, sep|
"[#{pry.input_ring.size}] " \
"[#{PryRails::Prompt.project_name}][#{PryRails::Prompt.formatted_env}]" \
"[#{current_tenant_name.call}] " \
"#{pry.config.prompt_name}(#{Pry.view_clip(target_self)})" \
"#{":#{nest_level}" unless nest_level.zero?}#{sep} "
end
puts
Pry.config.prompt = Pry::Prompt[:multi_tenant]
rescue StandardError => e
puts e.inspect
Pry.config.prompt = Pry::Prompt[:rails]
end
end
# Shortcut for calling pry_debug
if defined?(PryByebug)
Pry.commands.alias_command 't', 'backtrace'
Pry.commands.alias_command 's', 'step'
Pry.commands.alias_command 'n', 'next'
Pry.commands.alias_command 'c', 'continue'
Pry.commands.alias_command 'f', 'finish'
Pry.commands.alias_command 'ff', 'frame'
Pry.commands.alias_command 'u', 'up'
Pry.commands.alias_command 'd', 'down'
Pry.commands.alias_command 'b', 'break'
Pry.commands.alias_command 'w', 'whereami'
end
Pry.config.commands.alias_command 'h', 'hist -T 20', desc: 'Last 20 commands'
Pry.config.commands.alias_command 'hg', 'hist -T 20 -G', desc: 'Up to 20 commands matching expression'
Pry.config.commands.alias_command 'hG', 'hist -G', desc: 'Commands matching expression ever used'
Pry.config.commands.alias_command 'hr', 'hist -r', desc: 'hist -r <command number> to run a command'
Pry.commands.alias_command '?', 'show-source -d'
def more_help
puts
puts 'Helpful shortcuts:'
puts 'hh : hist -T 20 Last 20 commands'
puts 'hg : hist -T 20 -G Up to 20 commands matching expression'
puts 'hG : hist -G Commands matching expression ever used'
puts 'hr : hist -r hist -r <command number> to run a command'
puts
puts 'Introspection'
puts '$ : show whole method of current context'
puts '$ foo: show definition of foo'
puts '? foo: show docs for foo'
puts
puts "Be careful not to use shortcuts for temp vars, like 'u = User.first`"
puts 'Run `help` to see all the pry commands'
puts 'helper : Access Rails helpers'
puts 'app : Access url_helpers'
puts
puts 'Sidekiq::Queue.new.clear : To clear sidekiq'
puts 'Sidekiq.redis { |r| puts r.flushall } : Another clear of sidekiq'
puts
puts "Run `require 'factory_bot'; FactoryBot.find_definitions` for FactoryBot"
puts
puts 'Debugging Shortcuts'
puts
return '' unless defined?(PryByebug)
puts 'Installed debugging Shortcuts'
puts 'w : whereami'
puts 's : step'
puts 'n : next'
puts 'c : continue'
puts 'f : finish'
puts 'pp(obj) : pretty print object'
puts ''
puts 'Stack movement'
puts 't : backtrace'
puts 'ff : frame'
puts 'u : up'
puts 'd : down'
puts 'b : break'
puts
''
end
# Utils
def copy_class_vscode
file = `osascript <<EOF
tell application "System Events"
set frontApp to name of first process whose frontmost is true
end tell
if frontApp is "Visual Studio Code" then
tell application "Visual Studio Code"
try
set filePath to path of active document of front window
return filePath
on error
return ""
end try
end tell
end if
EOF
`.strip
unless file && File.exist?(file)
puts "❌ No open file detected from VSCode or file does not exist"
return
end
stack = []
File.readlines(file).each do |line|
case line
when /^\s*(module|class)\s+([\w:]+)/
stack << $2
when /^\s*end\b/
stack.pop
end
end
if stack.any?
full_name = stack.join('::')
puts "📋 Copied: #{full_name}"
IO.popen('pbcopy', 'w') { |f| f << full_name } rescue puts "(Couldn't copy to clipboard)"
else
puts "⚠️ No class/module found in #{file}"
end
end
def world_clock
ActiveSupport::TimeZone.all.map(&:name).map do |zone|
zone + ' : ' + Time.now.in_time_zone(zone).strftime('%F %T %:z')
end
end
def benchmark(repetitions = 100, &block)
require 'benchmark'
Benchmark.bm { |b| b.report { repetitions.times(&block) } }
end
def explain(sql)
result = ActiveRecord::Base.connection.execute("EXPLAIN #{sql}")
result.each { |row| puts row.values.join(" | ") }
end
Pry.config.prompt = Pry::NAV_PROMPT unless defined?(Pry::Prompt)
# https://github.com/pry/pry/issues/2185#issuecomment-945082143
ENV['PAGER'] = ' less --raw-control-chars -F -X'