Module: Hook::PromptFZF

Included in:
Prompt
Defined in:
lib/hook/prompt.rb

Overview

Methods for working installing/using FuzzyFileFinder

Instance Method Summary collapse

Instance Method Details

#fzfString

Get path to fzf binary, installing if needed

Returns:

  • (String)

    Path to fzf binary



39
40
41
# File 'lib/hook/prompt.rb', line 39

def fzf
  @fzf ||= install_fzf
end

#install_fzf(force: false) ⇒ String

Install fzf on the current system. Installs to a subdirectory of the gem

Parameters:

  • force (Boolean) (defaults to: false)

    If true, reinstall if needed

Returns:

  • (String)

    Path to fzf binary



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
# File 'lib/hook/prompt.rb', line 74

def install_fzf(force: false)
  if force
    uninstall_fzf
  elsif which_fzf
    return which_fzf
  end

  fzf_dir = File.join(File.dirname(__FILE__), '../helpers/fzf')
  FileUtils.mkdir_p(fzf_dir) unless File.directory?(fzf_dir)
  fzf_bin = File.join(fzf_dir, 'bin/fzf')
  return fzf_bin if File.exist?(fzf_bin)

  warn 'fzf: Compiling and installing fzf -- this will only happen once'
  warn 'fzf: fzf is copyright Junegunn Choi, MIT License <https://github.com/junegunn/fzf/blob/master/LICENSE>'

  silence_std
  `'#{fzf_dir}/install' --bin --no-key-bindings --no-completion --no-update-rc --no-bash --no-zsh --no-fish &> /dev/null`
  unless File.exist?(fzf_bin)
    restore_std
    warn 'Error installing, trying again as root'
    silence_std
    `sudo '#{fzf_dir}/install' --bin --no-key-bindings --no-completion --no-update-rc --no-bash --no-zsh --no-fish &> /dev/null`
  end
  restore_std
  unless File.exist?(fzf_bin)
    puts 'fzf: unable to install fzf. You can install manually and Hook CLI will use the system version.'
    puts 'fzf: see https://github.com/junegunn/fzf#installation'
    raise RuntimeError.new('Error installing fzf, please report at https://github.com/ttscoff/hookapp/issues')
  end

  warn "fzf: installed to #{fzf}"
  fzf_bin
end

#uninstall_fzfObject

Remove fzf binary



46
47
48
49
50
# File 'lib/hook/prompt.rb', line 46

def uninstall_fzf
  fzf_bin = File.join(File.dirname(__FILE__), '../helpers/fzf/bin/fzf')
  FileUtils.rm_f(fzf_bin) if File.exist?(fzf_bin)
  warn 'fzf: removed #{fzf_bin}'
end

#which_fzfString

Return the path to the fzf binary

Returns:



57
58
59
60
61
62
63
# File 'lib/hook/prompt.rb', line 57

def which_fzf
  fzf_dir = File.join(File.dirname(__FILE__), '../helpers/fzf')
  fzf_bin = File.join(fzf_dir, 'bin/fzf')
  return fzf_bin if File.exist?(fzf_bin)

  TTY::Which.which('fzf')
end