Skip to content

Commit

Permalink
feat: bg color ^& fix exec (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
linrongbin16 authored Aug 5, 2023
1 parent e1493ae commit 1a86b34
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 46 deletions.
4 changes: 4 additions & 0 deletions lua/fzfx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ local function setup(options)
end
vim.env._FZFX_NVIM_DEBUG_ENABLE = configs.debug.enable and 1 or 0

-- executable
require("fzfx.shell").nvim_exec()
require("fzfx.shell").fzf_exec()

-- files
require("fzfx.files").setup()

Expand Down
97 changes: 62 additions & 35 deletions lua/fzfx/color.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,70 +37,97 @@ local function get_color(attr, group)
return nil
end

--- @param color string|nil
--- @param color string
--- @param fg boolean
--- @return string|nil
local function csi(color, fg)
if type(color) ~= "string" then
return nil
end
local code = fg and 38 or 48
local r, g, b = color:match("#(..)(..)(..)")
if not r or not g or not b then
if r and g and b then
r = tonumber(r, 16)
g = tonumber(g, 16)
b = tonumber(b, 16)
local result = string.format("%d;2;%d;%d;%d", code, r, g, b)
log.debug(
"|fzfx.color - csi| rgb, color:%s, fg:%s, result:%s",
vim.inspect(color),
vim.inspect(fg),
vim.inspect(result)
)
return result
else
local result = string.format("%d;5;%s", code, color)
log.debug(
"|fzfx.color - csi| fallback, color:%s, fg:%s, result:nil",
"|fzfx.color - csi| non-rgb, color:%s, fg:%s, result:%s",
vim.inspect(color),
vim.inspect(fg)
vim.inspect(fg),
vim.inspect(result)
)
return nil
return result
end
r = tonumber(r, 16)
g = tonumber(g, 16)
b = tonumber(b, 16)
local result = string.format("%d;2;%d;%d;%d", code, r, g, b)
log.debug(
"|fzfx.color - csi| color:%s, fg:%s, result:%s",
vim.inspect(color),
vim.inspect(fg),
vim.inspect(result)
)
return result
end

--- @param text string
--- @param name string
--- @param group string
--- @return string
local function ansi(text, name, group)
local color = get_color("fg", group)
local fg = get_color("fg", group)
local fgcolor = nil
if type(fg) == "string" then
fgcolor = csi(fg, true)
log.debug(
"|fzfx.color - ansi| rgb, text:%s, name:%s, group:%s, fg:%s, fgcolor:%s",
vim.inspect(text),
vim.inspect(name),
vim.inspect(group),
vim.inspect(fg),
vim.inspect(fgcolor)
)
else
fgcolor = AnsiCode[name]
log.debug(
"|fzfx.color - ansi| ansi, text:%s, name:%s, group:%s, fg:%s, fgcolor:%s",
vim.inspect(text),
vim.inspect(name),
vim.inspect(group),
vim.inspect(fg),
vim.inspect(fgcolor)
)
end

local rgbcolor = csi(color, true)
if type(rgbcolor) == "string" and string.len(rgbcolor) > 0 then
local result = string.format("[%sm%s", rgbcolor, text)
local bg = get_color("bg", group)
local finalcolor = nil
if type(bg) == "string" then
local bgcolor = csi(bg, false)
log.debug(
"|fzfx.color - ansi| text:%s, name:%s, group:%s, fg:%s, rgbcolor:%s, result:%s",
"|fzfx.color - ansi| rgb, text:%s, name:%s, group:%s, bg:%s, bgcolor:%s",
vim.inspect(text),
vim.inspect(name),
vim.inspect(group),
vim.inspect(color),
vim.inspect(rgbcolor),
vim.inspect(result)
vim.inspect(bg),
vim.inspect(bgcolor)
)
return result
finalcolor = string.format("%s;%s", fgcolor, bgcolor)
else
log.debug(
"|fzfx.color - ansi| ansi, text:%s, name:%s, group:%s, bg:%s",
vim.inspect(text),
vim.inspect(name),
vim.inspect(group),
vim.inspect(bg)
)
finalcolor = fgcolor
end

local ansicolor = AnsiCode[name]
local result = string.format("[%sm%s", ansicolor, text)
log.debug(
"|fzfx.color - ansi| text:%s, name:%s, group:%s, fg:%s, ansicolor:%s, result:%s",
"|fzfx.color - ansi| ansi, finalcolor:%s",
vim.inspect(text),
vim.inspect(name),
vim.inspect(group),
vim.inspect(color),
vim.inspect(ansicolor),
vim.inspect(result)
vim.inspect(bg)
)
return result
return string.format("[%sm%s[0m", finalcolor, text)
end

local M = {}
Expand Down
17 changes: 6 additions & 11 deletions lua/fzfx/shell.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@ local log = require("fzfx.log")
local path = require("fzfx.path")
local conf = require("fzfx.config")

local Context = {
--- @type string|nil
nvim_path = nil,
--- @type string|nil
fzf_path = nil,
}

--- @return string|nil
local function nvim_exec()
local exe_list = {}
table.insert(exe_list, conf.get_config().env.nvim)
table.insert(exe_list, vim.v.argv[1])
table.insert(exe_list, vim.env.VIM)
table.insert(exe_list, "nvim")
for _, e in exe_list do
for _, e in ipairs(exe_list) do
if e ~= nil and vim.fn.executable(e) > 0 then
return e
end
Expand All @@ -29,14 +22,14 @@ end
local function fzf_exec()
local exe_list = {}
table.insert(exe_list, conf.get_config().env.fzf)
table.insert(exe_list, vim.fn["fzf#fzf_exec"]())
table.insert(exe_list, vim.fn["fzf#exec"]())
table.insert(exe_list, "fzf")
for _, e in exe_list do
for _, e in ipairs(exe_list) do
if e ~= nil and vim.fn.executable(e) > 0 then
return e
end
end
log.throw("error! failed to found executable 'nvim' on path!")
log.throw("error! failed to found executable 'fzf' on path!")
return nil
end

Expand All @@ -52,6 +45,8 @@ local function make_lua_command(...)
end

local M = {
nvim_exec = nvim_exec,
fzf_exec = fzf_exec,
make_lua_command = make_lua_command,
}

Expand Down

0 comments on commit 1a86b34

Please sign in to comment.