Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ use {
-- Jump to request line on run
jump_to_request = false,
env_file = '.env',
-- for telescope select
env_pattern = "\\.env$",
env_edit_command = "tabedit",
custom_dynamic_variables = {},
yank_dry_run = true,
search_back = true,
Expand Down Expand Up @@ -189,6 +192,55 @@ request method (e.g. `GET`) and run `rest.nvim`.
Run `export DEBUG_PLENARY="debug"` before starting nvim. Logs will appear most
likely in ~/.cache/nvim/rest.nvim.log

## Telescope Extension

```lua

-- first load extension
require("telescope").load_extension("rest")
-- then use it
require("telescope").extensions.rest.select_env()

```

### Mappings

- Enter: Select Env file
- Ctrl+O: Edit Env file

### Config

- env_pattern: For env file pattern
- env_edit_command: For env file edit command

## Lualine

We also have lualine component to get what env file you select!
And dont't worry, it will only show up under http files.

```lua
-- Juse add a component in your lualine config
{
sections = {
lualine_x = {
"rest"
}
}
}

-- To custom icon and color
{
sections = {
lualine_x = {
{
"rest",
icon = "",
fg = "#428890"
}
}
}
}
```

## Contribute

Expand Down
31 changes: 31 additions & 0 deletions lua/lualine/components/rest.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
local lualine_require = require("lualine_require")
local M = lualine_require.require("lualine.component"):extend()
local config = require("rest-nvim.config")

local default_options = {
fg = "#428890",
icon = "",
}

function M:init(options)
M.super.init(self, options)
self.options = vim.tbl_deep_extend("keep", self.options or {}, default_options)
self.icon = self.options.icon

self.highlight_color = self:create_hl({ fg = self.options.fg }, "Rest")
end

function M:apply_icon()
local default_highlight = self:get_default_hl()
self.status = self:format_hl(self.highlight_color) .. self.icon .. " " .. default_highlight .. self.status
end

function M.update_status()
local current_filetype = vim.bo.filetype
if current_filetype == "http" then
return config.get("env_file")
end
return ""
end

return M
2 changes: 2 additions & 0 deletions lua/rest-nvim/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ local config = {
},
jump_to_request = false,
env_file = ".env",
env_pattern = "\\.env$",
env_edit_command = "tabedit",
custom_dynamic_variables = {},
yank_dry_run = true,
search_back = true,
Expand Down
65 changes: 65 additions & 0 deletions lua/telescope/_extensions/rest.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
local has_telescope, telescope = pcall(require, "telescope")

if not has_telescope then
return
end

local rest = require("rest-nvim")

local state = require("telescope.actions.state")

local action_state = require("telescope.actions.state")
local actions = require("telescope.actions")
local finders = require("telescope.finders")
local pickers = require("telescope.pickers")
local conf = require("telescope.config").values

local config = require("rest-nvim.config")

local function rest_env_select(opt)
local pattern = config.get("env_pattern")
local edit = config.get("env_edit_command")

local command = string.format("fd -H '%s'", pattern)
local result = io.popen(command):read("*a")

local lines = {}
for line in result:gmatch("[^\r\n]+") do
table.insert(lines, line)
end

pickers
.new({}, {
prompt_title = "Select Env File",
finder = finders.new_table({
results = lines,
}),
attach_mappings = function(prompt_bufnr, map)
actions.select_default:replace(function()
local selection = action_state.get_selected_entry()
actions.close(prompt_bufnr)
if selection == nil then
return
end
rest.select_env(selection[1])
end)
map("i", "<c-o>", function()
actions.close(prompt_bufnr)
local selection = state.get_selected_entry(prompt_bufnr)
if selection == nil then
return
end
vim.api.nvim_command(edit .. " " .. selection[1])
end)
return true
end,
previewer = conf.grep_previewer({}),
})
:find()
end

return telescope.register_extension({
exports = {
select_env = rest_env_select,
},
})