Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix https://github.com/cmderdev/cmder/issues/2789 #2791

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 50 additions & 40 deletions vendor/clink.lua
Original file line number Diff line number Diff line change
Expand Up @@ -408,58 +408,72 @@ local function get_svn_status()
return true
end

---
-- Use a prompt coroutine to get git status in the background.
-- Cache the info so we can reuse it next time to reduce flicker.
---
local function get_git_info_table()
local info = clink_promptcoroutine(function ()
return get_git_status()
end)
if not info then
info = cached_info.git_info or {}
else
cached_info.git_info = info
end
return info
end

---
-- Get the status of working dir
-- @return {bool}
---
local last_git_status_time = nil
local last_git_status_setting = true
local function get_git_status_setting()
local time = os.clock()
local last_time = last_git_status_time
last_git_status_time = time
if last_time and time >= 0 and time - last_time < 10 then
return last_git_status_setting
end

-- When async prompt filtering is available, check the
-- prompt_overrideGitStatusOptIn config setting for whether to ignore the
-- cmder.status and cmder.cmdstatus git config opt-in settings.
if clink.promptcoroutine and io.popenyield and settings.get("prompt.async") then
if prompt_overrideGitStatusOptIn then
last_git_status_setting = true
return true
end
end

local gitStatusConfig = io.popen("git --no-pager config cmder.status 2>nul")

local gitStatusConfig = io_popenyield("git --no-pager config cmder.status 2>nul")
for line in gitStatusConfig:lines() do
if string.match(line, 'false') then
gitStatusConfig:close()
last_git_status_setting = false
return false
end
end
gitStatusConfig:close()

local gitCmdStatusConfig = io.popen("git --no-pager config cmder.cmdstatus 2>nul")
local gitCmdStatusConfig = io_popenyield("git --no-pager config cmder.cmdstatus 2>nul")
for line in gitCmdStatusConfig:lines() do
if string.match(line, 'false') then
gitCmdStatusConfig:close()
last_git_status_setting = false
return false
end
end
gitStatusConfig:close()
gitCmdStatusConfig:close()

last_git_status_setting = true
return true
end

---
-- Use a prompt coroutine to get git status in the background.
-- Cache the info so we can reuse it next time to reduce flicker.
---
local function get_git_info_table()
local info = clink_promptcoroutine(function ()
-- Use git status if allowed.
local cmderGitStatusOptIn = get_git_status_setting()
return cmderGitStatusOptIn and get_git_status() or {}
end)
if not info then
info = cached_info.git_info or {}
else
cached_info.git_info = info
end
return info
end

local function git_prompt_filter()

-- Don't do any git processing if the prompt doesn't want to show git info.
Expand All @@ -477,7 +491,6 @@ local function git_prompt_filter()

local git_dir = get_git_dir()
local color
cmderGitStatusOptIn = get_git_status_setting()
if git_dir then
local branch = get_git_branch(git_dir)
if branch then
Expand All @@ -487,28 +500,25 @@ local function git_prompt_filter()
cached_info.git_dir = git_dir
cached_info.git_branch = branch
end
-- Use git status if allowed.
if cmderGitStatusOptIn then
-- if we're inside of git repo then try to detect current branch
-- Has branch => therefore it is a git folder, now figure out status
local gitInfo = get_git_info_table()
local gitStatus = gitInfo.status
local gitConflict = gitInfo.conflict

if gitStatus == nil then
color = colors.nostatus
elseif gitStatus then
color = colors.clean
else
color = colors.dirty
end

if gitConflict then
color = colors.conflict
end
else
-- If we're inside of git repo then try to detect current branch
-- Has branch => therefore it is a git folder, now figure out status
local gitInfo = get_git_info_table()
local gitStatus = gitInfo.status
local gitConflict = gitInfo.conflict

if gitStatus == nil then
color = colors.nostatus
elseif gitStatus then
color = colors.clean
else
color = colors.dirty
end

if gitConflict then
color = colors.conflict
end

clink.prompt.value = string.gsub(clink.prompt.value, "{git}", " "..color.."("..verbatim(branch)..")")
return false
end
Expand Down