Skip to content

Instantly share code, notes, and snippets.

@aereal
Created December 1, 2012 18:06
Show Gist options
  • Save aereal/4183634 to your computer and use it in GitHub Desktop.
Save aereal/4183634 to your computer and use it in GitHub Desktop.
module History
module Shell
def zsh_history_file
@zsh_history_file ||= File.expand_path('~/.zsh_history')
end
def zsh_history
@zsh_history ||= File.read(zsh_history_file)
end
def encoded_zsh_history
@encoded_zsh_history ||= zsh_history.encode('UTF-8', 'ASCII-8BIT', undef: :replace, invalid: :replace, replace: '')
end
def history
@history ||= encoded_zsh_history.split(/\n/).map(&:strip)
end
def command_lines
@command_lines ||= history.map {|line| line.split(';').last.strip }
end
def command_history(pattern = nil)
(pattern ? command_lines.grep(pattern) : command_lines).map {|command_line| command_line.split(/\s+/) }
end
end
module Statistics
def stats(population, keys)
Hash[keys.map {|key| [key, population.count(key)] }]
end
end
module Git
include Shell
include Statistics
extend self
def git_command_history
@git_command_history ||= command_history(/^git\s/)
end
def git_subcommands
@git_subcommands ||= git_command_history.map {|command| command.find {|element| element != 'git' && /^\w/ === element } }.compact
end
def git_subcommands_stats
stats(git_subcommands, git_subcommands.uniq)
end
def git_aliases
@git_aliases ||= `git config --global --list | grep '^alias[.]' | cut -d. -f2 | cut -d= -f1`.split(/\n/)
end
def git_aliases_stats
stats(git_subcommands, git_aliases)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment