Module: Doing::PromptFZF

Included in:
Prompt
Defined in:
lib/doing/prompt/fzf.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



11
12
13
# File 'lib/doing/prompt/fzf.rb', line 11

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



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
# File 'lib/doing/prompt/fzf.rb', line 47

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)

  prev_level = Doing.logger.level
  Doing.logger.adjust_verbosity({ log_level: :info })
  Doing.logger.log_now(:warn, 'fzf:', 'Compiling and installing fzf -- this will only happen once')
  Doing.logger.log_now(: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
    Doing.logger.log_now(: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)
    Doing.logger.error('fzf:', 'unable to install fzf. You can install manually and Doing will use the system version.')
    Doing.logger.error('fzf:', 'see https://github.com/junegunn/fzf#installation')
    raise RuntimeError.new('Error installing fzf, please report at https://github.com/ttscoff/doing/issues')
  end

  Doing.logger.info('fzf:', "installed to #{fzf}")
  Doing.logger.adjust_verbosity({ log_level: prev_level })
  fzf_bin
end

#uninstall_fzfObject

Remove fzf binary



18
19
20
21
22
# File 'lib/doing/prompt/fzf.rb', line 18

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

#which_fzfString

Return the path to the fzf binary

Returns:



29
30
31
32
33
34
35
36
# File 'lib/doing/prompt/fzf.rb', line 29

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)

  Doing.logger.debug('fzf:', 'Using user-installed fzf')
  TTY::Which.which('fzf')
end