-
Notifications
You must be signed in to change notification settings - Fork 0
/
.irbrc
271 lines (247 loc) · 6.5 KB
/
.irbrc
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
# test
require 'irb/completion'
#------------------------------------------------------------
# sync texteditor (vim,emacs,mate,..) with irb
# http://github.com/jberkel/interactive_editor
# > mate
# # edit.. , save, quit
# > # use your written class, whatever within irb
begin
require 'interactive_editor'
rescue Exception
puts "no interactive_editor available"
end
#------------------------------------------------------------
# http://sketches.rubyforge.org/
# save/reload an editor sketch from irb
# > sketch :foo
begin
require 'sketches'
Sketches.config :editor => 'emacsclient -c'
rescue Exception
puts "no sketches available"
end
#------------------------------------------------------------
# hirb - rails nice ascii table formatter
# sucks terminal colors
#begin
# require 'hirb'
# require 'hirb/import_object'
# Hirb.enable
# extend Hirb::Console
#rescue #drop
#end
#------------------------------------------------------------
#
begin
require 'looksee'
# print short help
# irb> Looksee.help
#
# Looksee color config
Looksee::styles[:module] = "\e[0;47m%s\e[0m" # black on gray
Looksee::styles[:public] = "\e[0;32m%s\e[0m" # green
Looksee::styles[:protected] = "\e[0;33m%s\e[0m" # yellow
Looksee::styles[:private] = "\e[0;31m%s\e[0m" # red
Looksee::styles[:undefined] = "\e[0;34m%s\e[0m" # blue
Looksee::styles[:overridden] = "\e[0;30m%s\e[0m" # black
#Looksee.editor = "emacs -nw +%f %l"
# yeah monkey patch for my emacsclient alias :)
class InteractiveEditor
module Editors
{
:vi => nil,
:vim => nil,
:emacs => nil,
:e => "emacsclient -c",
:nano => nil,
:mate => 'mate -w',
:mvim => 'mvim -g -f' + case ENV['TERM_PROGRAM']
when 'iTerm.app'; ' -c "au VimLeave * !open -a iTerm"'
when 'Apple_Terminal'; ' -c "au VimLeave * !open -a Terminal"'
else '' #don't do tricky things if we don't know the Term
end
}.each do |k,v|
define_method(k) do |*args|
InteractiveEditor.edit(v || k, self, *args)
end
end
def ed(*args)
if ENV['EDITOR'].to_s.size > 0
InteractiveEditor.edit(ENV['EDITOR'], self, *args)
else
raise "You need to set the EDITOR environment variable first"
end
end
end
end
Looksee.editor = "e -nw +%f %l"
rescue Exception
puts "no looksee available"
end
#------------------------------------------------------------
#
# global namespace polution
#
# got from http://ruby-doc.org/docs/ProgrammingRuby/html/ospace.html
def hierarchy klass
if klass.instance_of? Class
begin
print klass
klass = klass.superclass
print " < " if klass
end while klass
puts
end
end
# show_regexp - stolen from the pickaxe
def show_regexp(a, re)
if a =~ re
"#{$`}<<#{$&}>>#{$'}"
else
"no match"
end
end
# a smarter RI
def ri arg
@store ||= {}
unless @store[arg]
@store[arg] = `ri #{arg}`
end
puts @store[arg]
end
# print the last n commands from history
def last_cmds(n=1)
puts Readline::HISTORY.entries[-(n+1)..-2].join("\n")
end
# less { yp IRB.conf }
#def less
# require 'stringio'
# $stdout, sout = StringIO.new, $stdout
# yield
# $stdout, str_io = sout, $stdout
# IO.popen('less', 'w') do |f|
# f.write str_io.string
# f.flush
# f.close_write
# end
#end
def yp(*data)
require 'yaml'
puts YAML::dump(*data)
end
#------------------------------------------------------------
# monkey patches
# Convenience method on Regexp so you can do
# /an/.show_match("banana")
class Regexp
def show_match(a)
show_regexp(a, self)
end
end
#class Module
# def ri(meth=nil)
# if meth
# if instance_methods(false).include? meth.to_s
# puts `ri #{self}##{meth}`
# else
# super
# end
# else
# puts `ri #{self}`
# end
# end
#end
# got from [email protected]
# local_methods shows methods that are only available for a given object.
class Object
# Return a list of methods defined locally for a particular object. Useful
# for seeing what it does whilst losing all the guff that's implemented
# by its parents (eg Object).
def local_methods(obj = self)
obj.methods(false).sort
end
end
require 'cgi'
class String
def to_base64
[self].pack('m')
end
def from_base64
self.unpack('m')
end
def unescape
CGI.unescape self
end
def escape
CGI.escape self
end
end
#------------------------------------------------------------
# awesome_print : http://github.com/michaeldv/awesome_print
#
# > data = [ false, 42, %w(forty two), { :now => Time.now, :class => Time}]
# > ap data
#
#require 'ap'
# use ap as default formatter
#IRB::Irb.class_eval do
# def output_value
# ap @context.last_value
# end
#end
#------------------------------------------------------------
# irb config
IRB.conf[:USE_READLINE] = true
IRB.conf[:AUTO_INDENT] = true
IRB.conf[:PROMPT][:CUSTOM] = {
:PROMPT_N => "%N:%i> ",
:PROMPT_S => "%N:%i%l ",
:PROMPT_C => "%N:%i* ",
:RETURN => "=> %s\n",
:PROMPT_I => "%N:%i> "
}
IRB.conf[:PROMPT_MODE] = :CUSTOM # set default
#------------------------------------------------------------
# history
require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:EVAL_HISTORY] = 100
HISTFILE = "~/.irb.hist"
MAXHISTSIZE = 100
# restore history
begin
if defined? Readline::HISTORY
histfile = File::expand_path( HISTFILE )
if File::exists?( histfile )
lines = IO::readlines( histfile ).collect {|line| line.chomp}
puts "Read %d saved history commands from %s." %
[ lines.nitems, histfile ] if $DEBUG || $VERBOSE
Readline::HISTORY.push( *lines )
else
puts "History file '%s' was empty or non-existant." %
histfile if $DEBUG || $VERBOSE
end
Kernel::at_exit {
lines = Readline::HISTORY.to_a.reverse.uniq.reverse
lines = lines[ -MAXHISTSIZE, MAXHISTSIZE ] if lines.size > MAXHISTSIZE
$stderr.puts "Saving %d history lines to %s." %
[ lines.length, histfile ] if $VERBOSE || $DEBUG
File::open( histfile, File::WRONLY|File::CREAT|File::TRUNC ) do |ofh|
lines.each {|line| ofh.puts line }
end
}
end
end
# print an array like a tree
# [1,2,[11,22,[111,222,333,444],33],3,4].recursive_print
class Object
def recursive_print indent = ""
puts indent.gsub(/\s+$/,"--") + to_s
end
end
module Enumerable
def recursive_print indent = ""
map { |child| child.recursive_print indent+"| " }
end
end