-
Notifications
You must be signed in to change notification settings - Fork 1
/
using_search_proc.rb
executable file
·68 lines (48 loc) · 1.8 KB
/
using_search_proc.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env ruby
# encoding: utf-8
# frozen_string_literal: true
# warn_indent: true
##########################################################
###
## File: using_search_proc.rb
## Desc: Simple demo of the PromptManager and FileStorageAdapter
## By: Dewayne VanHoozer ([email protected])
##
#
require 'prompt_manager'
require 'prompt_manager/storage/file_system_adapter'
require 'amazing_print'
require 'pathname'
HERE = Pathname.new( __dir__ )
PROMPTS_DIR = HERE + "prompts_dir"
SEARCH_SCRIPT = HERE + 'rgfzf' # a bash script using rg and fzf
######################################################
# Main
at_exit do
puts
puts "Done."
puts
end
# Configure the Storage Adapter to use
PromptManager::Storage::FileSystemAdapter.config do |config|
config.prompts_dir = PROMPTS_DIR
config.search_proc = ->(q) {`#{SEARCH_SCRIPT} #{q} #{PROMPTS_DIR}`} # default
# config.prompt_extension = '.txt' # default
# config.parms+_extension = '.json' # default
end
PromptManager::Prompt.storage_adapter = PromptManager::Storage::FileSystemAdapter.new
puts "Using Custom Search Proc"
puts "========================"
print "Search Proc Class: "
puts PromptManager::Prompt.storage_adapter.search_proc.class
search_term = "txt" # some comment lines show the file name example: todo.txt
puts "Search for '#{search_term}' ..."
prompt_id = PromptManager::Prompt.search search_term
# NOTE: the search proc uses fzf as a selection tool. In this
# case only one selected prompt ID that matches the search
# term will be returned.
puts "Found: '#{prompt_id}' which is a #{prompt_id.class}. empty? #{prompt_id.empty?}"
puts <<~EOS
When the rgfzf bash script does not find a prompt ID or if the
ESC key is pressed, the prompt ID that is returned will be an empty String.
EOS