Skip to content

Instantly share code, notes, and snippets.

@kyanagi
Created May 6, 2022 00:58
Show Gist options
  • Save kyanagi/2b2714831802aa666b42f2d37a4005b9 to your computer and use it in GitHub Desktop.
Save kyanagi/2b2714831802aa666b42f2d37a4005b9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Merge zsh's history files and print them to stdout.
# Entries are sorted by timestamp.
#
# usage:
# merge-zsh-history file1 file2 ...
# format of history entry
#
# single line
# : 1400003117:0;cp foo bar
#
# multi lines
# : 1400041717:0;ruby -ve 'def f(x); end\
# f(a: 1,\
# b: 2,\
# )'
entries = []
ARGV.each do |arg|
File.open(arg, 'rb') do |f|
while line = f.gets
lines = [line]
while lines.last.end_with?("\\\n")
lines << f.gets
end
time = lines[0].scan(/\A: ([0-9]+):/).flatten[0]
if time.nil?
raise "format error: #{lines.join}"
end
time = time.to_i
entries << [time, lines]
end
end
entries.sort_by!(&:first)
end
entries.each do |_, lines|
print lines.join
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment