Skip to content

Instantly share code, notes, and snippets.

@hrko
Last active February 2, 2019 14:01
Show Gist options
  • Save hrko/9becd912b14cdb605a8b6e60e66f9460 to your computer and use it in GitHub Desktop.
Save hrko/9becd912b14cdb605a8b6e60e66f9460 to your computer and use it in GitHub Desktop.
.zhistory を fish_history に変換するやつ

extended_history (タイムスタンプ付与)オプションが有効なzshで保存された履歴は考慮しない

#!/bin/ruby
require 'stringio'
File.open(File.expand_path('~/.zhistory')) do |f|
escaped = StringIO.new
# .zsh_historyのデコード
f.to_enum(:each_byte).tap do |enum|
processes = []
loop do
b = enum.next
# 0x83 を除いて、続くバイトの 6bit 目を反転
if b == 0x83
b = enum.next
b = (b ^ 0x20)
end
# \(0x5C) に続いて改行 (LF, 0x0A) があったら、 LF を n (0x6E) で置き換える
if b == 0x5C
b = enum.next
processes << 0x5C
if b == 0x0A
b = 0x6E
end
end
processes << b
end
escaped.write processes.pack('c*')
end
# fish_historyのyamlに変換
escaped.rewind
escaped.each_line do |command|
puts "- cmd: #{command}"
puts " when: 1548918011" # 任意の時間
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment