Module: IRB::FZF::History

Defined in:
lib/irb/fzf/history.rb,
lib/irb/fzf/history/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.enabledObject

Returns the value of attribute enabled.



9
10
11
# File 'lib/irb/fzf/history.rb', line 9

def enabled
  @enabled
end

Class Method Details

.disable!Object

Disable fzf integration and restore default behavior



22
23
24
25
26
27
# File 'lib/irb/fzf/history.rb', line 22

def disable!
  return unless @enabled

  @enabled = false
  restore_reline if defined?(Reline::LineEditor)
end

.enable!Object

Enable fzf integration with IRB’s reverse search



12
13
14
15
16
17
18
19
# File 'lib/irb/fzf/history.rb', line 12

def enable!
  return if @enabled

  @enabled = true
  @fzf_available = check_fzf_availability

  patch_reline if defined?(Reline::LineEditor)
end

.fzf_available?Boolean

Check if fzf is available in the system

Returns:

  • (Boolean)


30
31
32
# File 'lib/irb/fzf/history.rb', line 30

def fzf_available?
  @fzf_available ||= check_fzf_availability
end

.searchObject

Search IRB history using fzf



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/irb/fzf/history.rb', line 35

def search
  return if Reline::HISTORY.empty?

  commands_payload = Reline::HISTORY.to_a
                                    .reverse
                                    .uniq
                                    # Create a string with commands separated by null bytes
                                    .join("\0")

  # --read0 tells fzf that the line separator is a null byte,
  # allowing selection of complete multiline commands
  fzf_opts = ['fzf', '--read0', '--no-sort', '-i']

  IO.popen(fzf_opts, 'r+') do |pipe|
    pipe.write(commands_payload)
    pipe.close_write
    pipe.read.strip
  end
end