Skip to content

Instantly share code, notes, and snippets.

@youpy
Created February 24, 2012 05:35
Show Gist options
  • Save youpy/1898039 to your computer and use it in GitHub Desktop.
Save youpy/1898039 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: utf-8
# usage:
# $ command arg1 arg2 arg3 | hee
require 'pathname'
$kcode = 'u'
$stdout.sync = true
# # .zshrc
# setopt inc_append_history
histfile = ENV['HISTFILE']
def read_last_line(filename)
open(filename) do |file|
bufsize = 1024
pos = 0
separator_found = false
result = ''
file.seek(0, IO::SEEK_END)
until separator_found
file.seek(bufsize > file.pos ? - file.pos : - bufsize, IO::SEEK_CUR)
pos = file.pos
buf = file.read(bufsize)
# end of file
if buf.match(/#{$/}\z/) && result == ''
buf = buf.slice(0, buf.size - $/.size)
end
match = buf.match(/#{$/}+([^#{$/}]*)\z/m)
if match
result = match[1] + result
separator_found = true
else
result = buf + result
file.seek(pos)
end
end
result
end
end
last_cmd = read_last_line(Pathname(histfile).expand_path)
$stdout.puts '# %s $ %s' % [Dir.pwd, last_cmd]
while buf = $stdin.read(1024)
$stdout.write(buf)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment