Skip to content

Commit 4680f7f

Browse files
committed
feat(status): add virtual environment component
1 parent 90aa0fe commit 4680f7f

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

lua/astroui/status/component.lua

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,19 @@ function M.lsp(opts)
332332
))
333333
end
334334

335+
--- A function to build a set of children components for a git branch section
336+
---@param opts? table options for configuring git branch and the overall padding
337+
---@return table # The Heirline component table
338+
-- @usage local heirline_component = require("astroui.status").component.git_branch()
339+
function M.virtual_env(opts)
340+
opts = extend_tbl({
341+
virtual_env = { icon = { kind = "Environment", padding = { right = 1 } } },
342+
surround = { separator = "right", color = "virtual_env_bg", condition = condition.has_virtual_env },
343+
hl = hl.get_attributes "virtual_env",
344+
}, opts)
345+
return M.builder(status_utils.setup_providers(opts, { "virtual_env" }))
346+
end
347+
335348
--- A function to build a set of components for a foldcolumn section in a statuscolumn
336349
---@param opts? table options for configuring foldcolumn and the overall padding
337350
---@return table # The Heirline component table

lua/astroui/status/condition.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ function M.has_filetype(bufnr)
100100
return vim.bo[bufnr or 0].filetype and vim.bo[bufnr or 0].filetype ~= ""
101101
end
102102

103+
--- A condition function if a virtual environment is activated
104+
---@return boolean # whether or not virtual environment is activated
105+
-- @usage local heirline_component = { provider = "Example Provider", condition = require("astroui.status").condition.has_virtual_env }
106+
function M.has_virtual_env() return vim.env.VIRTUAL_ENV ~= nil end
107+
103108
--- A condition function if Aerial is available
104109
---@return boolean # whether or not aerial plugin is installed
105110
-- @usage local heirline_component = { provider = "Example Provider", condition = require("astroui.status").condition.aerial_available }

lua/astroui/status/provider.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,24 @@ function M.lsp_client_names(opts)
503503
end
504504
end
505505

506+
--- A provider function for showing the current virtual environment name
507+
---@param opts table options passed to the stylize function
508+
---@return function # the function for outputting the virtual environment
509+
-- @usage local heirline_component = { provider = require("astroui.status").provider.virtual_env() }
510+
-- @see astroui.status.utils.stylize
511+
function M.virtual_env(opts)
512+
opts = extend_tbl({ env_names = { "env", ".env", "venv", ".venv" } }, opts)
513+
return function()
514+
local venv = vim.env.VIRTUAL_ENV
515+
if venv then
516+
local path = vim.fn.split(venv, "/")
517+
local venv_name = path[#path]
518+
if #path > 1 and vim.tbl_contains(opts.env_names, venv_name) then venv_name = path[#path - 1] end
519+
return status_utils.stylize(opts.format and opts.format:format(venv_name) or venv_name, opts)
520+
end
521+
end
522+
end
523+
506524
--- A provider function for showing if treesitter is connected
507525
---@param opts? table options passed to the stylize function
508526
---@return function # function for outputting TS if treesitter is connected

0 commit comments

Comments
 (0)