Skip to content

Commit

Permalink
doc: Added a new recipe
Browse files Browse the repository at this point in the history
  • Loading branch information
OXY2DEV committed Aug 7, 2024
1 parent 2898c3c commit 39dfc69
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 6 deletions.
169 changes: 168 additions & 1 deletion RECIPES.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ These are some simple examples.

You can customise the headings like `statusline` component via the `label` style.

![better_headings](https://github.com/OXY2DEV/markview.nvim/blob/images/Recipes/better_headings.jpg)

This allows adding corners, paddings & icons to them.

Let's start with something simple. Run the following code.
Expand Down Expand Up @@ -123,15 +125,180 @@ vim.cmd("Markview enableAll");

If you want to apply this to the all the headings then `markview` comes a preset for that.

Just add this to your config table.

>[!CAUTION]
> The colors are meant to only be added once so running it now will not add the highlight groups.
```lua
local heading_presets = require("markview.presets").headings;
local hl_presets = require("markview.presets").highlight_groups;

require("markview").setup({
highlight_groups = hl_presets.h_decorated,
headings = heading_presets.decorated
headings = heading_presets.decorated_labels
});
```

## 📏 Centered horizontal rules

By default, the icon in the middle of the `horizontal rules` aren't centered.

This is due to not being able to check the correct `textoff` value.

But if you manually set it up you can center it.

Let's start by creating a repeating part.

```lua
require("markview").setup({
horizontal_rules = {
parts = {
{
type = "repeating",
text = "",

repeat_amount = function ()
local w = vim.api.nvim_win_get_width(0);
local l = vim.api.nvim_buf_line_count(0);

l = vim.fn.strchars(tostring(l)) + 4;

return math.floor((w - (l + 3)) / 2);
end
}
}
}
});
```

Here's a HR to see what it does.

---

Now, let's explain what this does.

`w` is the current windows width and `l` is the amount of lines in the current buffer.

Now, we can turn `l` into a string and count how many character it has(using `vim.fn.strchars()`). This is the number of columns the line number will take in the statuscolumn.

In my case, I have a signcolumn(this takes 2 columns) a foldcolumn(this takes 1 column) and a separator which also takes a character. So we add 2 + 1 + 1 = 4 to the value of `l`. This is how wide my statuscolumn is.

Now, since I want ``(3 characters) I also add 3 to `l` in the final result.

This is going to be the left side of the horizontal rule.

Now, we copy it but this time replace `math.floor` with `math.ceil` to handle when we have an odd number of free columns.

Finally we make the center part.

```lua
{
type = "text",
text = ""
}
```

By combining them we get this,

```lua
require("markview").setup({
horizontal_rules = {
parts = {
{
type = "repeating",
text = "",

repeat_amount = function ()
local w = vim.api.nvim_win_get_width(0);
local l = vim.api.nvim_buf_line_count(0);

l = vim.fn.strchars(tostring(l)) + 4;

return math.floor((w - (l + 3)) / 2);
end
},
{
type = "text",
text = ""
},
{
type = "repeating",
text = "",

repeat_amount = function ()
local w = vim.api.nvim_win_get_width(0);
local l = vim.api.nvim_buf_line_count(0);

l = vim.fn.strchars(tostring(l)) + 4;

return math.ceil((w - (l + 3)) / 2);
end
},
}
}
});
```

Now, use the default `highlight groups` and you are done.

>[!Tip]
> `direction` can be used to apply highlights to a specific side of the text.
```lua
require("markview").setup({
horizontal_rules = {
parts = {
{
type = "repeating",
text = "",

direction = "left",
hl = {
"Gradient1", "Gradient2",
"Gradient3", "Gradient4",
"Gradient5", "Gradient6",
"Gradient7", "Gradient8",
"Gradient9", "Gradient10"
},

repeat_amount = function ()
local w = vim.api.nvim_win_get_width(0);
local l = vim.api.nvim_buf_line_count(0);

l = vim.fn.strchars(tostring(l)) + 4;

return math.floor((w - (l + 3)) / 2);
end
},
{
type = "text",
text = ""
},
{
type = "repeating",
text = "",

direction = "right",
hl = {
"Gradient1", "Gradient2",
"Gradient3", "Gradient4",
"Gradient5", "Gradient6",
"Gradient7", "Gradient8",
"Gradient9", "Gradient10"
},

repeat_amount = function ()
local w = vim.api.nvim_win_get_width(0);
local l = vim.api.nvim_buf_line_count(0);

l = vim.fn.strchars(tostring(l)) + 4;

return math.ceil((w - (l + 3)) / 2);
end
},
}
}
});
```

18 changes: 13 additions & 5 deletions lua/markview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ markview.deep_merge = function (behavior, tbl_1, tbl_2)
end

if vim.islist(value) then
for index, item in ipairs(value) do
if not markview.list_contains(tbl_1[key], item) then
table.insert(tbl_1[key], item);
else
tbl_1[key][index] = markview.deep_merge(behavior, tbl_1[key][index], item);
if not tbl_1.overwrite or tbl_1.overwrite and not vim.list_contains(tbl_1.overwrite, key) then
for index, item in ipairs(value) do
if not markview.list_contains(tbl_1[key], item) then
table.insert(tbl_1[key], item);
else
tbl_1[key][index] = markview.deep_merge(behavior, tbl_1[key][index], item);
end
end
else
tbl_1[key] = value;
end
elseif type(value) == "table" then
tbl_1[key] = markview.deep_merge(behavior, tbl_1[key], value);
Expand Down Expand Up @@ -1117,6 +1121,7 @@ markview.configuration = {

block_quotes = {
enable = true,
overwrite = { "callouts" },

default = {
border = "", border_hl = "MarkviewBlockQuoteDefault"
Expand Down Expand Up @@ -1265,6 +1270,7 @@ markview.configuration = {
},
horizontal_rules = {
enable = true,
overwrite = { "parts" },

parts = {
{
Expand Down Expand Up @@ -1594,6 +1600,8 @@ markview.setup = function (user_config)
if vim.islist(markview.configuration.highlight_groups) then
markview.add_hls(markview.configuration.highlight_groups);
end

markview.commands.enableAll();
end

return markview;

0 comments on commit 39dfc69

Please sign in to comment.