Created
October 24, 2018 16:19
-
-
Save flightonary/f4bd0eb4c7ba402fd414b8a4fe9e6359 to your computer and use it in GitHub Desktop.
Convert zsh_history to fish_history (in case that zsh_history is old format)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# from https://qiita.com/troter/items/f011f3bf5afa72749731 | |
require 'stringio' | |
require 'date' | |
File.open(File.expand_path('~/.zsh_history')) do |f| | |
escaped = StringIO.new | |
# .zsh_historyのデコード | |
f.to_enum(:each_byte).tap do |enum| | |
processes = [] | |
loop do | |
b = enum.next | |
if b == 0x83 | |
b = enum.next | |
b = (b ^ 0x20) | |
end | |
processes << b | |
end | |
escaped.write processes.pack('c*') | |
end | |
# 雑にパースしてfish_historyのyamlに変換 | |
# ヒアドキュメントなどを使った複数行入力については諦めて標準エラー出力に出す | |
multi_line = "" | |
escaped.rewind | |
escaped.each_line do |l| | |
# unless l[0] == ':' | |
# $stderr.puts l | |
# next | |
# end | |
# command = l.split(/;/, 2).last | |
# time = l.split(/\:/, 3)[1] | |
# if time.nil? | |
# $stderr.puts l | |
# next | |
# end | |
l.strip! | |
if l.chars.last(2).join == "\\\\" | |
multi_line = multi_line + " " + l[0, l.length-2] | |
else | |
cmd = l | |
if multi_line != "" | |
cmd = multi_line | |
multi_line = "" | |
end | |
puts "- cmd: #{cmd}" | |
puts " when: #{Time.now.to_i}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment