Skip to content

Commit

Permalink
More efficient git plug-in with 10 second refresh limit
Browse files Browse the repository at this point in the history
  • Loading branch information
curlpipe committed Nov 28, 2024
1 parent 9ee5c88 commit 6cf7113
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
59 changes: 36 additions & 23 deletions plugins/git.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--[[
Git v0.4
Git v0.5
A plug-in for git integration that provides features to:
- Choose which files to add to a commit
Expand All @@ -14,6 +14,7 @@ git = {
status = {},
icons = (git or { icons = false }).icons,
has_git = shell:output("git --version"):find("git version"),
last_update = nil,
}

function git:ready()
Expand All @@ -26,30 +27,35 @@ function git:repo_path()
end

function git:refresh_status()
local repo_path = self:repo_path()
local status_output = shell:output("git status --porcelain")
local status = {}
for line in status_output:gmatch("[^\r\n]+") do
local staged_status = line:sub(1, 1)
local unstaged_status = line:sub(2, 2)
local file_name = repo_path .. "/" .. line:sub(4)
local staged
local modified
if self.icons then
staged = "󰸩 "
modified = "󱇨 "
else
staged = "S"
modified = "M"
end
-- M = modified, S = staged
if staged_status ~= " " and staged_status ~= "?" then
status[file_name] = staged
elseif unstaged_status ~= " " or unstaged_status == "?" then
status[file_name] = modified
local duration_since_update = os.time(os.date("*t")) - os.time(self.last_update)
-- Only do a refresh every 10 seconds maximum
if self.last_update == nil or duration_since_update > 10 then
local repo_path = self:repo_path()
local status_output = shell:output("git status --porcelain")
local status = {}
for line in status_output:gmatch("[^\r\n]+") do
local staged_status = line:sub(1, 1)
local unstaged_status = line:sub(2, 2)
local file_name = repo_path .. "/" .. line:sub(4)
local staged
local modified
if self.icons then
staged = "󰸩 "
modified = "󱇨 "
else
staged = "S"
modified = "M"
end
-- M = modified, S = staged
if staged_status ~= " " and staged_status ~= "?" then
status[file_name] = staged
elseif unstaged_status ~= " " or unstaged_status == "?" then
status[file_name] = modified
end
end
self.status = status
self.last_update = os.date("*t")
end
self.status = status
end

function git:get_stats()
Expand Down Expand Up @@ -119,6 +125,12 @@ end
-- Initial status grab
after(0, "git_init")

-- When the user saves a document, force a refresh
event_mapping["ctrl_s"] = function()
git.last_update = nil
git:refresh_status()
end

-- Export the git command
commands["git"] = function(args)
-- Check if git is installed
Expand Down Expand Up @@ -194,6 +206,7 @@ commands["git"] = function(args)
end
end
-- Refresh state after a git command
git.last_update = nil
git:refresh_status()
end
end
2 changes: 1 addition & 1 deletion plugins/pomodoro.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Pomodoro Timer v0.1
A simple timer to help you space out your work with breaks
This technique is also said to help increase memory retention during study
--]]
]]--

-- Define our pomodoro state
pomodoro = {
Expand Down

0 comments on commit 6cf7113

Please sign in to comment.