From d1da293f474a4e5f62027b93c135c894320dbbef Mon Sep 17 00:00:00 2001 From: Ian Kaplan Date: Wed, 31 Jan 2024 10:37:01 -0500 Subject: [PATCH] initial commit --- .editorconfig | 9 +++ .github/workflows/format.yml | 26 +++++++ .github/workflows/lint.yml | 18 +++++ .luacheckrc | 5 ++ .luarc.json | 5 ++ .neoconf.json | 8 +++ .stylua.toml | 5 ++ LICENSE | 20 ++++++ Makefile | 9 +++ README.md | 14 ++++ lua/hermes/app.lua | 130 +++++++++++++++++++++++++++++++++++ lua/hermes/init.lua | 41 +++++++++++ lua/hermes/request.lua | 68 ++++++++++++++++++ 13 files changed, 358 insertions(+) create mode 100644 .editorconfig create mode 100644 .github/workflows/format.yml create mode 100644 .github/workflows/lint.yml create mode 100644 .luacheckrc create mode 100644 .luarc.json create mode 100644 .neoconf.json create mode 100644 .stylua.toml create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 lua/hermes/app.lua create mode 100644 lua/hermes/init.lua create mode 100644 lua/hermes/request.lua diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..8529c8b --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +# top-most EditorConfig file +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +indent_style = space +indent_size = 4 +insert_final_newline = true diff --git a/.github/workflows/format.yml b/.github/workflows/format.yml new file mode 100644 index 0000000..a5f3ce3 --- /dev/null +++ b/.github/workflows/format.yml @@ -0,0 +1,26 @@ +name: Format + +on: [push, pull_request] + +jobs: + format: + name: Stylua + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - run: date +%W > weekly + + - name: Restore cache + id: cache + uses: actions/cache@v2 + with: + path: | + ~/.cargo/bin + key: ${{ runner.os }}-cargo-${{ hashFiles('weekly') }} + + - name: Install + if: steps.cache.outputs.cache-hit != 'true' + run: cargo install stylua + + - name: Format + run: stylua --check lua/ --config-path=.stylua.toml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..46759d5 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,18 @@ +name: Lint + +on: [push, pull_request] + +jobs: + lint: + name: Luacheck + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Setup + run: | + sudo apt-get update + sudo apt-get install luarocks + sudo luarocks install luacheck + + - name: Lint + run: luacheck lua/ --globals vim diff --git a/.luacheckrc b/.luacheckrc new file mode 100644 index 0000000..405c7b2 --- /dev/null +++ b/.luacheckrc @@ -0,0 +1,5 @@ +std = luajit +cache = true +codes = true + +read_globals = { "vim" } diff --git a/.luarc.json b/.luarc.json new file mode 100644 index 0000000..b8e3ecf --- /dev/null +++ b/.luarc.json @@ -0,0 +1,5 @@ +{ + "diagnostics.globals": [ + "P" + ] +} \ No newline at end of file diff --git a/.neoconf.json b/.neoconf.json new file mode 100644 index 0000000..ae41c73 --- /dev/null +++ b/.neoconf.json @@ -0,0 +1,8 @@ +{ + "neodev": { + "library": { + "enabled": true, + "plugins": ["plenary.nvim", "nui.nvim"] + } + } +} diff --git a/.stylua.toml b/.stylua.toml new file mode 100644 index 0000000..0b2e146 --- /dev/null +++ b/.stylua.toml @@ -0,0 +1,5 @@ +column_width = 80 +line_endings = "Unix" +indent_type = "Spaces" +indent_width = 4 +quote_style = "AutoPreferDouble" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5c4d426 --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +MIT License + + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5da43dc --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +fmt: + echo "===> Formatting" + stylua lua/ --config-path=.stylua.toml + +lint: + echo "===> Linting" + luacheck lua/ --globals vim + +pr-ready: fmt lint diff --git a/README.md b/README.md new file mode 100644 index 0000000..b615f96 --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +
+ +# Hermes HTTP Ui + +##### Minimal http client for neovim + +[![Lua](https://img.shields.io/badge/Lua-blue.svg?style=for-the-badge&logo=lua)](http://www.lua.org) +[![Neovim](https://img.shields.io/badge/Neovim%200.5+-green.svg?style=for-the-badge&logo=neovim)](https://neovim.io) + +
+ +## ⇁ WIP (Under construction) + +This is designed to help with my personal workflow. If you experience any issues, see some improvement you think would be amazing, or just have some feedback or advice, make an issue! diff --git a/lua/hermes/app.lua b/lua/hermes/app.lua new file mode 100644 index 0000000..3615ff4 --- /dev/null +++ b/lua/hermes/app.lua @@ -0,0 +1,130 @@ +local Popup = require("nui.popup") +local request = require("hermes.request") +local Layout = require("nui.layout") +local M = {} + +M._state = { + authorization = nil, + output_bufnr = nil, + layout = nil, +} + +--- @param bearer string|nil +M.set_bearer_token = function(bearer) + M._state.authorization = { bearer = bearer } +end + +M.init = function() + local input = Popup({ + border = "rounded", + enter = true, + }) + + input:map("n", "", function() + local text = vim.api.nvim_buf_get_lines( + vim.api.nvim_get_current_buf(), + 0, + -1, + true + )[1] + + local request_data = vim.split(text, " ") + M.send_request({ + method = request_data[1], + url = request_data[2], + }) + end, { noremap = true }) + + local output = Popup({ + border = "rounded", + }) + + M._state.output_bufnr = output.bufnr + + local layout = Layout( + { + position = "50%", + size = { + width = "50%", + height = "50%", + }, + }, + Layout.Box({ + Layout.Box(input, { size = "10%" }), + Layout.Box(output, { size = "90%" }), + }, { dir = "col" }) + ) + + layout:mount() + M._state.layout = { state = "show", layout = layout } + -- set line numbers for the response + vim.api.nvim_win_set_option(output.winid, "number", true) +end + +M.hide = function() + if M._state.layout == nil then + return + end + M._state.layout.layout:unmount() + M._state.layout = nil +end + +-- TODO: set auth token +--- TODO: support post put and patch +M.send_request = function(opts) + if opts.method == nil then + vim.notify("Request method is required", vim.log.levels.ERROR) + return + end + + if opts.url == nil then + vim.notify("Request url is required", vim.log.levels.ERROR) + return + end + + if opts.method == "GET" then + local response = request.get(opts.url, opts) + vim.api.nvim_buf_set_lines( + M._state.output_bufnr, + 0, + -1, + true, + vim.split(response or "", "\n") + ) + end + + if opts.method == "POST" then + local response = request.post(opts.url, opts) + vim.api.nvim_buf_set_lines( + M._state.output_bufnr, + 0, + -1, + true, + vim.split(response or "", "\n") + ) + end + + if opts.method == "PUT" then + local response = request.put(opts.url, opts) + vim.api.nvim_buf_set_lines( + M._state.output_bufnr, + 0, + -1, + true, + vim.split(response or "", "\n") + ) + end + + if opts.method == "PATCH" then + local response = request.put(opts.url, opts) + vim.api.nvim_buf_set_lines( + M._state.output_bufnr, + 0, + -1, + true, + vim.split(response or "", "\n") + ) + end +end + +return M diff --git a/lua/hermes/init.lua b/lua/hermes/init.lua new file mode 100644 index 0000000..eedbccf --- /dev/null +++ b/lua/hermes/init.lua @@ -0,0 +1,41 @@ +local app = require("hermes.app") +local M = {} + +function M.setup() + vim.api.nvim_create_user_command("HermesShowUI", function() + app.init() + end, { + desc = "Show Hermes UI", + nargs = 0, + }) + vim.api.nvim_create_user_command("HermesHideUi", function() + app.hide() + end, { + + desc = "Hide Hermes Ui ", + nargs = 0, + }) + vim.api.nvim_create_user_command("HermesSetBearerToken", function(opts) + if #opts.fargs ~= 1 then + vim.notify( + "SetBearerToken requires 1 argument", + vim.log.levels.ERROR + ) + return + end + + app.set_bearer_token(opts.fargs[1]) + end, { + desc = "Set authorization token", + nargs = 1, + }) + + vim.api.nvim_create_user_command("HermesClearBearerToken", function() + app.set_bearer_token(nil) + end, { + desc = "Clear authorization token", + nargs = 0, + }) +end + +return M diff --git a/lua/hermes/request.lua b/lua/hermes/request.lua new file mode 100644 index 0000000..bb0ca6a --- /dev/null +++ b/lua/hermes/request.lua @@ -0,0 +1,68 @@ +local M = {} +local curl = require("plenary.curl") + +--- @param url string +--- @param opts table +function M.get(url, opts) + local response = + curl.get(url, { accept = "application/json", query = opts.query or {} }) + if response.status >= 400 then + vim.notify( + "Request failed with status " .. response.status, + vim.log.levels.ERROR + ) + return + end + + return response.body +end + +--- @param url string +--- @param opts table +function M.post(url, opts) + local response = + curl.post(url, { accept = "application/json", body = opts.body or {} }) + if response.status >= 400 then + vim.notify( + "Request failed with status " .. response.status, + vim.log.levels.ERROR + ) + return + end + + return response.body +end + +--- @param url string +--- @param opts table +function M.put(url, opts) + local response = + curl.put(url, { accept = "application/json", body = opts.body or {} }) + if response.status >= 400 then + vim.notify( + "Request failed with status " .. response.status, + vim.log.levels.ERROR + ) + return + end + + return response.body +end + +--- @param url string +--- @param opts table +function M.patch(url, opts) + local response = + curl.patch(url, { accept = "application/json", body = opts.body or {} }) + if response.status >= 400 then + vim.notify( + "Request failed with status " .. response.status, + vim.log.levels.ERROR + ) + return + end + + return response.body +end + +return M