Skip to content

Commit

Permalink
fix: disable gh/glab cli color output #53
Browse files Browse the repository at this point in the history
Follow up to #71
  • Loading branch information
petertriho committed Oct 27, 2024
1 parent 3511428 commit ec04903
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
9 changes: 6 additions & 3 deletions lua/cmp_git/sources/github.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ local github_url = function(git_host, path)
end

local get_items = function(callback, gh_args, curl_url, handle_item, handle_parsed)
local gh_job = utils.build_job("gh", callback, gh_args, handle_item, handle_parsed)
local gh_job = utils.build_job("gh", gh_args, {
GITHUB_API_TOKEN = vim.fn.getenv("GITHUB_API_TOKEN"),
CLICOLOR = 0, -- disables color output to avoid parsing errors
}, callback, handle_item, handle_parsed)

local curl_args = {
"curl",
Expand All @@ -57,7 +60,7 @@ local get_items = function(callback, gh_args, curl_url, handle_item, handle_pars
table.insert(curl_args, authorization_header)
end

local curl_job = utils.build_job("curl", callback, curl_args, handle_item, handle_parsed)
local curl_job = utils.build_job("curl", curl_args, nil, callback, handle_item, handle_parsed)

return utils.chain_fallback(gh_job, curl_job)
end
Expand Down Expand Up @@ -148,7 +151,7 @@ local get_issues_job = function(callback, git_info, trigger_char, config)
end

local use_gh_default_repo_if_set = function(git_info)
local gh_default_repo = vim.fn.system({ 'gh', 'repo', 'set-default', '--view' })
local gh_default_repo = vim.fn.system({ "gh", "repo", "set-default", "--view" })
if vim.v.shell_error ~= 0 then
return git_info
end
Expand Down
8 changes: 5 additions & 3 deletions lua/cmp_git/sources/gitlab.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local utils = require("cmp_git.utils")
local sort = require("cmp_git.sort")
local log = require("cmp_git.log")
local format = require("cmp_git.format")

Expand Down Expand Up @@ -33,7 +32,10 @@ local get_project_id = function(git_info)
end

local get_items = function(callback, glab_args, curl_url, handle_item)
local glab_job = utils.build_job("glab", callback, glab_args, handle_item)
local glab_job = utils.build_job("glab", glab_args, {
GITLAB_TOKEN = vim.fn.getenv("GITLAB_TOKEN"),
NO_COLOR = 1, -- disables color output to avoid parsing errors
}, callback, handle_item)

curl_args = {
"-s",
Expand All @@ -47,7 +49,7 @@ local get_items = function(callback, glab_args, curl_url, handle_item)
table.insert(curl_args, authorization_header)
end

local curl_job = utils.build_job("curl", callback, curl_args, handle_item)
local curl_job = utils.build_job("curl", curl_args, nil, callback, handle_item)

return utils.chain_fallback(glab_job, curl_job)
end
Expand Down
11 changes: 10 additions & 1 deletion lua/cmp_git/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,25 @@ M.get_cwd = function()
return vim.fn.getcwd()
end

M.build_job = function(exec, callback, args, handle_item, handle_parsed)
M.build_job = function(exec, args, env, callback, handle_item, handle_parsed)
-- TODO: Find a nicer way, that we can keep chaining jobs at call side
if vim.fn.executable(exec) ~= 1 or not args then
log.fmt_debug("Can't work with %s for this call", exec)
return nil
end

local job_env = nil
if env ~= nil then
-- NOTE: setting env causes it to not inherit it from the parent environment
vim.tbl_extend("force", env, {
path = vim.fn.getenv("PATH"),
})
end

return Job:new({
command = exec,
args = args,
env = job_env,
cwd = M.get_cwd(),
on_exit = vim.schedule_wrap(function(job, code)
if code ~= 0 then
Expand Down

0 comments on commit ec04903

Please sign in to comment.