Skip to content

Commit 82307bc

Browse files
committed
refactor: shortening of relative dates
1 parent dd9c0aa commit 82307bc

File tree

4 files changed

+24
-25
lines changed

4 files changed

+24
-25
lines changed

lua/tinygit/commands/commit.lua

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ function M.fixupCommit(opts)
186186
local result = vim.system({
187187
"git",
188188
"log",
189-
"-n" .. tostring(opts.selectFromLastXCommits),
189+
"--max-count=" .. opts.selectFromLastXCommits,
190190
"--format=" .. gitlogFormat,
191191
}):wait()
192192
if u.nonZeroExit(result) then return end
@@ -196,16 +196,17 @@ function M.fixupCommit(opts)
196196
local icon = require("tinygit.config").config.appearance.mainIcon
197197
local prompt = vim.trim(icon .. " Select commit to fixup")
198198
local commitFormatter = function(commitLine)
199-
local _, subject, date, nameAtCommit = unpack(vim.split(commitLine, "\t"))
200-
local displayLine = ("%s\t%s"):format(subject, date)
199+
local _, subject, relDate, nameAtCommit = unpack(vim.split(commitLine, "\t"))
200+
local shortRelDate = u.shortenRelativeDate(relDate)
201+
local displayLine = ("%s\t%s"):format(subject, shortRelDate)
201202
-- append name at commit, if it exists
202203
if nameAtCommit then displayLine = displayLine .. ("\t(%s)"):format(nameAtCommit) end
203204
return displayLine
204205
end
205206
local stylingFunc = function()
206-
local hl = require("tinygit.shared.highlights")
207-
hl.commitType()
208-
hl.inlineCodeAndIssueNumbers()
207+
local highlights = require("tinygit.shared.highlights")
208+
highlights.commitType()
209+
highlights.inlineCodeAndIssueNumbers()
209210
vim.fn.matchadd("Comment", [[\t.*$]])
210211
end
211212
local onChoice = function(commit)

lua/tinygit/commands/commit/preview.lua

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,13 @@ end
5757
---@return string[] logLines
5858
function M.getGitLog()
5959
local loglines = require("tinygit.config").config.commit.preview.loglines
60-
local args = { "git", "log", "--max-count=" .. loglines, "--format=%s %cr" }
60+
local args = { "git", "log", "--max-count=" .. loglines, "--format=%s %cr" } -- subject, date
6161
local lines = vim.split(u.syncShellCmd(args), "\n")
6262

63-
-- shorten units, like `minutes` to `min`
64-
lines = vim.tbl_map(function(line)
65-
local shortLine = line:gsub(" (%a+) ago$", function(unit)
66-
local shortUnit = (unit:match("%ai?n?") or "") -- 1 unit char (expect min)
67-
:gsub("m$", "mo") -- "month" -> "mo" to be distinguishable from "min"
68-
return shortUnit .. " ago"
69-
end)
70-
return shortLine
71-
end, lines)
72-
73-
return lines
63+
return vim.tbl_map(
64+
function(line) return line:gsub("%d+ %a+ ago$", u.shortenRelativeDate) end,
65+
lines
66+
)
7467
end
7568

7669
---@param bufnr number

lua/tinygit/shared/utils.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,16 @@ function M.intentToAddUntrackedFiles()
6060
end
6161
end
6262

63+
---@param longStr string
64+
---@return string shortened
65+
function M.shortenRelativeDate(longStr)
66+
local shortStr = (longStr:match("%d+ %ai?n?") or "") -- 1 unit char (expect min)
67+
:gsub("m$", "mo") -- "month" -> "mo" to keep it distinguishable from "min"
68+
:gsub(" ", "")
69+
:gsub("%d+s$", "just now") -- secs -> just now
70+
if shortStr ~= "just now" then shortStr = shortStr .. " ago" end
71+
return shortStr
72+
end
73+
6374
--------------------------------------------------------------------------------
6475
return M

lua/tinygit/statusline/blame.lua

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,14 @@ local function getBlame(bufnr)
2424

2525
local hash, author, relDate, msg = unpack(vim.split(stdout, "\t"))
2626
if vim.tbl_contains(config.ignoreAuthors, author) then return "" end
27+
local shortRelDate = u.shortenRelativeDate(relDate)
2728

2829
-- GUARD shallow and on first commit
2930
-- get first commit: https://stackoverflow.com/a/5189296/22114136
3031
local isOnFirstCommit = hash == u.syncShellCmd { "git", "rev-list", "--max-parents=0", "HEAD" }
3132
local shallowRepo = require("tinygit.shared.utils").inShallowRepo()
3233
if shallowRepo and isOnFirstCommit then return "" end
3334

34-
-- shorten the output
35-
local shortRelDate = (relDate:match("%d+ %wi?n?") or "") -- 1 unit char (expect min)
36-
:gsub("m$", "mo") -- "month" -> "mo" to be distinguishable from "min"
37-
:gsub(" ", "")
38-
:gsub("%d+s", "just now") -- secs -> just now
39-
if not shortRelDate:find("just now") then shortRelDate = shortRelDate .. " ago" end
40-
4135
if vim.list_contains(config.showOnlyTimeIfAuthor, author) then
4236
return vim.trim(("%s %s"):format(config.icon, shortRelDate))
4337
end

0 commit comments

Comments
 (0)