Skip to content

Commit

Permalink
use top bottom and middle commit symbols
Browse files Browse the repository at this point in the history
I like to use ┌ ├ └ for the commits, because it gives
me lines to follow visually. With flog v1 this works:

Plug 'rbong/vim-flog', { 'branch': 'v1' }
Plug 'TamaMcGlinn/flog-forest', { 'branch': 'v1' }

But with v2 the ability to customize the forest used in the background
was lost, so I have re-added this by checking if the commit has child
commits already drawn, and whether it has children that will later be
drawn, and still falling back to the • if neither (disconnected
commits).

The change to the MergeStart line just makes it a little more strict,
as it must be followed by a non-space character such as ──╮.
Otherwise, it thinks each middle-commit line is actually a
mergestart line and those commit lines become all-white.
  • Loading branch information
TamaMcGlinn committed Jun 14, 2024
1 parent 255631a commit c0dca86
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
35 changes: 33 additions & 2 deletions lua/flog/graph.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
local graph_error = 'flog: internal error drawing graph'

-- Init graph strings
local current_commit_str = ''
local top_commit_str = ''
local middle_commit_str = ''
local bottom_commit_str = ''
local disconnected_commit_str = ''
local commit_branch_str = ''
local commit_empty_str = ' '
local complex_merge_str_1 = '┬┊'
Expand All @@ -28,6 +31,21 @@ local missing_parent_str = '┊ '
local missing_parent_branch_str = ''
local missing_parent_empty_str = ' '

local function get_commit_string(is_top, is_bottom)
if not is_top and not is_bottom then
return middle_commit_str
end
if is_top and is_bottom then
return disconnected_commit_str
end
if is_top then
return top_commit_str
end
if is_bottom then
return bottom_commit_str
end
end

local function flog_get_graph(
enable_vim,
enable_nvim,
Expand Down Expand Up @@ -122,6 +140,8 @@ local function flog_get_graph(
local branch_hashes = {}
local branch_indexes = {}
local nbranches = 0
-- set of commits that have children already printed
local commit_hashes_with_children = {}

-- Draw graph

Expand Down Expand Up @@ -201,6 +221,11 @@ local function flog_get_graph(
parent_index = parent_index + 1
end

-- Mark parent commits as mentioned
for _, parent_hash in ipairs(parents) do
commit_hashes_with_children[parent_hash] = true
end

-- Traverse old and new branches

if enable_graph then
Expand All @@ -227,7 +252,13 @@ local function flog_get_graph(
is_commit = true
end

local current_commit_str
if is_commit then
-- identify symbol to draw for commit
local is_top = commit_hashes_with_children[commit_hash] == nil
local is_bottom = nparents == 0
current_commit_str = get_commit_string(is_top, is_bottom)

-- Count commit merge
nmerges_right = nmerges_right - 1
nmerges_left = nmerges_left + 1
Expand Down Expand Up @@ -294,7 +325,7 @@ local function flog_get_graph(
branch_indexes[parent_hash] = branch_index
branch_hashes[branch_index] = parent_hash

-- Update branch has
-- Update branch hash
branch_hash = parent_hash

-- Update the number of branches
Expand Down
4 changes: 2 additions & 2 deletions syntax/floggraph.vim
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,15 @@ for branch_idx in range(1, 9)
exec 'syntax match ' . branch . ' contained nextgroup=' . next_branch . ',' . next_branch . 'Commit,' . next_branch . 'MergeStart,' . next_branch . 'ComplexMergeStart,' . next_branch . 'MissingParentsStart,flogCollapsedCommit,@flogDiff /\v%( |%u2502 |%u2502$)/'

" Commit indicators
exec 'syntax match ' . branch . 'Commit contained nextgroup=' . next_branch . 'AfterCommit,@flogCommitInfo /\%u2022 /'
exec 'syntax match ' . branch . 'Commit contained nextgroup=' . next_branch . 'AfterCommit,@flogCommitInfo /\v%(%u2022|%u250c|%u251c|%u2514) /'
exec 'highlight link ' . branch . 'Commit flogCommit'

" Branches to the right of the commit indicator
exec 'syntax match ' . branch . 'AfterCommit contained nextgroup=' . next_branch . 'AfterCommit,@flogCommitInfo /\v%( |%u2502 |%u2502$)/'
exec 'highlight link ' . branch . 'AfterCommit ' . branch

" Start of a merge - saves the branch that the merge starts on (see below)
exec 'syntax match ' . branch . 'MergeStart contained nextgroup=' . next_merge_branch . ',' . next_merge_branch . 'End /\v%(%u251c|%u256d|%u2570)/'
exec 'syntax match ' . branch . 'MergeStart contained nextgroup=' . next_merge_branch . ',' . next_merge_branch . 'End /\v%(%u251c|%u256d|%u2570)[^ ]/'
exec 'highlight link ' . branch . 'MergeStart ' . branch

" Horizontal merge character
Expand Down

0 comments on commit c0dca86

Please sign in to comment.