Class: AIA::Fzf

Inherits:
Tools show all
Defined in:
lib/aia/tools/fzf.rb

Constant Summary collapse

DEFAULT_PARAMETERS =
%w[
  --tabstop=2
  --header-first
  --prompt='Search term: '
  --delimiter :
  --preview-window=down:50%:wrap
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Tools

catalog, get_meta, inherited, load_tools, #meta, meta, search_for, setup_backend, validate_tools

Constructor Details

#initialize(list:, directory:, query: '', subject: 'Prompt IDs', prompt: 'Select one:', extension: '.txt') ⇒ Fzf

Returns a new instance of Fzf.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/aia/tools/fzf.rb', line 27

def initialize(
    list:,          # Array of Strings (basenames of files w/o extension)
    directory:,     # Parent directory of the list items
    query:      '', # String, the thing be searched for
    subject:    'Prompt IDs', # or 'Role Names'
    prompt:     'Select one:',
    extension:  '.txt'
  )

  @list       = list
  @directory  = directory
  @query      = query
  @subject    = subject
  @prompt     = prompt
  @extension  = extension
  
  build_command
end

Instance Attribute Details

#commandObject (readonly)

Returns the value of attribute command.



25
26
27
# File 'lib/aia/tools/fzf.rb', line 25

def command
  @command
end

#directoryObject (readonly)

Returns the value of attribute directory.



25
26
27
# File 'lib/aia/tools/fzf.rb', line 25

def directory
  @directory
end

#extensionObject (readonly)

Returns the value of attribute extension.



25
26
27
# File 'lib/aia/tools/fzf.rb', line 25

def extension
  @extension
end

#listObject (readonly)

Returns the value of attribute list.



25
26
27
# File 'lib/aia/tools/fzf.rb', line 25

def list
  @list
end

#promptObject (readonly)

Returns the value of attribute prompt.



25
26
27
# File 'lib/aia/tools/fzf.rb', line 25

def prompt
  @prompt
end

#queryObject (readonly)

Returns the value of attribute query.



25
26
27
# File 'lib/aia/tools/fzf.rb', line 25

def query
  @query
end

#subjectObject (readonly)

Returns the value of attribute subject.



25
26
27
# File 'lib/aia/tools/fzf.rb', line 25

def subject
  @subject
end

Instance Method Details

#build_commandObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/aia/tools/fzf.rb', line 47

def build_command
  fzf_options = DEFAULT_PARAMETERS.dup
  fzf_options << "--header='#{subject} which contain: #{query}\\nPress ESC to cancel.'"
  fzf_options << "--preview='cat #{directory}/{1}#{extension}'"
  fzf_options << "--prompt=#{Shellwords.escape(prompt)}"
  
  fzf_command = "#{meta.name} #{fzf_options.join(' ')}"

  @command = "cat #{tempfile_path} | #{fzf_command}"
end

#runObject



59
60
61
62
63
64
65
# File 'lib/aia/tools/fzf.rb', line 59

def run
  puts "Executing: #{@command}"
  selected = `#{@command}`
  selected.strip.empty? ? nil : selected.strip
ensure
  unlink_tempfile
end