-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.rb
More file actions
30 lines (25 loc) · 1.08 KB
/
Copy pathutil.rb
File metadata and controls
30 lines (25 loc) · 1.08 KB
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
# Common utilities.
require 'shellwords'
PWD = ENV['PWD'] || ::Dir.pwd
def path_with_tilde(abs_path)
home = ENV['HOME']
# Only abbreviate the home dir when it is a leading path component: anchored to the
# start and followed by a separator (or the end), with regex metacharacters escaped.
abs_path.sub(/\A#{Regexp.escape(home)}(?=\/|\z)/, '~')
end
def pipe_to_fzf_and_print(fzf_input, reverse, env_var)
# Ignore --fzf command (must be first arg).
query = ARGV[1..-1].join(' ')
# The opts env vars contain shell-quoted option strings; split them like a shell would.
opts = Shellwords.split(ENV['COMPNAV_FZF_OPTS'] || '') +
Shellwords.split(ENV[env_var] || '')
opts.unshift '--tac' if reverse
# Invoke fzf directly (no shell) so that directory names and the query are passed as data,
# never interpreted as code. fzf reads the list from stdin and still draws its UI on /dev/tty.
# --query: Start fzf with any given arguments as the query.
IO.popen(['fzf', '--query', query, *opts], 'r+') do |fzf|
fzf.write fzf_input
fzf.close_write
puts fzf.read
end
end