Last active
April 3, 2024 09:35
-
-
Save AndrewRadev/64e3c641cd0f258df082d0e34a4c106e to your computer and use it in GitHub Desktop.
Show colors in the sign column based on age of line
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Proof-of-concept, probably won't work for you, sorry | |
" Terminal colors, all grays, lighter to darker | |
let s:colors = range(232, 255) | |
augroup FugitiveColors | |
autocmd! | |
autocmd BufRead * call ShowBlameColors() | |
augroup END | |
function! ShowBlameColors() abort | |
if !exists('*FugitiveGitDir') | |
finish | |
endif | |
let git_dir = FugitiveGitDir() | |
if empty(git_dir) | |
return | |
endif | |
let blame = systemlist('git blame --date=unix ' .. expand('%:S')) | |
let relative_times = [] | |
let now = localtime() | |
" Find the timestamps | |
for line in blame | |
let ts = matchstr(line, '\d\+\ze\s\+\d\+)') | |
call add(relative_times, str2nr(ts)) | |
endfor | |
" Take time relative to latest one | |
let max_time = max(relative_times) | |
call map(relative_times, { _, time -> max_time - time}) | |
" Apply logarithm to put intervals in a closer range | |
call map(relative_times, { _, time -> time > 0 ? float2nr(round(5 * log(time))) : time }) | |
" Define a highlight group and sign for each distinct value | |
let index = 0 | |
for value in uniq(sort(copy(relative_times))) | |
let name = 'BlameColor' .. string(value) | |
exe 'highlight ' .. name .. ' ctermbg=' .. s:colors[index] .. ' ctermfg=white' | |
call sign_define(name, {'numhl': name, 'texthl': name, 'text': ' '}) | |
let index += 1 | |
if index >= len(s:colors) | |
" If we have too many distinct values, bail out | |
break | |
endif | |
endfor | |
" Place a sign on each line highlighted with the specific color | |
let lnum = 1 | |
for relative_time in relative_times | |
let name = 'BlameColor' .. string(relative_time) | |
call sign_place(0, '', name, bufnr(), {'lnum': lnum}) | |
let lnum += 1 | |
endfor | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment