-
Notifications
You must be signed in to change notification settings - Fork 7
/
pryrc
274 lines (232 loc) · 6.27 KB
/
pryrc
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
=begin
Author: Capt. Downer
Email: captdowner (@) gma1l d0t c0m
Repo: https:github.com/CaptDowner/.pryrc
Pry custom commands defined in my .pryrc:
cat(f) Display text file contents.
chk List current startup info for services.
dt Show the system date and time.
fl(fn) Load and execute a Ruby source file.
gl2(str) List gems matching search parameter.
glist List all installed gems.
lc Display only filenames in multiple column format.
lf List only filenames, one per line, in alpha order.
loc Display which computer is in use.
lpci List all pci devices.
lsa List all files in filename order.
lsd List only directories.
lss List all files by size from smallest to largest.
lusb List all usb devices.
path Display current PATH.
pm Show methods defined in .pryrc.
pss Display process list.
pwd Print (current) working directory.
rl Reload and excute the most recently loaded ruby source file.
rqa(f) Search for installed RPM.
sip Show location, current ip(s) and network addresses.
time(&b) Display execution timing.
=end
require 'pry'
# Set Pry theme
Pry.config.theme = "tomorrow-night"
# Other (IMHO) good looking pry-themes:
# "vividchalk", "railscasts", "github", "solarized"
# "pry-zealand-16", "tomorrow", "pry-modern", "monokai"
# Configure Pry's prompt, showiung current Ruby and Rails versions
# Rails version is shown after you require 'rails'
Pry.config.prompt = proc do |obj, level, _|
prompt = ""
prompt << "#{Rails.version}@" if defined?(Rails)
prompt << "#{RUBY_VERSION}"
"#{prompt} (#{obj})> "
end
# Reformat Exception
Pry.config.exception_handler = proc do |output, exception, _|
output.puts "\e[31m#{exception.class}: #{exception.message}"
output.puts "from #{exception.backtrace.first}\e[0m"
end
# Using Rails, I include some utilities
if defined?(Rails)
begin
require "rails/console/app"
require "rails/console/helpers"
rescue LoadError => e
require "console_app"
require "console_with_helpers"
end
end
# Shortcuts for debugging commands
if defined?(PryByebug)
Pry.commands.alias_command 'c', 'continue'
Pry.commands.alias_command 's', 'step'
Pry.commands.alias_command 'n', 'next'
Pry.commands.alias_command 'f', 'finish'
Pry.commands.alias_command 'ss', 'show-source'
end
# ===================
# Custom Pry aliases
# ===================
# Where am I?
Pry.config.commands.alias_command 'w', 'whereami'
# Clear Screen
Pry.config.commands.alias_command '.cls', '.clear'
# Return only the methods not present on basic objects
class Object
def ims
(self.methods - Object.instance_methods).sort
end
end
# Load and execute a Ruby source file
def fl(fn)
fn += '.rb' unless fn =~ /\.rb/
@@recent = fn
load "#{fn}"
end
# Reload and excute the most recently loaded ruby source file
def rl
fl(@@recent)
end
# Display only filenames in multiple column format
def lc
op = %x{ ls -aC }
op.gsub!('\t','\n')
puts op
end
# List all files in filename order
def lsa
puts %x{ls -la}.split("\n")
end
# List all files by size from smallest to largest
def lss
puts %x{ls -la -S -r}.split("\n")
end
# List only directories
def ld
puts %x{ls -la | egrep "^d"}
end
# List all files by date
def lsd
puts %x{ls -la --sort=t -r -p}
end
# List only filenames, one per line, in alpha order
def lf
puts %x(ls -aF)
end
# Print (current) working directory
def pwd
%x{pwd}.rstrip
end
# Pist all installed gems
def glist
puts %x{ gem list }
end
# List gems matching search parameter
def gl2(str)
puts %x{ gem list | sort | grep #{str} }
end
# Display execution timing
def time(&b)
require 'benchmark'
res = nil
timing = Benchmark.measure do
res = yield
end
puts "Using yield, it took: user system total real"
puts " #{timing}"
res
end
# Display which computer is in use
def loc
pc=%x{ cat /var/www/html/location.txt }
print pc
end
# Display current PATH
def path
%x{ echo $PATH }
end
# Display process list
def pss
op = %x{ ps --context ax }
puts op
end
# Find installed RPM, where f = string|regexp
def rqa(f)
# if f is a string, convert it to a regexp
if(f.class == String)
f = Regexp.new f
end
# output list of .rpm files matching the regexp
((%x{rpm -qa | sort}).split("\n").each.grep f).each {|m| puts m }
end
# Show the system date and time
def dt
puts %x{date}
end
# List current startup info for services
def chk
puts %x{/sbin/chkconfig --list}
end
# List all pci devices
def lpci
puts %x{ lspci }
end
# List all usb devices
def lusb
puts %x{ lsusb }
end
# Show location, current ip(s) and network addresses
def sip
# you must have a text file named "location.txt"
# which should identify your machine in a unique way
# I include a computer name, ip address, and email in mine
if(Dir.entries('/var/www/html/').detect {|f| f.match /^location.txt/})
f = File.open("/var/www/html/location.txt","r")
f.each_byte do |i|
print i.chr
end
f.close
end
print %x(ifconfig)
end
# Show methods defined in .pryrc
# This depends heavily on the format of
# method definitions. A single comment
# line, followed by the method 'def <method>'
# line will include any new methods in this
# method summary.
def pm
res=[]
save_comment=String.new
data = %x{ cat ~/.pryrc }
str_arr = data.split("\n")
puts "Pry custom commands defined in .pryrc:"
counter=0
while(str_arr[counter] != nil)
# Ruby 1.9 returns an ordinal rather than a character, so...
if(str_arr[counter][0] == "#" || str_arr[counter][0] == 35) then
save_comment = str_arr[counter]
elsif(str_arr[counter].index("def") == 0) then
m = str_arr[counter].gsub("def ","")
m.chomp
# output so columns are aligned
if(m.length < 8) then
res << (m + "\t " + save_comment + "\n")
else
res << (m + " " + save_comment + "\n")
end
m = ""
save_comment = ""
end
counter += 1
end
res.sort!
0.upto(counter) do |x|
print res[x]
end
end
# Display text file contents
def cat(f)
puts %x{cat #{f}}
end
# When opening pry, show summary of methods defined
pm