Skip to content

Commit

Permalink
todo: open hexer on visual selection
Browse files Browse the repository at this point in the history
  • Loading branch information
theKnightsOfRohan committed Jan 31, 2025
1 parent d8b26b0 commit 035e984
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ For example, `:Hexer "0x45"` would produce the following table:
└─────┴─────┴────┴─────────┴─────┘
```

You can also run `:Hexer` on a highlighted selection in visual mode, allowing for an easier time looking at unfamiliar code. The argument is restricted from being multiple lines, and it is unadvisable to try and parse a string which is too long.

## Installation
*We do be kinda lazy.*

Expand All @@ -59,20 +61,20 @@ return {

Hexer also exposes two lua functions:
```lua
-- open() works the same way as the `:Hexer` command, with both nil representing no argument.
-- open() parses the passed string arg and opens the hexr buffer, with nil or an empty string representing retaining the past table.
require("hexer"):open(arg)
-- close() is how the hexer buffer closes normally. Useful if you want to remap your own close keys.
-- Note that a BufLeave autocmd is with this already set
-- Note that a BufLeave autocmd is already set for if you leave hexer or open another buffer with it open.
require("hexer"):close()
```

This plugin doesn't take advantage of any special lazy.nvim stuff, just use your other plugin manager's default method.

## TODO
- [ ] Handle negative numbers more gracefully
- [ ] Open hexer on highlighted selection
- [x] Open hexer on highlighted selection
- [ ] Create abstraction for representation format
- [ ] Create config for representation format
- [ ] Create config for hexer popup options
- [ ] Expand into some binary operations?

- [ ] Gracefully handle abnormal ascii characters
22 changes: 20 additions & 2 deletions lua/hexer/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,26 @@ function M.setup()
M._display:_setup_window()

vim.api.nvim_create_user_command("Hexer", function(opts)
M._display:hexer_open(opts.args)
end, { nargs = "?" })
local str = opts.args or ""

-- Means that a range was supplied to the command, AKA called in visual mode
if opts.count ~= -1 then
local s_start = vim.fn.getpos("'<")
local s_end = vim.fn.getpos("'>")
local text = vim.api.nvim_buf_get_text(0, s_start[2] - 1, s_start[3] - 1, s_end[2] - 1, s_end[3], {})[1]

if text ~= "" then
if s_start[2] ~= s_end[2] then
vim.notify("Hexer can only operate on single-line strings!", vim.log.levels.ERROR)
return
else
str = text
end
end
end

M._display:hexer_open(str)
end, { nargs = "?", range = true })
end

return M

0 comments on commit 035e984

Please sign in to comment.