Skip to content

Instantly share code, notes, and snippets.

@note
Created April 23, 2020 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save note/e6e04f911e120cbed0cf21ced3655fbf to your computer and use it in GitHub Desktop.
Save note/e6e04f911e120cbed0cf21ced3655fbf to your computer and use it in GitHub Desktop.
Slightly modified version of https://github.com/defunkt/repl/blob/master/bin/repl script, adjusted for tlarepl
#!/usr/bin/env ruby
# repl(1) -- sometimes you need a repl
#
# repl is an interactive program which tenderly wraps another,
# non-interactive program.
#
# For example:
#
# $ repl redis-cli
# >> set name chris
# OK
# >> get name
# chris
#
# If you have rlwrap(1) installed you'll get the full benefits of
# readline: history, reverse searches, etc.
def show_help
puts <<-help
Usage: repl [options] command ...
Options:
--help Display this message
--stdin Pipe input to command's STDIN
--debug Display each command executed
--man Display the man page
Bug reports, suggestions, updates:
http://http://github.com/defunkt/repl/issues
help
exit
end
if ARGV.empty? || ARGV.any? { |arg| %w( -h --help -help help ).include?(arg) }
show_help
end
if ARGV.include? '--man'
dir = File.dirname(__FILE__)
exec "man #{dir}/../man/repl.1"
end
completion_dir = ENV['REPL_COMPLETION_DIR'] || "~/.repl"
if File.exists?(cdir = File.expand_path(completion_dir))
script = ARGV.detect { |a| a !~ /^-/ }
if script
cfile = Dir[cdir + '/' + File.basename(script)].first
cfile = nil if cfile && !File.exists?(cfile)
end
end
history_dir = ENV['REPL_HISTORY_DIR'] || "~/"
if File.exists?(hdir = File.expand_path(history_dir))
if script = ARGV.detect { |a| a !~ /^-/ }
script = File.basename(script)
hfile = "#{hdir}/.#{script}_history"
end
end
if !ENV['__REPL_WRAPPED'] && system("which rlwrap > /dev/null 2> /dev/null")
ENV['__REPL_WRAPPED'] = '0'
rlargs = ""
rlargs << " -f #{cfile}" if cfile
rlargs << " -H #{hfile}" if hfile
exec "rlwrap #{rlargs} #$0 #{ARGV.join(' ')}"
end
if ARGV[0] == '--debug'
debug = ARGV.delete('--debug')
end
if ARGV.include? '--stdin'
from_stdin = ARGV.delete('--stdin')
end
command = ARGV.join(' ')
show_help if command.empty?
if debug
print 'rlwrap ' if ENV['__REPL_WRAPPED']
print "-f #{cfile} " if cfile
print "-H #{hfile} " if hfile
puts command.inspect
end
loop do
print ENV['REPL_PROMPT'] || "#{ARGV[0]}>> "
begin
line = $stdin.gets.chomp
rescue NoMethodError, Interrupt
exit
end
if from_stdin
# That the only change we need for tlarepl:
run = "echo '%s' | #{command}" % [ line, nil ]
# Original line was :
# run = "echo "%s" | #{command}" % [ line, nil ]
else
run = "#{command} %s" % [ line, nil ]
end
puts "$ #{run}" if debug
system run
warn "Use Ctrl-D (i.e. EOF) to exit" if line =~ /^(exit|quit)$/
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment