Skip to main content

🤙 Callback functions

☎️ on_venv_activate_callback

This option is used to make the plugin call your function when a venv is activated.


-- Create your own function and decide what you want to do when a venv is activated.
local function call_me_on_venv_activation()
local source = require("venv-selector").source() or "" -- source is nil if a venv is loaded from cache (when its not selected in viewer)
local venv = require("venv-selector").venv()
local python = require("venv-selector").python() -- full path to python found, not used in notify message below
if venv ~= nil and source ~= nil then
vim.notify("Venv activated: " .. venv .. " (search name = " .. source .. ")", vim.log.levels.INFO, { title = "VenvSelect" })
end
end

-- And in the plugin section, just reference the local function above.
return {
"linux-cultist/venv-selector.nvim",
dependencies = {
"neovim/nvim-lspconfig",
{ "nvim-telescope/telescope.nvim", branch = "0.1.x", dependencies = { "nvim-lua/plenary.nvim" } },
},
ft = "python", -- Load when opening Python files
opts = {
options = { -- your options here
on_venv_activate_callback = call_me_on_venv_activation -- set the callback to call your function
},
search = {}, -- your search options here
},
},
},

info

You can use this to execute code when a specific venv is selected.

Maybe update a status bar or run some other commands. Its up to you.

☎️ on_fd_result_callback

This is similar to on_venv_activate_callback. It is executed when a search result is found by fd.


-- This function gets called by the plugin when a new result is found by fd
-- You can change the filename displayed here to what you like.
-- Here in the example for linux/mac we replace the home directory with '~' and remove the /bin/python part.
local function shorter_name(filename)
return filename:gsub(os.getenv("HOME"), "~"):gsub("/bin/python", "")
end

-- And in the plugin section, just reference the local function above.
return {
"linux-cultist/venv-selector.nvim",
dependencies = {
"neovim/nvim-lspconfig",
{ "nvim-telescope/telescope.nvim", branch = "0.1.x", dependencies = { "nvim-lua/plenary.nvim" } },
},
ft = "python", -- Load when opening Python files
opts = {
options = { -- your options here
on_fd_result_callback = shorter_name -- calls your function for all search results
},
search = {
my_projects = {
command = "$FD '/bin/python$' ~/Code/MyProjects --full-path --color never -HI -a -L -E /proc -E .git/ -E .wine/ -E .steam/ -E Steam/ -E site-packages/"
-- If you put the callback in your own search and not in global options above, its only called for your "my_projects" search
on_fd_result_callback = shorter_name
},
},
},
},
},

info

You can use this to execute code when a search result is found.

Here we use it to shorten the name of the python path in the viewer.

The result returned from the function is just visual. The viewer still uses the full path to the found python when you activate a venv.