Class: AIA::Fzf

Inherits:
Object
  • Object
show all
Defined in:
lib/aia/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

Constructor Details

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

Returns a new instance of Fzf.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/aia/fzf.rb', line 18

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.



16
17
18
# File 'lib/aia/fzf.rb', line 16

def command
  @command
end

#directoryObject (readonly)

Returns the value of attribute directory.



16
17
18
# File 'lib/aia/fzf.rb', line 16

def directory
  @directory
end

#extensionObject (readonly)

Returns the value of attribute extension.



16
17
18
# File 'lib/aia/fzf.rb', line 16

def extension
  @extension
end

#listObject (readonly)

Returns the value of attribute list.



16
17
18
# File 'lib/aia/fzf.rb', line 16

def list
  @list
end

#promptObject (readonly)

Returns the value of attribute prompt.



16
17
18
# File 'lib/aia/fzf.rb', line 16

def prompt
  @prompt
end

#queryObject (readonly)

Returns the value of attribute query.



16
17
18
# File 'lib/aia/fzf.rb', line 16

def query
  @query
end

#subjectObject (readonly)

Returns the value of attribute subject.



16
17
18
# File 'lib/aia/fzf.rb', line 16

def subject
  @subject
end

Instance Method Details

#build_commandObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/aia/fzf.rb', line 38

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 = "fzf #{fzf_options.join(' ')}"

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

#runObject



50
51
52
53
54
55
56
57
# File 'lib/aia/fzf.rb', line 50

def run
  # puts "Executing: #{@command}"
  selected = `#{@command}`.strip

  selected.strip.empty? ? nil : selected.strip
ensure
  unlink_tempfile
end