Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix prompt UI #46

Merged
merged 5 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions autoload/neural.vim
Original file line number Diff line number Diff line change
Expand Up @@ -355,3 +355,5 @@ augroup NeuralCleanupGroup
autocmd VimSuspend call neural#Cleanup()
endif
augroup END

highlight NeuralPromptBorder ctermfg=172 guifg=#ff9d0a
23 changes: 18 additions & 5 deletions doc/neural.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CONTENTS *neural-contents*
4.2 Neural Buffer ............................. |neural-buffer|
4.3 OpenAI .................................... |neural-openai|
4.4 ChatGPT ................................... |neural-chatgpt|
4.5 Highlights ................................ |neural-highlights|
5. API ........................................ |neural-api|
6. Environment Variables ...................... |neural-env|
6.1 Linux + KDE ............................. |neural-env-kde|
Expand Down Expand Up @@ -40,7 +41,7 @@ appropriate value. OpenAI is the default data source.
3. Commands/Keybinds *neural-commands*


Neural *Neural*
`:Neural` *Neural*

Prompt Neural for a response. e.g. `:Neural say hello`

Expand All @@ -55,7 +56,7 @@ Neural *Neural*
completes writing text to a buffer.


NeuralBuffer *NeuralBuffer*
`:NeuralBuffer` *NeuralBuffer*

Create a buffer with a `neuralbuf` filetype for interacting with neural
sources directly. This can be a useful scratch buffer and playground for code
Expand All @@ -67,7 +68,7 @@ NeuralBuffer *NeuralBuffer*
A plug mapping `<Plug>(neural_buffer)` is defined for this.


NeuralCompletion *NeuralCompletion*
`:NeuralCompletion` *NeuralCompletion*

A command for a |NeuralBuffer| (`neuralbuf` filetype) that sends all buffer
contents to the current neural source for completion and appends the
Expand All @@ -77,7 +78,7 @@ NeuralCompletion *NeuralCompletion*
with the default: `<C-CR>`


NeuralExplain *NeuralExplain*
`:NeuralExplain` *NeuralExplain*

A |visual-mode| command for explaining the highlighted lines. The visual
selection will be sent to the currently selected source model and the
Expand All @@ -89,7 +90,7 @@ NeuralExplain *NeuralExplain*
A plug mapping `<Plug>(neural_explain)` is defined for this.


NeuralStop *NeuralStop*
`:NeuralStop` *NeuralStop*

Stop any currently running Neural tasks, and immediately stop printing text
to a Vim buffer at the first available opportunity.
Expand Down Expand Up @@ -405,6 +406,18 @@ g:neural.source.chatgpt.top_p *g:neural.source.chatgpt.top_p*
See Also: |g:neural.source.openai.top_p|


-------------------------------------------------------------------------------
4.5 Highlights *neural-highlights*

The following highlights can be configured to change |neural|'s colors.


`NeuralPromptBorder` *NeuralPromptBorder*

Default: `ctermfg=172 guifg=#ff9d0a`

Color for the |Neural| prompt border.

===============================================================================
5. API *neural-api*

Expand Down
63 changes: 27 additions & 36 deletions lua/neural/ui.lua
Original file line number Diff line number Diff line change
@@ -1,72 +1,63 @@
-- UI module for creating the nui.nvim input popup
-- Author: Anexon <[email protected]>
-- Description: UI module for creating the prompt with nui.nvim input popup.

local Input = require('nui.input')
local Event = require('nui.utils.autocmd').event

local UI = {}

-- TODO: Expose option configuration.
local opts = {
default_value = '',
winblend = 0,
style = 'rounded'
}

-- Prompts the user for input.
--- @param title string The title of the prompt.
--- @param on_submit function The function to call when the user submits the prompt.
function UI.prompt(title, on_submit)
-- TODO: Make escape keys configurable.
local exit_keys = {
{'n', 'q',
function(_)
vim.api.nvim_command(':q')
end, {noremap = true},
},
{'n', '<ESC>',
function(_)
vim.api.nvim_command(':q')
end, {noremap = true},
},
{'i', '<ESC>',
function(_)
vim.api.nvim_command(':q')
end, {noremap = true},
},
{'i', '<C-c>',
function(_)
vim.api.nvim_command(':q')
end, {noremap = true},
},
}

-- TODO: Make prompt more configurable.
local input = Input({
position = {row = '85.2%', col = '50%'},
size = {
width = '51.8%',
height = '20%',
},
size = { width = '51.8%', height = '20%'},
relative = 'editor',
border = {
highlight = 'NeuralPromptBorder',
style = 'rounded',
style = opts.style,
text = {
top = title,
top_align = 'center',
},
},
win_options = {
winblend = 10,
winhighlight = 'Normal:Normal',
winblend = opts.winblend,
},
}, {
prompt = vim.g.neural.ui.prompt_icon .. ' ',
default_value = '',
default_value = opts.default_value,
on_close = function() end,
on_submit = function(value)
on_submit(value)
end,
})
input:mount()

-- Handle unmounting
input:on(Event.BufLeave, function()
input:unmount()
end)
for _, v in ipairs(exit_keys) do
input:map(unpack(v))

local exit_keys = {
{'n', 'q'},
{'n', '<ESC>'},
{'i', '<ESC>'},
{'n', '<C-c>'},
{'i', '<C-c>'},
}
for _, key in ipairs(exit_keys) do
input:map(key[1], key[2], function()
input:unmount()
end, { noremap = true })
end
end

Expand Down
Loading