Skip to content

Commit

Permalink
Added generic hl_comonent() (#21)
Browse files Browse the repository at this point in the history
* Added generic hl_comonent()

* Added dynamic flow direction
  • Loading branch information
sschleemilch authored Aug 29, 2024
1 parent 0c1e37b commit a365961
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 126 deletions.
43 changes: 7 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,47 +222,18 @@ It will render to something like this (depending on your colorscheme):

![c1](./doc/custom_components/1.png)

Let's add some color. To do that we can use the `highlights` component of slimline:

If you want to use internal render functionality of a component you can do it like that:
```lua
function ()
local sh = require("slimline.highlights")
local content = "Hello World"
return sh.hl_content(content, sh.hls.primary.text)
local h = require("slimline.highlights")
local c = require("slimline").config
return h.hl_component({primary = "Hello", secondary = "World"}, h.hls, config.sep)
end
```
It will now look like that:

![c2](./doc/custom_components/2.png)

To add configured separators we can add them to `hl_content`:

```lua
function ()
local sh = require("slimline.highlights")
local cfg = require("slimline").config
local content = " Hello World "
return sh.hl_content(content, sh.hls.primary.text, cfg.sep.left, cfg.sep.right)
end
```

Now we have a primary part of a new component:

![c3](./doc/custom_components/3.png)

To add a secondary part we need to change it like that:

```lua
function ()
local sh = require("slimline.highlights")
local cfg = require("slimline").config
local content = sh.hl_content(" Hello ", sh.hls.primary.text, cfg.sep.left)
content = content .. sh.hl_content(cfg.sep.right, sh.hls.primary.sep_transition)
content = content .. sh.hl_content(" World ", sh.hls.secondary.text, nil, cfg.sep.right)
return content
end
```

And we have our final result of a custom component which respects separators and colors from the config:
It will now render to that (depending on the config)

![c4](./doc/custom_components/4.png)

Of course you can use `Slimline*` highlight groups on your own to create your own styled component
Binary file removed doc/custom_components/2.png
Binary file not shown.
Binary file removed doc/custom_components/3.png
Binary file not shown.
9 changes: 6 additions & 3 deletions lua/slimline/components/diagnostics.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
local highlights = require('slimline.highlights')
local config = require('slimline').config
local M = {}

local last_diagnostic_component = ''

--- @param config table
--- @param sep {left: string, right: string}
--- @param direction string
--- |'"right"'
--- |'"left"'
--- @return string
function M.render(config, sep)
function M.render(sep, direction)
-- Lazy uses diagnostic icons, but those aren"t errors per se.
if vim.bo.filetype == 'lazy' then
return ''
Expand Down Expand Up @@ -44,7 +47,7 @@ function M.render(config, sep)
return ''
end
last_diagnostic_component =
highlights.hl_content(' ' .. last_diagnostic_component .. ' ', highlights.hls.primary.text, sep.left, sep.right)
highlights.hl_component({ primary = last_diagnostic_component }, highlights.hls, sep, direction)
return last_diagnostic_component
end

Expand Down
24 changes: 8 additions & 16 deletions lua/slimline/components/filetype_lsp.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
local M = {}
local highlights = require('slimline.highlights')

--- @param config table
--- @param sep {left: string, right: string}
function M.render(config, sep)
--- @param direction string
--- |'"right"'
--- |'"left"'
--- @return string
function M.render(sep, direction)
local filetype = vim.bo.filetype
if filetype == '' then
filetype = '[No Name]'
end
local icon = ''
local ok, MiniIcons = pcall(require, 'mini.icons')
if ok then
icon = ' ' .. MiniIcons.get('filetype', filetype)
icon = MiniIcons.get('filetype', filetype)
end
filetype = highlights.hl_content(icon .. ' ' .. filetype .. ' ', highlights.hls.primary.text, nil, sep.right)
filetype = icon .. ' ' .. filetype

local attached_clients = vim.lsp.get_clients { bufnr = 0 }
local it = vim.iter(attached_clients)
Expand All @@ -24,18 +27,7 @@ function M.render(config, sep)
local names = it:totable()
local lsp_clients = string.format('%s', table.concat(names, ','))

local filetype_hl_sep_left = highlights.hls.primary.sep
if #attached_clients > 0 then
filetype_hl_sep_left = highlights.hls.primary.sep_transition
end
filetype = highlights.hl_content(config.sep.left, filetype_hl_sep_left) .. filetype
lsp_clients = highlights.hl_content(' ' .. lsp_clients .. ' ', highlights.hls.secondary.text, sep.left)

local result = filetype
if #attached_clients > 0 then
result = lsp_clients .. result
end
return result
return highlights.hl_component({ primary = filetype, secondary = lsp_clients }, highlights.hls, sep, direction)
end

return M
38 changes: 17 additions & 21 deletions lua/slimline/components/git.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
local M = {}
local highlights = require('slimline.highlights')
local config = require('slimline').config

--- @param config table
--- @param sep {left: string, right: string}
--- @param direction string
--- |'"right"'
--- |'"left"'
--- @return string
function M.render(config, sep)
function M.render(sep, direction)
local status = vim.b.gitsigns_status_dict
if not status then
return ''
Expand All @@ -13,38 +16,31 @@ function M.render(config, sep)
return ''
end

local branch = string.format('%s %s', config.icons.git.branch, status.head)

local added = status.added and status.added > 0
local removed = status.removed and status.removed > 0
local changed = status.changed and status.changed > 0
local modifications = added or removed or changed

local branch = string.format(' %s %s ', config.icons.git.branch, status.head)
branch = highlights.hl_content(branch, highlights.hls.primary.text, sep.left)
local branch_hl_right_sep = highlights.hls.primary.sep
if modifications then
branch_hl_right_sep = highlights.hls.primary.sep_transition
end
-- if there are modifications the main part of the git components should have a right side
-- seperator
if modifications then
sep.right = config.sep.right
end
branch = branch .. highlights.hl_content(sep.right, branch_hl_right_sep)

local mods = ''
local mods = {}
if modifications then
if added then
mods = mods .. string.format(' +%s', status.added)
table.insert(mods, string.format('+%s', status.added))
end
if changed then
mods = mods .. string.format(' ~%s', status.changed)
table.insert(mods, string.format('~%s', status.changed))
end
if removed then
mods = mods .. string.format(' -%s', status.removed)
table.insert(mods, string.format('-%s', status.removed))
end
mods = highlights.hl_content(mods .. ' ', highlights.hls.secondary.text, nil, sep.right)
end
return branch .. mods
return highlights.hl_component(
{ primary = branch, secondary = table.concat(mods, ' ') },
highlights.hls,
sep,
direction
)
end

return M
11 changes: 7 additions & 4 deletions lua/slimline/components/mode.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
local M = {}
local highlights = require('slimline.highlights')
local utils = require('slimline.utils')
local config = require('slimline').config

--- @param config table
--- @param sep {left: string, right: string}
--- @param direction string
--- |'"right"'
--- |'"left"'
--- @return string
function M.render(config, sep)
function M.render(sep, direction)
local mode = utils.get_mode()
local render = mode
if config.verbose_mode == false then
render = string.sub(mode, 1, 1)
end
local content = ' ' .. render .. ' '
return highlights.hl_content(content, highlights.get_mode_hl(mode), sep.left, sep.right)
local hl = highlights.get_mode_hl(mode)
return highlights.hl_component({ primary = render }, hl, sep, direction)
end

return M
14 changes: 8 additions & 6 deletions lua/slimline/components/path.lua
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
local M = {}
local highlights = require('slimline.highlights')
local config = require('slimline').config

--- @param config table
--- @param sep {left: string, right: string}
--- @param direction string
--- |'"right"'
--- |'"left"'
--- @return string
function M.render(config, sep)
local file = highlights.hl_content(' ' .. vim.fn.expand('%:t') .. ' %m%r', highlights.hls.primary.text, sep.left)
file = file .. highlights.hl_content(config.sep.right, highlights.hls.primary.sep_transition)
function M.render(sep, direction)
local file = vim.fn.expand('%:t') .. '%m%r'

local path = vim.fs.normalize(vim.fn.expand('%:.:h'))
if #path == 0 then
return ''
end
path = highlights.hl_content(' ' .. config.icons.folder .. path .. ' ', highlights.hls.secondary.text, nil, sep.right)
path = config.icons.folder .. path

return file .. path
return highlights.hl_component({ primary = file, secondary = path }, highlights.hls, sep, direction)
end

return M
11 changes: 7 additions & 4 deletions lua/slimline/components/progress.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
local M = {}
local highlights = require('slimline.highlights')
local utils = require('slimline.utils')
local config = require('slimline').config

--- @param config table
--- @param sep {left: string, right: string}
--- @param direction string
--- |'"right"'
--- |'"left"'
--- @return string
function M.render(config, sep)
function M.render(sep, direction)
local cur = vim.fn.line('.')
local total = vim.fn.line('$')
local content
Expand All @@ -16,8 +19,8 @@ function M.render(config, sep)
else
content = string.format('%2d%%%%', math.floor(cur / total * 100))
end
content = string.format(' %s %s / %s ', config.icons.lines, content, total)
return highlights.hl_content(content, highlights.get_mode_hl(utils.get_mode()), sep.left, sep.right)
content = string.format('%s %s / %s', config.icons.lines, content, total)
return highlights.hl_component({ primary = content }, highlights.get_mode_hl(utils.get_mode()), sep, direction)
end

return M
Loading

0 comments on commit a365961

Please sign in to comment.