Skip to content

Commit

Permalink
Lazy load colors when first command is called
Browse files Browse the repository at this point in the history
  • Loading branch information
pwntester committed Sep 26, 2023
1 parent d1e52f9 commit 35a938e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
34 changes: 20 additions & 14 deletions lua/octo/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ local window = require "octo.ui.window"
local writers = require "octo.ui.writers"
local utils = require "octo.utils"
local config = require "octo.config"
local colors = require "octo.ui.colors"
local vim = vim

local M = {}
Expand Down Expand Up @@ -331,6 +332,11 @@ function M.process_varargs(repo, ...)
end

function M.octo(object, action, ...)
if not _G.octo_colors_loaded then
colors.setup()
_G.octo_colors_loaded = true
end

if not object then
if config.get_config().enable_builtin then
M.commands.actions()
Expand Down Expand Up @@ -388,14 +394,14 @@ function M.add_comment()
viewerCanDelete = true,
viewerDidAuthor = true,
reactionGroups = {
{ content = "THUMBS_UP", users = { totalCount = 0 } },
{ content = "THUMBS_UP", users = { totalCount = 0 } },
{ content = "THUMBS_DOWN", users = { totalCount = 0 } },
{ content = "LAUGH", users = { totalCount = 0 } },
{ content = "HOORAY", users = { totalCount = 0 } },
{ content = "CONFUSED", users = { totalCount = 0 } },
{ content = "HEART", users = { totalCount = 0 } },
{ content = "ROCKET", users = { totalCount = 0 } },
{ content = "EYES", users = { totalCount = 0 } },
{ content = "LAUGH", users = { totalCount = 0 } },
{ content = "HOORAY", users = { totalCount = 0 } },
{ content = "CONFUSED", users = { totalCount = 0 } },
{ content = "HEART", users = { totalCount = 0 } },
{ content = "ROCKET", users = { totalCount = 0 } },
{ content = "EYES", users = { totalCount = 0 } },
},
}

Expand Down Expand Up @@ -798,12 +804,12 @@ function M.create_pr(is_draft)

-- get remote branches
if
info == nil
or info.refs == nil
or info.refs.nodes == nil
or info == vim.NIL
or info.refs == vim.NIL
or info.refs.nodes == vim.NIL
info == nil
or info.refs == nil
or info.refs.nodes == nil
or info == vim.NIL
or info.refs == vim.NIL
or info.refs.nodes == vim.NIL
then
utils.error "Cannot grab remote branches"
return
Expand All @@ -819,7 +825,7 @@ function M.create_pr(is_draft)
local remote_branch = local_branch
if not remote_branch_exists then
local choice =
vim.fn.confirm("Remote branch '" .. local_branch .. "' does not exist. Push local one?", "&Yes\n&No\n&Cancel", 2)
vim.fn.confirm("Remote branch '" .. local_branch .. "' does not exist. Push local one?", "&Yes\n&No\n&Cancel", 2)
if choice == 1 then
local remote = "origin"
remote_branch = vim.fn.input {
Expand Down
4 changes: 2 additions & 2 deletions lua/octo/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ local gh = require "octo.gh"
local graphql = require "octo.gh.graphql"
local picker = require "octo.picker"
local reviews = require "octo.reviews"
local colors = require "octo.ui.colors"
local signs = require "octo.ui.signs"
local window = require "octo.ui.window"
local writers = require "octo.ui.writers"
local utils = require "octo.utils"
local vim = vim

_G.octo_repo_issues = {}
_G.octo_buffers = {}
_G.octo_colors_loaded = false

local M = {}

Expand All @@ -32,7 +33,6 @@ function M.setup(user_config)
config.setup(user_config or {})
signs.setup()
picker.setup()
colors.setup()
completion.setup()
folds.setup()
autocmds.setup()
Expand Down
14 changes: 8 additions & 6 deletions lua/octo/ui/colors.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local config = require "octo.config"
local vim = vim

local M = {}

Expand Down Expand Up @@ -139,10 +140,11 @@ end

function M.setup()
for name, v in pairs(get_hl_groups()) do
local fg = v.fg and " guifg=" .. v.fg or ""
local bg = v.bg and " guibg=" .. v.bg or ""
local fg = v.fg and " guifg=" .. v.fg or ""
local bg = v.bg and " guibg=" .. v.bg or ""
local gui = v.gui and " gui=" .. v.gui or ""
vim.cmd("hi def Octo" .. name .. fg .. bg .. gui)
local cmd = "hi def Octo" .. name .. fg .. bg .. gui
vim.cmd(cmd)
end

for from, to in pairs(get_hl_links()) do
Expand All @@ -166,7 +168,7 @@ local function color_is_bright(r, g, b)
-- Counting the perceptive luminance - human eye favors green color
local luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255
if luminance > 0.5 then
return true -- Bright colors, black font
return true -- Bright colors, black font
else
return false -- Dark colors, white font
end
Expand All @@ -176,8 +178,8 @@ function M.get_background_color_of_highlight_group(highlight_group_name)
local highlight_group = vim.api.nvim_get_hl_by_name(highlight_group_name, true)
local highlight_group_normal = vim.api.nvim_get_hl_by_name("Normal", true)
local background_color = highlight_group.background
or highlight_group_normal.background
or highlight_group_normal.foreground
or highlight_group_normal.background
or highlight_group_normal.foreground
if background_color then
return string.format("#%06x", background_color)
else
Expand Down

0 comments on commit 35a938e

Please sign in to comment.