Skip to content

Commit

Permalink
now accepts user input
Browse files Browse the repository at this point in the history
  • Loading branch information
VoxelPrismatic committed Jun 20, 2024
1 parent d088ff3 commit 8f01c2d
Show file tree
Hide file tree
Showing 10 changed files with 345 additions and 48 deletions.
14 changes: 10 additions & 4 deletions lua/rabbit/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,16 @@ local box = {
}


local function grab_color(name)
---@param name string Highlight group name
---@param key? string Key, eg `fg` or `bg`
---@return string | nil
local function grab_color(name, key)
local details = vim.api.nvim_get_hl(0, { name = name })
if details == nil or details.fg == nil then
key = key or "fg"
if details == nil or details[key] == nil then
return nil
end
return string.format("#%06x", details.fg)
return string.format("#%06x", details[key])
end

---@type Rabbit.Options
Expand All @@ -58,7 +62,7 @@ local options = {
file = { fg = grab_color("Normal") },
term = { fg = grab_color("Constant"), italic = true },
noname = { fg = grab_color("Function"), italic = true },
message = { fg = grab_color("Identifier"), italic = true },
message = { fg = grab_color("Identifier"), italic = true, bold = true },
},
window = {
box = box.round,
Expand All @@ -81,6 +85,8 @@ local options = {
open = { "<leader>r" },
file_add = { "a" },
file_del = { "<Del>" },
group = { "A" },
group_up = { "-" },
},
plugin_opts = {},
enable = {
Expand Down
19 changes: 18 additions & 1 deletion lua/rabbit/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ local rabbit = {
plugins = {},

compat = compat,

input = require("rabbit.input"),
}

-- Display a message in the buffer
Expand Down Expand Up @@ -128,6 +130,10 @@ end

-- Close the Rabbit window
function rabbit.func.close(_)
if screen.ctx.in_input then
return
end

if rabbit.rabbit.win ~= nil then
if rabbit.rabbit.win == rabbit.user.win then
vim.api.nvim_win_set_buf(rabbit.user.win, rabbit.user.buf)
Expand All @@ -151,6 +157,10 @@ end

-- The window should not scroll when exiting Rabbit
local function close_with_cursor(_)
if screen.ctx.in_input then
return
end

(rabbit.ctx.plugin.func.close or rabbit.func.close)()

local term_codes = ("<Up><Left>"):rep(rabbit.user.view.topline) ..
Expand Down Expand Up @@ -334,6 +344,8 @@ function rabbit.MakeBuf(mode)
fullscreen = rabbit.user.win == rabbit.rabbit.win,
title = b_title or "",
mode = b_mode,
pos_col = opts.col,
pos_row = opts.row,
}

local fs = screen.set_border(rabbit.rabbit.win, buf, b_kwargs)
Expand Down Expand Up @@ -422,8 +434,13 @@ function rabbit.Redraw()
{ text = rabbit.ctx.listing[i] .. "", color = "RabbitFile" },
}, i + 1)
elseif string.find(target, "rabbitmsg://") == 1 then
target = target:sub(#"rabbitmsg://" + 1) .. "\n"
local msg = vim.split(target, "\n")[1]
local extra = vim.split(target, "\n")[2]
local space = (" "):rep(buf.w - vim.fn.strwidth(msg .. extra .. "| . |") - math.max(2, #(tostring(i))))
screen.add_entry({
{ text = target:sub(#"rabbitmsg://" + 1), color = "RabbitMsg" },
{ text = msg, color = "RabbitMsg" },
{ text = space .. extra, color = "RabbitDir" },
}, i + 1)
elseif target:sub(1, 1) ~= "/" then
local rel = rabbit.RelPath(buf_path, target)
Expand Down
84 changes: 84 additions & 0 deletions lua/rabbit/input.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
local screen = require("rabbit.screen")


---@class Rabbit.Input
local M = {}


-- Prompts the user with a question for short-text input
---@param title string The question/input prompt
---@param callback fun(response: string) The callback to be called with the user's input
---@param check? fun(response: string): boolean The callback to check the user's input
function M.prompt(title, callback, check)
local rabbit = require("rabbit")

check = check or function() return true end

screen.ctx.in_input = true
local buf = vim.api.nvim_create_buf(false, true)

local win = vim.api.nvim_open_win(buf, true, {
relative = "win",
width = screen.ctx.width - 4,
height = 1,
row = (screen.ctx.height - 3) / 2,
col = 1,

style = "minimal",
border = {
screen.ctx.box.top_left,
screen.ctx.box.horizontal,
screen.ctx.box.top_right,
screen.ctx.box.vertical,
screen.ctx.box.bottom_right,
screen.ctx.box.horizontal,
screen.ctx.box.bottom_left,
screen.ctx.box.vertical
},
title = {{
screen.ctx.box.horizontal .. " " .. title .. " ",
"FloatBorder"
}}
})

vim.fn.feedkeys("i", "n")

vim.api.nvim_create_autocmd("InsertLeave", {
buffer = buf,
callback = function()
screen.ctx.in_input = false
vim.api.nvim_win_close(win, true)
vim.api.nvim_buf_delete(buf, { force = true })
vim.fn.feed_termcodes("<Esc>", "n")
end
})

vim.api.nvim_create_autocmd("TextChangedI", {
buffer = buf,
callback = function()
vim.api.nvim_buf_clear_namespace(buf, 42, 0, -1)
if not check(vim.fn.getline(".")) then
vim.api.nvim_buf_add_highlight(buf, 42, "Error", 0, 0, -1)
end
end
})

local cb = function()
local line = vim.fn.getline(".")
if not check(line) then
return
end
screen.ctx.in_input = false
vim.api.nvim_win_close(win, true)
vim.api.nvim_buf_delete(buf, { force = true })
callback(line)
vim.fn.feed_termcodes("<Esc>", "n")
end

for _, k in ipairs(rabbit.opts.default_keys.select) do
vim.api.nvim_buf_set_keymap(buf, "i", k, "", { callback = cb })
end
end


return M
12 changes: 7 additions & 5 deletions lua/rabbit/luadoc/init.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
---@class Rabbit.Keymap
---@field public open? string[] Keys to open the window
---@field public select? string[] Keys to select the current entry
---@field public close? string[] Keys to close the window
---@field public file_add? string[] Keys to add a file, like in Harpoon
---@field public file_del? string[] Keys to delete a file, like in Harpoon
---@field public open string[] Keys to open the window
---@field public select string[] Keys to select the current entry
---@field public close string[] Keys to close the window
---@field public file_add string[] Keys to add a file, like in Harpoon
---@field public file_del string[] Keys to delete a file, like in Harpoon
---@field public group string[] Keys to create a collection of files
---@field public group_up string[] Keys to move up a collection
---@field [string] string[]


Expand Down
1 change: 1 addition & 0 deletions lua/rabbit/luadoc/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,6 @@
---| "RabbitNil" # Color of Blank Filename
---| "RabbitTerm" # Color of extras, eg :term or :Oil
---| "RabbitMsg" # Color of messages
---| "RabbitInput" # Color of the input prompt background


16 changes: 15 additions & 1 deletion lua/rabbit/luadoc/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
---@field close? fun(integer) Close Rabbit window
---@field file_add? fun(integer) Add a file, like in Harpoon
---@field file_del? fun(integer) Delete a file, like in Harpoon
---@field group? fun(integer) Create a collection of files
---@field group_up? fun(integer) Move up a collection
---@field [string] fun(integer)


Expand All @@ -33,13 +35,18 @@
---@field close? string[] Keys to close the window
---@field file_add? string[] Keys to add a file, like in Harpoon
---@field file_del? string[] Keys to delete a file, like in Harpoon
---@field group? string[] Keys to create a collection of files
---@field group_up? string[] Keys to move up a collection
---@field [string] string[]


---@class Rabbit.Plugin.Listing
---@field [0] Rabbit.Plugin.Listing.Window Listing shown to the user
---@field persist? Rabbit.Plugin.Listing.Persist Internal persistent listing
---@field opened? Rabbit.Plugin.Listing.Window Tracks open files
---@field collections? Rabbit.Plugin.Listing.Persist.Recursive Tracks collections
---@field recursive? Rabbit.Plugin.Listing.Persist.Recursive
---@field paths? Rabbit.Plugin.Listing.Persist.Table[]
---@field [integer] Rabbit.Plugin.Listing.Window
---@field [string] Rabbit.Plugin.Listing.Window

Expand Down Expand Up @@ -89,6 +96,7 @@
---@field RabbitEnter? fun(winnr: integer) Called when the Rabbit window is opened
---@field [string] Rabbit.Event.Handler


---@alias Rabbit.Event.Handler fun(evt: NvimEvent, winid: integer)


Expand All @@ -98,10 +106,16 @@

---@class Rabbit.Plugin.Listing.Persist.Table
---@field [integer] string Just the filename; no Oxide details
---@field [string] Rabbit.Plugin.Listing.Persist.Entry `File Name : Entry` table
---@field [string] Rabbit.Plugin.Listing.Persist.Entry | Rabbit.Plugin.Listing.Persist.Table `File Name : Entry` table


---@class Rabbit.Plugin.Listing.Persist.Entry
---@field age integer The last time the file was accessed
---@field count integer The total number of times this file was accessed


---@alias Rabbit.Plugin.Listing.Persist.Recursive
---| Rabbit.Plugin.Listing.Persist
---| Rabbit.Plugin.Listing.Persist.Table
---| Rabbit.Plugin.Listing.Persist.Entry

16 changes: 9 additions & 7 deletions lua/rabbit/luadoc/screen.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
---@class (exact) Rabbit.Screen.Border_Kwargs
---@field colors Rabbit.Options.Color
---@field border_color NvimHlKwargs | string
---@field width integer
---@field height integer
---@field emph_width integer
---@field box Rabbit.Box
---@field fullscreen boolean
---@field title string
---@field mode string
---@field width integer Window width
---@field height integer Window height
---@field emph_width integer Emphasis character width
---@field box Rabbit.Box Box style
---@field fullscreen boolean Full screen
---@field title string Window title
---@field mode string Plugin name
---@field pos_col integer Window position: column
---@field pos_row integer Window position: row


---@class Rabbit.Screen.Spec
Expand Down
Loading

0 comments on commit 8f01c2d

Please sign in to comment.