mfm/src/fzf.cr

37 lines
928 B
Crystal
Raw Normal View History

2023-10-25 12:01:46 +00:00
# SPDX-License-Identifier: GPL-3.0-or-later
#
# SPDX-FileCopyrightText: 2023 Glenn Y. Rolland <glenux@glenux.net>
# Copyright © 2023 Glenn Y. Rolland <glenux@glenux.net>
2023-10-22 21:23:56 +00:00
module GX
class Fzf
def self.run(list : Array(String)) : String
input = IO::Memory.new
input.puts list.join("\n")
input.rewind
output = IO::Memory.new
error = STDERR
process = Process.new("fzf", ["--ansi"], input: input, output: output, error: error)
2023-10-24 14:00:12 +00:00
status = process.wait
case status.exit_code
when 0
when 1
STDERR.puts "No match".colorize(:red)
exit(1)
when 130
STDERR.puts "Interrupted".colorize(:red)
exit(1)
else # includes retcode = 2 (error)
STDERR.puts "Error executing fzf: #{error.to_s.strip} (#{status.exit_code})".colorize(:red)
2023-10-22 21:23:56 +00:00
exit(1)
end
result = output.to_s.strip #.split.first?
end
end
end