From e48a8f9c0ca6dde5f532ad7a319d7abb5dd72ebd Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Mon, 17 Jun 2024 17:56:51 -0500 Subject: [PATCH] feat: initial commit --- doc/http-codes.txt | 42 +++++++++++ lua/http-codes.lua | 36 +++++++++ .../_extensions/http => http-codes}/codes.lua | 0 lua/http-codes/fzf.lua | 15 ++++ lua/http-codes/os.lua | 17 +++++ lua/http-codes/telescope.lua | 3 + lua/telescope/_extensions/getOS.lua | 11 --- lua/telescope/_extensions/http.lua | 25 +------ lua/telescope/_extensions/http/list.lua | 8 +- readme.md | 74 +++++++++---------- 10 files changed, 153 insertions(+), 78 deletions(-) create mode 100644 doc/http-codes.txt create mode 100644 lua/http-codes.lua rename lua/{telescope/_extensions/http => http-codes}/codes.lua (100%) create mode 100644 lua/http-codes/fzf.lua create mode 100644 lua/http-codes/os.lua create mode 100644 lua/http-codes/telescope.lua delete mode 100644 lua/telescope/_extensions/getOS.lua diff --git a/doc/http-codes.txt b/doc/http-codes.txt new file mode 100644 index 0000000..af99008 --- /dev/null +++ b/doc/http-codes.txt @@ -0,0 +1,42 @@ +http-codes *http-codes.txt* + +Author: Barrett Ruth +Homepage: + +=============================================================================== +INTRODUCTION *http-codes.nvim* + +https-codes.nvim lets you quickly investigate HTTP status codes with mozilla, +supporting fzf-lua and telescope.nvim. + +Author: Barrett Ruth + +=============================================================================== +SETUP *http-codes.setup()* +>lua + require('http-codes').setup(config) +< +Parameters: ~ + + {config} `(table)`: table containing configuration for http-codes. + Defaults shown below. + +Usage: ~ +>lua + require('http-codes').setup({ + -- *REQUIRED*: 'fzf' or 'telescope' + use = 'fzf', + -- How the mozilla url is opened. + -- Configured by default based on OS: + open_url = 'xdg-open %s' -- UNIX + -- = 'open %s' -- OSX + -- = 'start %s' -- Windows + }) +< +=============================================================================== +COMMANDS *http-codes.commands* + +:HTTPCodes Browse HTTP codes using the configured picker. + +------------------------------------------------------------------------------- +vim:tw=80:ft=help: diff --git a/lua/http-codes.lua b/lua/http-codes.lua new file mode 100644 index 0000000..de7c6c4 --- /dev/null +++ b/lua/http-codes.lua @@ -0,0 +1,36 @@ +local M = {} + +local open_url = require('http-codes.os').get_open_url() + +M.config = { + use = nil, + open_url = open_url, +} + +M.http_codes = function() + if M.config.use == 'telescope' then + require('telescope').extensions.http.list(M.config.open_url) + elseif M.config.use == 'fzf' then + require('http-codes.fzf').pick(M.config.open_url) + end +end + +M.setup = function(user_config) + if + user_config.use == nil + or not vim.tbl_contains({ 'fzf', 'telescope' }, user_config.use) + then + vim.notify_once 'http-codes.nvim: must specify `use = {fzf,telescope}` in setup.' + return + end + + M.config = vim.tbl_deep_extend('force', M.config, user_config) + + if M.config.use == 'telescope' then + require('http-codes.telescope').setup() + end + + vim.api.nvim_create_user_command('HTTPCodes', M.http_codes, {}) +end + +return M diff --git a/lua/telescope/_extensions/http/codes.lua b/lua/http-codes/codes.lua similarity index 100% rename from lua/telescope/_extensions/http/codes.lua rename to lua/http-codes/codes.lua diff --git a/lua/http-codes/fzf.lua b/lua/http-codes/fzf.lua new file mode 100644 index 0000000..344a0df --- /dev/null +++ b/lua/http-codes/fzf.lua @@ -0,0 +1,15 @@ +local codes = require 'http-codes.codes' + +return { + pick = function(open_url) + require('fzf-lua').fzf_exec(vim.tbl_keys(codes), { + actions = { + default = function(selected) + local link = codes[selected[1]] + + vim.fn.jobstart(open_url:format(link)) + end, + }, + }) + end, +} diff --git a/lua/http-codes/os.lua b/lua/http-codes/os.lua new file mode 100644 index 0000000..f0135ca --- /dev/null +++ b/lua/http-codes/os.lua @@ -0,0 +1,17 @@ +local OS_TO_OPEN_URL = { + OSX = 'open %s', + Windows = 'start %s', +} + +local function get_os() + if jit then return jit.os end + + local fh, _ = assert(io.popen('uname -o 2>/dev/null', 'r')) + if fh then return fh:read() end + + return fh and fh:read() or 'Windows' +end + +return { + get_open_url = function() return OS_TO_OPEN_URL[get_os()] or 'xdg-open %s' end, +} diff --git a/lua/http-codes/telescope.lua b/lua/http-codes/telescope.lua new file mode 100644 index 0000000..95555dc --- /dev/null +++ b/lua/http-codes/telescope.lua @@ -0,0 +1,3 @@ +return { + setup = function() require('telescope').load_extension 'http' end, +} diff --git a/lua/telescope/_extensions/getOS.lua b/lua/telescope/_extensions/getOS.lua deleted file mode 100644 index e05d020..0000000 --- a/lua/telescope/_extensions/getOS.lua +++ /dev/null @@ -1,11 +0,0 @@ -return function() - -- ask LuaJIT first - -- will return "Windows", "Linux", "OSX", "BSD", "POSIX" or "Other" - if jit then return jit.os end - - -- Unix, Linux variants - local fh, _ = assert(io.popen('uname -o 2>/dev/null', 'r')) - if fh then return fh:read() end - - return 'Windows' -end diff --git a/lua/telescope/_extensions/http.lua b/lua/telescope/_extensions/http.lua index 0e612f1..580be82 100644 --- a/lua/telescope/_extensions/http.lua +++ b/lua/telescope/_extensions/http.lua @@ -1,26 +1,5 @@ -local ok, telescope = pcall(require, 'telescope') - -if not ok then - error 'Install nvim-telescope/telescope.nvim to use barrett-ruth/telescope-http.nvim.' -end - local list = require 'telescope._extensions.http.list' -local osname = require 'telescope._extensions.getOS' -local open_url_os = { - ['OSX'] = { open_url = 'open %s' }, - ['Windows'] = { open_url = 'start %s' }, -} - -local default_opts = open_url_os[osname()] -if not default_opts then default_opts = { open_url = 'xdg-open %s' } end -local opts = {} - -return telescope.register_extension { - setup = function(http_opts, _) - opts = vim.tbl_extend('force', default_opts, http_opts) - end, - exports = { - list = function(_) list(_, opts) end, - }, +return require('telescope').register_extension { + exports = { list = list }, } diff --git a/lua/telescope/_extensions/http/list.lua b/lua/telescope/_extensions/http/list.lua index 27accff..e06e12e 100644 --- a/lua/telescope/_extensions/http/list.lua +++ b/lua/telescope/_extensions/http/list.lua @@ -4,11 +4,11 @@ local pickers = require 'telescope.pickers' local sorters = require 'telescope.sorters' local state = require 'telescope.actions.state' -local codes = require 'telescope._extensions.http.codes' +local codes = require 'http-codes.codes' -return function(_, http_opts) +return function(open_url) pickers - .new(_, { + .new(nil, { prompt_title = 'HTTP Codes', finder = finders.new_table { results = vim.tbl_keys(codes), @@ -21,7 +21,7 @@ return function(_, http_opts) local selection = state.get_selected_entry() local link = codes[selection[1]] - vim.fn.jobstart(http_opts.open_url:format(link)) + vim.fn.jobstart(open_url:format(link)) end) return true end, diff --git a/readme.md b/readme.md index 257bed2..b8c7442 100644 --- a/readme.md +++ b/readme.md @@ -1,55 +1,49 @@ -# telescope-http.nvim +# http-codes.nvim -Quickly investigate HTTP status codes with the help of -[telescope.nvim](https://github.com/nvim-telescope/telescope.nvim) -and [mozilla](https://developer.mozilla.org/en-US/docs/Web/HTTP). +Quickly investigate HTTP status codes with the help of [mozilla](https://developer.mozilla.org/en-US/docs/Web/HTTP), including [telescope](https://github.com/barrett-ruth/telescope-http.nvim/) and [fzf-lua](https://github.com/ibhagwan/fzf-lua) integrations. ## Installation -Install via your favorite package manager, like [paq](https://github.com/savq/paq-nvim): +Install via your favorite package manager, like [lazy](https://github.com/folke/lazy.nvim): ```lua -require 'paq' { - 'savq/paq-nvim', - 'barrett-ruth/telescope-http.nvim' -} -``` - -Then load the extension: - -```lua -require('telescope').load_extension 'http' -``` - -## Dependencies - -- [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim) - -## Configuration - -```lua -require('telescope').setup { - extensions = { - http = { - -- How the mozilla url is opened. By default will be configured based on OS: +require('lazy').setup({ + { + 'barrett-ruth/http-codes.nvim' + opts = { + -- *REQUIRED*: 'fzf' or 'telescope' + use = 'fzf', + -- How the mozilla url is opened. + -- Configured by default based on OS: open_url = 'xdg-open %s' -- UNIX - -- open_url = 'open %s' -- OSX - -- open_url = 'start %s' -- Windows - } + -- = 'open %s' -- OSX + -- = 'start %s' -- Windows + }, + dependencies = { + -- choose your picker: + 'ibhagwan/fzf-lua' + -- 'nvim-telescope/telescope.nvim', + }, } -} +}) ``` +## [Configuration](./doc/http-codes.txt) + ## Usage -Creating a mapping for the following command in vim: - -```vim -:Telescope http list -``` - -or lua: +Use the exposed command in vimscript: ```lua -require('telescope').extensions.http.list() +:HTTPCodes ``` + +or in lua: + +```lua +require('http-codes').http_codes() +``` + +## Migration + +If migrating from [telescope-http.nvim](https://github.com/barrett-ruth/telescope-http.nvim), follow the above instructions—no telescope-specific config is necessary.