-
Notifications
You must be signed in to change notification settings - Fork 0
/
.irbrc
70 lines (57 loc) · 1.58 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
require 'irb/ext/save-history'
require 'irb/completion'
require 'pp'
#History configuration
IRB.conf[:SAVE_HISTORY] = 300
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-history"
IRB.conf[:AUTO_INDENT]=true
begin
require 'awesome_print'
def ap object = nil, options={ indent: -2 }
super
end
rescue LoadError
def ap object, options={}
pp object
end
end
module Robocarp
# Send stuff to the system clipboard (osx)
def copy stuff
IO.popen('pbcopy', 'w') { |f| f << stuff.to_s }
stuff
end
def paste
IO.popen('pbpaste', 'r') { |f| f.gets }
end
def history search=nil, limit=19
history = IRB::HistorySavingAbility::HISTORY.to_a
history.pop # remove current command
filtered_history = case search
when Regexp
history.select {|e| e =~ search}
when String
history.map {|s| [ levenshtein_distance(s, search), s ] }
.sort {|a,b| b.first <=> a.first }
.map {|s| s[1] }
when NilClass
history
end
filtered_history.last(limit).uniq
end
# credit: 2016-05-18 http://rosettacode.org/wiki/Levenshtein_distance#Ruby
def levenshtein_distance(a, b)
a, b = a.downcase, b.downcase
costs = Array(0..b.length) # i == 0
(1..a.length).each do |i|
costs[0], nw = i, i - 1 # j == 0; nw is lev(i-1, j)
(1..b.length).each do |j|
costs[j], nw = [costs[j] + 1, costs[j-1] + 1, a[i-1] == b[j-1] ? nw : nw + 1].min, costs[j]
end
end
costs[b.length]
end
end
extend Robocarp
irbrc = File.join(Dir.pwd, ".irbrc")
load irbrc if File.exists?(irbrc) unless irbrc == __FILE__