Skip to content

Commit

Permalink
add news sction to readme, update names according to snake_case
Browse files Browse the repository at this point in the history
  • Loading branch information
linarcx committed Jan 13, 2022
1 parent 0e6c1be commit 1859cf7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,21 @@ If you have any idea to improve this project, please create a pull-request for i

2. There should be a one-to-one relation between features and pull requests. Please create separate pull-requests for each feature.
3. Please use [snake_case](https://en.wikipedia.org/wiki/Snake_case) for function names ans local variables
4. If your PR have more than one commit, please squash them into one.
5. Use meaningful name for variables and functions. Don't use abbreviations as far as you can.


# News

* 2022/01/13
* append environment name to buffer( default behavior `<cr>`)
* append environment value to buffer( mapping `<c-a>`)
* edit environment value for the current session( mapping `<c-e>`)
* fixes issue with fish terminal in multiline values

# Roadmap :blue_car:
- [x] copy selected value when select an item in picker.
- [ ] ability to add temporary variables.
- [ ] use previews, because some variables like `PATH` don't fit on screen.
- [ ] `PATH` is a special variable! It's cool to allow people to add, remove and edit items of it.
- :heavy_check_mark: append environment name/value to buffer. (Thanks to: [sbulav](https://github.com/sbulav))
- :heavy_check_mark: edit environment value for the current session. (Thanks to: [sbulav](https://github.com/sbulav))
- [ ] ability to add new environment variables.
- [ ] use telescope previews, because some variables like `PATH` don't fit on screen.
- [ ] `PATH` is a special variable! It's cool to allow people to add, remove and edit items to/from/of it.
62 changes: 32 additions & 30 deletions lua/telescope/_extensions/env.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,40 @@ local pickers = require("telescope.pickers")
local finders = require("telescope.finders")
local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")
local make_entry = require("telescope.make_entry")
local conf = require("telescope.config").values
local entry_display = require("telescope.pickers.entry_display")
local conf = require("telescope.config").values

local function prepareEnvironmentVariables()
Items = {}
local function prepare_environment_variables()
local items = {}
for key, value in pairs(vim.fn.environ()) do
table.insert(Items, { key, value })
table.insert(items, { key, value })
end
return Items
return items
end

local function appendEnvironmentValue(prompt_bufnr)
local function append_environment_name(prompt_bufnr)
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
if selection.value == "" then
return
end
if vim.api.nvim_buf_get_option(vim.api.nvim_get_current_buf(), "modifiable") then
vim.api.nvim_put({ selection.value[2] }, "b", true, true)
vim.api.nvim_put({ selection.value[1] }, "b", true, true)
end
end

local function appendEnvironmentName(prompt_bufnr)
local function append_environment_value(prompt_bufnr)
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
if selection.value == "" then
return
end
if vim.api.nvim_buf_get_option(vim.api.nvim_get_current_buf(), "modifiable") then
vim.api.nvim_put({ selection.value[1] }, "b", true, true)
vim.api.nvim_put({ selection.value[2] }, "b", true, true)
end
end

local function editEnvironmentValue(prompt_bufnr)
local function edit_environment_value(prompt_bufnr)
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
local value = vim.fn.input("[ENV] Enter new value: ", selection.value[2])
Expand All @@ -48,40 +47,43 @@ local function editEnvironmentValue(prompt_bufnr)
vim.fn.setenv(selection.value[1], value)
end

local function showEnvironmentVariables(opts)
local function show_environment_variables(opts)
opts = opts or {}
pickers.new(opts, {
prompt_title = "Environment Variables",
finder = finders.new_table({
results = prepareEnvironmentVariables(),
results = prepare_environment_variables(),
entry_maker = function(entry)
local cols = vim.o.columns
local width = conf.width or conf.layout_config.width or conf.layout_config[conf.layout_strategy].width or cols
local tel_win_width
local columns = vim.o.columns
local width = conf.width
or conf.layout_config.width
or conf.layout_config[conf.layout_strategy].width
or columns
local telescope_width
if width > 1 then
tel_win_width = width
telescope_width = width
else
tel_win_width = math.floor(cols * width)
telescope_width = math.floor(columns * width)
end
local desc_width = math.floor(cols * 0.05)
local command_width = 22
local env_name_width = math.floor(columns * 0.05)
local env_value_width = 22

-- NOTE: the width calculating logic is not exact, but approx enough
local displayer = entry_display.create({
separator = "",
items = {
{ width = command_width },
{ width = tel_win_width - desc_width - command_width },
{ width = env_value_width },
{ width = telescope_width - env_name_width - env_value_width },
{ remaining = true },
},
})

local function make_display(ent)
-- Concatinate multiline env values
local concat_value = entry[2]:gsub("\r?\n", " ")
local function make_display()
-- concatenating multiline env values
local concatenated_width = entry[2]:gsub("\r?\n", " ")
return displayer({
{ entry[1] },
{ concat_value },
{ concatenated_width },
})
end

Expand All @@ -94,16 +96,16 @@ local function showEnvironmentVariables(opts)
}),
sorter = conf.generic_sorter(opts),
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(appendEnvironmentName)
map("i", "<c-a>", appendEnvironmentValue)
map("i", "<c-e>", editEnvironmentValue)
actions.select_default:replace(append_environment_name)
map("i", "<c-a>", append_environment_value)
map("i", "<c-e>", edit_environment_value)
return true
end,
}):find()
end

local function run()
showEnvironmentVariables()
show_environment_variables()
end

return require("telescope").register_extension({
Expand Down

0 comments on commit 1859cf7

Please sign in to comment.