ninjatimeline.lua (1160B)
1 #! /usr/bin/lua 2 3 local log = io.open( ".ninja_log", "r" ):read( "*all" ) 4 5 local edges = { } 6 7 for line in log:gmatch( "([^\n]+)" ) do 8 local start, finish, target = line:match( "^(%d+)%s+(%d+)%s+%d+%s+(%S+)" ) 9 if start then 10 table.insert( edges, { 11 start = tonumber( start ), 12 dt = tonumber( finish ) - tonumber( start ), 13 target = target:match( "[^/]+$" ), 14 } ) 15 end 16 end 17 18 table.sort( edges, function( a, b ) 19 return a.start < b.start 20 end ) 21 22 local function term_width() 23 local p = io.popen( "tput cols", "r" ) 24 if not p then 25 return 26 end 27 28 local c = p:read( "*all" ) 29 p:close() 30 31 return tonumber( c ) 32 end 33 34 local notches = term_width() 35 if notches then 36 notches = notches - 33 37 else 38 notches = 200 39 end 40 41 local total_duration = edges[ #edges ].start + edges[ #edges ].dt 42 local c = 0 43 for _, e in ipairs( edges ) do 44 local pre = math.floor( notches * e.start / total_duration ) 45 local width = math.max( 0, math.floor( notches * e.dt / total_duration - 2 ) ) 46 47 local color = ( "\027[0;%dm" ):format( c + 31 ) 48 c = ( c + 1 ) % 7 49 50 print( ( "%s%25s: %s[%s] %.2fs" ):format( color, e.target, string.rep( " ", pre ), string.rep( ">", width ), e.dt / 1000 ) ) 51 end