Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ianjkaplan committed Jun 5, 2024
0 parents commit d1da293
Show file tree
Hide file tree
Showing 13 changed files with 358 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -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
26 changes: 26 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
std = luajit
cache = true
codes = true

read_globals = { "vim" }
5 changes: 5 additions & 0 deletions .luarc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"diagnostics.globals": [
"P"
]
}
8 changes: 8 additions & 0 deletions .neoconf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"neodev": {
"library": {
"enabled": true,
"plugins": ["plenary.nvim", "nui.nvim"]
}
}
}
5 changes: 5 additions & 0 deletions .stylua.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
column_width = 80
line_endings = "Unix"
indent_type = "Spaces"
indent_width = 4
quote_style = "AutoPreferDouble"
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 9 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fmt:
echo "===> Formatting"
stylua lua/ --config-path=.stylua.toml

lint:
echo "===> Linting"
luacheck lua/ --globals vim

pr-ready: fmt lint
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div align="center">

# 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)

</div>

## ⇁ 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!
130 changes: 130 additions & 0 deletions lua/hermes/app.lua
Original file line number Diff line number Diff line change
@@ -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", "<CR>", 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
41 changes: 41 additions & 0 deletions lua/hermes/init.lua
Original file line number Diff line number Diff line change
@@ -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
68 changes: 68 additions & 0 deletions lua/hermes/request.lua
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d1da293

Please sign in to comment.