Class: Omglog::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/omglog.rb

Constant Summary collapse

CLEAR =
"\n----\n"
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/

Class Method Summary collapse

Class Method Details

.arrange_commit(commit, cols) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/omglog.rb', line 59

def self.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[-1, 0] = if commit[-1].length > room
      commit.pop[0...(room - 1)] + ''
    else
      commit.pop.ljust(room)
    end
  }
end

.log_cmdObject



35
36
37
# File 'lib/omglog.rb', line 35

def self.log_cmd
  @log_cmd ||= [LOG_CMD].concat(ARGV).join(' ')
end

.render_commit(commit, cols) ⇒ Object



52
53
54
55
56
57
# File 'lib/omglog.rb', line 52

def self.render_commit commit, cols
  row_highlight = commit[2][/[^\/]HEAD\b/] ? HIGHLIGHT : YELLOW
  [nil, row_highlight, GREEN, '', GREY].map {|c| "\e[#{c}m" if c }.zip(
    arrange_commit(commit, cols)
  ).join + "\e[m"
end

.runObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/omglog.rb', line 39

def self.run
  rows, cols = `tput lines; tput cols`.scan(/\d+/).map(&:to_i)
  `#{log_cmd} -#{rows}`.tap {|log|
    log_lines = Array.new(rows, '')
    log_lines.unshift *log.split("\n")

    print CLEAR + log_lines[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