Skip to content

Instantly share code, notes, and snippets.

@bootleq
Created February 28, 2016 11:02
Show Gist options
  • Save bootleq/786cb41a8072e537467e to your computer and use it in GitHub Desktop.
Save bootleq/786cb41a8072e537467e to your computer and use it in GitHub Desktop.
Script to serve Tmux `copy-pipe` input
#!/usr/bin/env ruby
type = ARGV.shift
# Support both ARGV (normal argument) and ARGF (tmux copy-pipe)
arg = ARGV.any? ? ARGV.join(' ') : ARGF.read
case type
when 'ag'
q = Regexp.escape(arg)
.gsub('"', '\"')
.gsub(/!/, '\!')
%x(tmux new-window -a -c "\#{pane_current_path}" -n ag)
%x(sleep 0.2 && tmux send-keys -l 'ag "#{q}"' && tmux send-keys Enter)
when 'gitdiffall'
case arg
when /\A-{1,2}/, /\w{7}/
%x(tmux new-window -a -c "\#{pane_current_path}" -n ⎇)
if arg[0] == '-'
%x(sleep 0.2 && tmux send-keys -l 'gitdiffall #{arg}' && tmux send-keys Enter)
elsif arg =~ /\A\w*\z/
%x(sleep 0.2 && tmux send-keys -l 'gitdiffall @#{arg[0, 7]}' && tmux send-keys Enter)
else
%x(sleep 0.2 && tmux send-keys -l 'gitdiffall #{arg}' && tmux send-keys Enter)
end
else
%x(tmux display-message 'Invalid argument.')
end
when 'cd'
q = arg.chomp.sub(/\A~(?=\/)/, ENV['HOME'])
q = File.dirname(q) unless File.directory?(q)
if File.directory? q
%x(tmux new-window -a -c "#{q}" -n "#{File.basename q}")
else
%x(tmux display-message 'Invalid directory.')
end
else
%x(tmux display-message 'Invalid argument, check tmux config.')
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment