Skip to content

Instantly share code, notes, and snippets.

@pimeys
Created January 4, 2012 09:21
Show Gist options
  • Save pimeys/1559259 to your computer and use it in GitHub Desktop.
Save pimeys/1559259 to your computer and use it in GitHub Desktop.
omglog for linux
#!/usr/bin/env ruby
# coding: utf-8
require 'rb-inotify'
CLEAR = "\n----\n"
YELLOW, BLUE, GREY, HIGHLIGHT = '0;33', '0;34', '0;90', '1;30;47'
SHORTEST_MESSAGE = 12
LOG_CMD = %{git log --all --date-order --graph --color --pretty="format: \2%h\3\2%d\3\2 %an, %ar\3\2 %s\3"}
LOG_REGEX = /(.*)\u0002(.*)\u0003\u0002(.*)\u0003\u0002(.*)\u0003\u0002(.*)\u0003/
def omglog
rows, cols = `tput lines; tput cols`.scan(/\d+/).map(&:to_i)
`#{LOG_CMD} -#{rows}`.tap {|log|
print CLEAR + log.split("\n")[0...(rows - 1)].map {|l|
commit = l.scan(LOG_REGEX).flatten.map(&:to_s)
commit.any? ? render_commit(commit, cols) : l
}.join("\n") + "\n" + "\e[#{GREY}mupdated #{Time.now.strftime("%a %H:%M:%S")}\e[m ".rjust(cols + 8)
}
end
def render_commit commit, cols
row_highlight = commit[2][/[^\/]HEAD\b/] ? HIGHLIGHT : YELLOW
[nil, row_highlight, BLUE, '', GREY].map {|c| "\e[#{c}m" if c }.zip(
arrange_commit(commit, cols)
).join + "\e[m"
end
def arrange_commit commit, cols
commit[0].chomp!(' ')
commit[-2].sub!(/(\d+)\s(\w)[^\s]+ ago/, '\1\2 ago')
room = [cols - [commit[0].gsub(/\e\[[\d;]*m/, ''), commit[1..-2]].flatten.map(&:length).inject(&:+), SHORTEST_MESSAGE].max
commit.tap {|commit|
commit[3, 0] = if commit[-1].length > room
commit.pop[0...(room - 1)] + '…'
else
commit.pop.ljust(room)
end
}
end
def on_change &block
INotify::Notifier.new.tap do |notifier|
notifier.watch('.git', :modify, :recursive, &block)
['TERM', 'INT', 'QUIT'].each do |signal|
trap(signal) do
notifier.stop
exit(0)
end
end
notifier.run
end
end
abort("Run omglog at the root of the git repo you'd like to watch.") if (ARGV & %w[-h --help help]).any?
abort("The current directory doesn't look like the root of a git repo.") unless File.directory?('.git')
omglog
on_change { omglog }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment