-
Notifications
You must be signed in to change notification settings - Fork 3
/
irbrc
124 lines (104 loc) · 3.31 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
#!/ruby
IRB.conf[:SAVE_HISTORY] = 1000
# Load Libraries
require 'rubygems' rescue nil
# require 'json'
alias q exit # Enable exiting irb or the rails console with just `q`
class Object
def local_methods
(methods - Object.instance_methods).sort
end
end
IRB.conf[:AUTO_INDENT] = true
ANSI = {}
ANSI[:RESET] = "\e[0m"
ANSI[:BOLD] = "\e[1m"
ANSI[:UNDERLINE] = "\e[4m"
ANSI[:LGRAY] = "\e[0;37m"
ANSI[:GRAY] = "\e[1;30m"
ANSI[:RED] = "\e[31m"
ANSI[:GREEN] = "\e[32m"
ANSI[:YELLOW] = "\e[33m"
ANSI[:BLUE] = "\e[34m"
ANSI[:MAGENTA] = "\e[35m"
ANSI[:CYAN] = "\e[36m"
ANSI[:WHITE] = "\e[37m"
# Build a simple colorful IRB prompt
IRB.conf[:PROMPT][:SIMPLE_COLOR] = {
:PROMPT_I => "#{ANSI[:BLUE]}>>#{ANSI[:RESET]} ",
:PROMPT_N => "#{ANSI[:BLUE]}>>#{ANSI[:RESET]} ",
:PROMPT_C => "#{ANSI[:RED]}?>#{ANSI[:RESET]} ",
:PROMPT_S => "#{ANSI[:YELLOW]}?>#{ANSI[:RESET]} ",
:RETURN => "#{ANSI[:GREEN]}=>#{ANSI[:RESET]} %s\n",
:AUTO_INDENT => true }
IRB.conf[:PROMPT_MODE] = :SIMPLE_COLOR
# Loading extensions of the console. This is wrapped
# because some might not be included in your Gemfile
# and errors will be raised
def extend_console(name, care = true, required = true)
if care
require name if required
yield if block_given?
$console_extensions << "#{ANSI[:GREEN]}#{name}#{ANSI[:RESET]}"
else
$console_extensions << "#{ANSI[:GRAY]}#{name}#{ANSI[:RESET]}"
end
rescue LoadError
$console_extensions << "#{ANSI[:RED]}#{name}#{ANSI[:RESET]}"
end
$console_extensions = []
# Wirble is a gem that handles coloring the IRB
# output and history
extend_console 'wirble' do
Wirble.init(:history_uniq => false)
Wirble.colorize
end
# Hirb makes tables easy.
extend_console 'hirb' do
Hirb.enable
extend Hirb::Console
end
# awesome_print is prints prettier than pretty_print
extend_console 'ap' do
alias pp ap
end
# Add a method `pm <object>` that displays every method on an object
# Pass a regex to filter these
#
extend_console 'pm', true, false do
def pm(obj, *options) # Print methods
methods = obj.methods
methods -= Object.methods unless options.include? :more
filter = options.select {|opt| opt.kind_of? Regexp}.first
methods = methods.select {|name| name =~ filter} if filter
data = methods.sort.collect do |name|
method = obj.method(name)
if method.arity.zero?
args = '()'
elsif method.arity.positive?
n = method.arity
args_list = (1..n).map { |i| "arg#{i}" }.join(', ')
args = "(#{args_list})"
else
n = -method.arity
args_list = (1..n).map { |i| "arg#{i}" }.join(', ')
args = "(#{args_list}, ...)"
end
klass = $1 if method.inspect =~ /Method: (.*?)#/
[name.to_s, args, klass]
end
max_name = data.map { |item| item[0].size }.max
max_args = data.map { |item| item[1].size }.max
data.each do |item|
print " #{ANSI[:YELLOW]}#{item[0].to_s.rjust(max_name)}#{ANSI[:RESET]}"
print "#{ANSI[:BLUE]}#{item[1].ljust(max_args)}#{ANSI[:RESET]}"
print " #{ANSI[:GRAY]}#{item[2]}#{ANSI[:RESET]}\n"
end
data.size
end
end
extend_console 'interactive_editor' do
# no configuration needed
end
# Show results of all extension-loading
puts "#{ANSI[:GRAY]}~> Console extensions:#{ANSI[:RESET]} #{$console_extensions.join(' ')}#{ANSI[:RESET]}"