Skip to content

Commit

Permalink
fix(renderer): Made icons optional
Browse files Browse the repository at this point in the history
Added a new `icons` option to `code_blocks` to disable icons.

Ref: #68, #83
  • Loading branch information
OXY2DEV committed Aug 7, 2024
1 parent ca5f617 commit 6671dd4
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
3 changes: 3 additions & 0 deletions lua/definitions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@
--- Name of the style to use
---@field style string
---
--- Used for disabling icons on language view
---@field icons boolean?
---
--- Default highlight group used by the rest of the options
--- Used for highlighting the lines when the style is "simple"
---@field hl string?
Expand Down
1 change: 1 addition & 0 deletions lua/markview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,7 @@ markview.configuration = {

code_blocks = {
enable = true,
icons = true,

style = "language",
hl = "MarkviewCode",
Expand Down
37 changes: 30 additions & 7 deletions lua/markview/renderer.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
local renderer = {};
local devicons = require("nvim-web-devicons");
local devicons_loaded, devicons = pcall(require, "nvim-web-devicons");

local entites = require("markview.entites");
local languages = require("markview.languages");

renderer.get_icon = function (language, config_table)
if config_table.icons == false then
return "", "Normal";
end

if devicons_loaded then
return devicons.get_icon(nil, language, { default = true })
end

return "󰡯", "Normal";
end

_G.__markview_views = {};

Expand Down Expand Up @@ -1042,10 +1053,11 @@ renderer.render_code_blocks = function (buffer, content, config_table)
end
elseif config_table.style == "language" then
local language = languages.get_fs(content.language);
local icon, hl = devicons.get_icon(nil, language, { default = true });
local icon, hl = renderer.get_icon(content.language, config_table);
local block_length = content.largest_line;

local languageName;
local icon_section = icon ~= "" and " " .. icon .. " " or " ";

if config_table.language_names ~= nil then
for _, lang in ipairs(config_table.language_names) do
Expand All @@ -1063,16 +1075,27 @@ renderer.render_code_blocks = function (buffer, content, config_table)
block_length = config_table.min_width
end

local lang_width = vim.fn.strchars(" " .. icon .. " " .. languageName .. " ");
local info_width = math.min(vim.fn.strchars(content.info_string or ""), content.largest_line);
local lang_width = vim.fn.strchars(icon_section .. languageName .. " ");

if config_table.language_direction == nil or config_table.language_direction == "left" then
vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, content.row_start, content.col_start, {
end_col = vim.fn.strchars(content.info_string),
conceal = "",
});

local rendered_info = vim.fn.strcharpart(content.block_info or "", 0, block_length - lang_width + ((config_table.pad_amount or 1) * 2) - 4);

if content.block_info ~= "" and vim.fn.strchars(content.block_info) > (block_length - lang_width - ((content.pad_amount or 1) * 2)) then
rendered_info = rendered_info .. "...";
end

vim.api.nvim_buf_set_extmark(buffer, renderer.namespace, content.row_start, content.col_start + 3 + vim.fn.strlen(content.language), {
virt_text_pos = config_table.position or "inline",
virt_text = {
{ " " .. icon .. " ", set_hl(hl) },
{ icon_section, set_hl(hl) },
{ languageName .. " ", set_hl(config_table.name_hl) or set_hl(hl) },
{ string.rep(config_table.pad_char or " ", block_length - lang_width + ((config_table.pad_amount or 1) * 2)), set_hl(config_table.hl) },
{ string.rep(config_table.pad_char or " ", block_length - lang_width - vim.fn.strchars(rendered_info) + ((config_table.pad_amount or 1) * 2)), set_hl(config_table.hl) },
{ rendered_info, set_hl(config_table.info_hl or config_table.hl) },
},

sign_text = config_table.sign == true and icon or nil,
Expand All @@ -1097,7 +1120,7 @@ renderer.render_code_blocks = function (buffer, content, config_table)
virt_text = {
{ rendered_info, set_hl(config_table.info_hl or config_table.hl) },
{ string.rep(config_table.pad_char or " ", block_length - lang_width - vim.fn.strchars(rendered_info) + ((config_table.pad_amount or 1) * 2)), set_hl(config_table.hl) },
{ " " .. icon .. " ", set_hl(hl) },
{ icon_section, set_hl(hl) },
{ languageName .. " ", set_hl(config_table.name_hl) or set_hl(hl) },
},

Expand Down
2 changes: 1 addition & 1 deletion markview.nvim.wiki

0 comments on commit 6671dd4

Please sign in to comment.