Skip to content

Commit 829cfa5

Browse files
committed
Implement a toggle to show author or date of commit
Signed-off-by: Aaron Hallaert <[email protected]>
1 parent de813bc commit 829cfa5

File tree

7 files changed

+91
-16
lines changed

7 files changed

+91
-16
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ _Grep behaviour_: filter on added, updated or removed code (log content: `-G` op
3535
- `<C-e>` show the entire commit for all files in neovim with diff plugin
3636
- `<C-o>` Open the selected commit in the browser
3737
- `<C-y>` copy the commit hash to clipboard
38+
- `<C-w>` toggle date and author in entry
3839

3940
### 2. search_log_content_file -- Search in file log content
4041

@@ -49,6 +50,7 @@ _Grep behaviour_: filter on added, updated or removed code (log content: `-G` op
4950
- `<C-e>` show the entire commit for all files in neovim with diff plugin
5051
- `<C-o>` Open the selected commit in the browser
5152
- `<C-y>` copy the commit hash to clipboard
53+
- `<C-w>` toggle date and author in entry
5254

5355
### 3. diff_commit_file -- Diff current file with commit
5456

@@ -63,6 +65,7 @@ _Grep behaviour_: filter on commit message.
6365
- `<C-e>` show the entire commit for all files in neovim with diff plugin
6466
- `<C-o>` Open the selected commit in the browser
6567
- `<C-y>` copy the commit hash to clipboard
68+
- `<C-w>` toggle date and author in entry
6669

6770
### 4. diff_commit_line -- Diff current file with selected line history
6871

@@ -79,6 +82,7 @@ _Grep behaviour_: filter on commit message.
7982
- `<C-e>` show the entire commit for all files in neovim with diff plugin
8083
- `<C-o>` opens a the selected commit in the browser
8184
- `<C-y>` copy the commit hash to clipboard
85+
- `<C-w>` toggle date and author in entry
8286

8387
### 5. diff_branch_file -- Diff file with branch
8488

lua/advanced_git_search/fzf/mappings/init.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ local global_actions = require("advanced_git_search.actions")
55
local file_utils = require("advanced_git_search.utils.file")
66
local git_utils = require("advanced_git_search.utils.git")
77

8+
---FZF: <C-w> Toggle date or author in picker entry
9+
---@return table
10+
M.toggle_entry_value = function()
11+
return {
12+
["ctrl-w"] = {
13+
fn = function(_, _)
14+
require("advanced_git_search.fzf.pickers.utils").toggle_show_date_instead_of_author()
15+
end,
16+
reload = true,
17+
},
18+
}
19+
end
20+
821
---FZF: <C-o> Opens the selected commit in browser
922
---@return table
1023
M.open_commit_in_brower = function()

lua/advanced_git_search/fzf/pickers/init.lua

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ M.search_log_content = function()
2323
fzf_mappings.open_commit_in_brower(),
2424
fzf_mappings.open_diff_buffer_with_selected_commit(bufnr),
2525
fzf_mappings.show_entire_commit(),
26-
fzf_mappings.copy_commit_hash()
26+
fzf_mappings.copy_commit_hash(),
27+
fzf_mappings.toggle_entry_value()
2728
),
2829
}
2930

@@ -37,7 +38,7 @@ M.search_log_content_file = function()
3738

3839
local opts = {
3940
prompt = "Log> ",
40-
exec_empty_query = false,
41+
exec_empty_query = true,
4142
func_async_callback = false,
4243
fzf_opts = {
4344
["--preview"] = fzf_previewers.git_diff_content_previewer(),
@@ -50,7 +51,8 @@ M.search_log_content_file = function()
5051
fzf_mappings.open_commit_in_brower(),
5152
fzf_mappings.open_diff_buffer_with_selected_commit(bufnr),
5253
fzf_mappings.show_entire_commit(),
53-
fzf_mappings.copy_commit_hash()
54+
fzf_mappings.copy_commit_hash(),
55+
fzf_mappings.toggle_entry_value()
5456
),
5557
}
5658

@@ -88,7 +90,8 @@ M.diff_commit_line = function()
8890
fzf_mappings.open_commit_in_brower(),
8991
fzf_mappings.open_diff_buffer_with_selected_commit(bufnr),
9092
fzf_mappings.show_entire_commit(),
91-
fzf_mappings.copy_commit_hash()
93+
fzf_mappings.copy_commit_hash(),
94+
fzf_mappings.toggle_entry_value()
9295
),
9396
}
9497

@@ -115,7 +118,8 @@ M.diff_commit_file = function()
115118
fzf_mappings.open_commit_in_brower(),
116119
fzf_mappings.open_diff_buffer_with_selected_commit(bufnr),
117120
fzf_mappings.show_entire_commit(),
118-
fzf_mappings.copy_commit_hash()
121+
fzf_mappings.copy_commit_hash(),
122+
fzf_mappings.toggle_entry_value()
119123
),
120124
}
121125

lua/advanced_git_search/fzf/pickers/utils.lua

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ local color = require("fzf-lua").utils.ansi_codes
22
local utils = require("advanced_git_search.utils")
33

44
local M = {}
5+
local show_date_instead_of_author = false
6+
M.toggle_show_date_instead_of_author = function()
7+
show_date_instead_of_author = not show_date_instead_of_author
8+
end
59

610
M.make_entry = function(entry)
711
if entry == "" or entry == nil then
@@ -13,7 +17,7 @@ M.make_entry = function(entry)
1317
local split = utils.split_string(cleaned, "_")
1418
local attrs = utils.split_string(split[1])
1519
local hash = attrs[1]
16-
-- local date = attrs[2]
20+
local date = attrs[2]
1721
local author = ""
1822
for i = 3, #attrs do
1923
author = author .. attrs[i] .. " "
@@ -27,9 +31,17 @@ M.make_entry = function(entry)
2731
end
2832

2933
-- NOTE: make sure the first value is the commit hash
30-
return color.magenta(hash)
31-
.. color.cyan(" @" .. author)
32-
.. color.yellow(message)
34+
local final_entry = ""
35+
if show_date_instead_of_author then
36+
final_entry = color.magenta(hash)
37+
.. color.cyan(" " .. date)
38+
.. color.yellow(message)
39+
else
40+
final_entry = color.magenta(hash)
41+
.. color.cyan(" @" .. author)
42+
.. color.yellow(message)
43+
end
44+
return final_entry
3345
end
3446

3547
M.make_reflog_entry = function(entry)

lua/advanced_git_search/telescope/finders/utils.lua

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
local utils = require("advanced_git_search.utils")
22
local entry_display = require("telescope.pickers.entry_display")
33

4+
local show_date_instead_of_author = false
5+
46
local M = {}
57
local last_prompt = nil
68

9+
M.toggle_show_date_instead_of_author = function()
10+
show_date_instead_of_author = not show_date_instead_of_author
11+
end
12+
713
--- Parse "--format=%C(auto)%h %as %C(green)%an _ %Creset %s" to table
814
--- with opts: commit_hash, date, author, message, prompt
915
--- @param entry string
@@ -13,7 +19,7 @@ M.git_log_entry_maker = function(entry)
1319
local cleaned = string.gsub(entry, "'", "")
1420
local split = utils.split_string(cleaned, "_")
1521
local attrs = utils.split_string(split[1])
16-
local hash = attrs[1]
22+
local hash = string.sub(attrs[1], 1, 7)
1723
local date = attrs[2]
1824
local author = attrs[3]
1925
for i = 4, #attrs do
@@ -28,21 +34,42 @@ M.git_log_entry_maker = function(entry)
2834
end
2935
end
3036

37+
local second_width = 10
38+
if show_date_instead_of_author then
39+
second_width = 10
40+
else
41+
second_width = #author
42+
end
43+
3144
local displayer = entry_display.create({
3245
separator = " ",
3346
items = {
3447
{ width = 7 },
35-
{ width = 7 },
48+
{ width = second_width },
3649
{ remaining = true },
3750
},
3851
})
3952

4053
local make_display = function(display_entry)
41-
return displayer({
42-
{ display_entry.opts.commit_hash, "TelescopeResultsIdentifier" },
43-
{ display_entry.opts.author, "TelescopeResultsVariable" },
44-
{ display_entry.opts.message, "TelescopeResultsConstant" },
45-
})
54+
if show_date_instead_of_author then
55+
return displayer({
56+
{
57+
display_entry.opts.commit_hash,
58+
"TelescopeResultsIdentifier",
59+
},
60+
{ display_entry.opts.date, "TelescopeResultsVariable" },
61+
{ display_entry.opts.message, "TelescopeResultsConstant" },
62+
})
63+
else
64+
return displayer({
65+
{
66+
display_entry.opts.commit_hash,
67+
"TelescopeResultsIdentifier",
68+
},
69+
{ display_entry.opts.author, "TelescopeResultsVariable" },
70+
{ display_entry.opts.message, "TelescopeResultsConstant" },
71+
})
72+
end
4673
end
4774

4875
return {

lua/advanced_git_search/telescope/mappings/init.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ M.omnimap = omnimap
1818

1919
-- create a local function and assign it to a map to get which_key description
2020
-------------------------------------------------------------------------------
21+
local toggle_date_author = function(prompt_bufnr)
22+
require("advanced_git_search.telescope.finders.utils").toggle_show_date_instead_of_author()
23+
action_state.get_current_picker(prompt_bufnr):refresh()
24+
end
25+
26+
M.toggle_entry_value = function(map)
27+
omnimap(map, "<C-w>", toggle_date_author)
28+
end
29+
30+
-------------------------------------------------------------------------------
31+
2132
local open_commit_in_browser = function(prompt_bufnr)
2233
actions.close(prompt_bufnr)
2334
local selection = action_state.get_selected_entry()

lua/advanced_git_search/telescope/pickers/init.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ M.diff_commit_line = function()
101101
telescope_ags_mappings.open_selected_commit_in_browser(map)
102102
telescope_ags_mappings.copy_commit_hash_to_clipboard(map)
103103
telescope_ags_mappings.show_entire_commit(map)
104+
telescope_ags_mappings.toggle_entry_value(map)
104105
return true
105106
end,
106107
}, theme_opts))
@@ -128,6 +129,7 @@ M.search_log_content = function()
128129
telescope_ags_mappings.open_selected_commit_in_browser(map)
129130
telescope_ags_mappings.copy_commit_hash_to_clipboard(map)
130131
telescope_ags_mappings.show_entire_commit(map)
132+
telescope_ags_mappings.toggle_entry_value(map)
131133
return true
132134
end,
133135
}, theme_opts))
@@ -158,6 +160,7 @@ M.search_log_content_file = function()
158160
telescope_ags_mappings.open_selected_commit_in_browser(map)
159161
telescope_ags_mappings.copy_commit_hash_to_clipboard(map)
160162
telescope_ags_mappings.show_entire_commit(map)
163+
telescope_ags_mappings.toggle_entry_value(map)
161164

162165
return true
163166
end,
@@ -185,6 +188,7 @@ M.diff_commit_file = function()
185188
map
186189
)
187190
telescope_ags_mappings.show_entire_commit(map)
191+
telescope_ags_mappings.toggle_entry_value(map)
188192
telescope_ags_mappings.open_selected_commit_in_browser(map)
189193
telescope_ags_mappings.copy_commit_hash_to_clipboard(map)
190194

0 commit comments

Comments
 (0)