LoginSignup
4
2

More than 5 years have passed since last update.

雑にzsh_historyをfish_historyに変換する

Posted at

zsh_historyにはASCII以外の文字がメタ文字を使ってエスケープされているので、適切にデコードしてからfishが読み取れるyamlに変換する。

convert.rb
require 'stringio'

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に変換
  # ヒアドキュメントなどを使った複数行入力については諦めて標準エラー出力に出す
  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

    puts "- cmd: #{command}"
    puts "  when: #{time.strip}"
  end
end
$ ruby convert.rb > ~/fish_history
$ cat ~/fish_history >> ~/.local/share/fish/fish_history

参考

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2