From b1ea6ef06c83b82b8623368b07e0a209e3c65900 Mon Sep 17 00:00:00 2001 From: Anexon <38880939+Angelchev@users.noreply.github.com> Date: Wed, 3 Jan 2024 22:47:35 +0000 Subject: [PATCH] Fix prompt UI (#46) * Close #33 fix: default prompt border highlight * docs: improve command formatting * docs: Add highlights options * Close #45 - fix: unmount nui prompt correctly --- autoload/neural.vim | 2 ++ doc/neural.txt | 23 +++++++++++++---- lua/neural/ui.lua | 63 +++++++++++++++++++-------------------------- 3 files changed, 47 insertions(+), 41 deletions(-) diff --git a/autoload/neural.vim b/autoload/neural.vim index 5a55ed0..b7381da 100644 --- a/autoload/neural.vim +++ b/autoload/neural.vim @@ -355,3 +355,5 @@ augroup NeuralCleanupGroup autocmd VimSuspend call neural#Cleanup() endif augroup END + +highlight NeuralPromptBorder ctermfg=172 guifg=#ff9d0a diff --git a/doc/neural.txt b/doc/neural.txt index de40433..618c9b1 100644 --- a/doc/neural.txt +++ b/doc/neural.txt @@ -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| @@ -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` @@ -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 @@ -67,7 +68,7 @@ NeuralBuffer *NeuralBuffer* A plug mapping `(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 @@ -77,7 +78,7 @@ NeuralCompletion *NeuralCompletion* with the default: `` -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 @@ -89,7 +90,7 @@ NeuralExplain *NeuralExplain* A plug mapping `(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. @@ -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* diff --git a/lua/neural/ui.lua b/lua/neural/ui.lua index b3c0edf..63ea30e 100644 --- a/lua/neural/ui.lua +++ b/lua/neural/ui.lua @@ -1,72 +1,63 @@ --- UI module for creating the nui.nvim input popup +-- Author: Anexon +-- 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', '', - function(_) - vim.api.nvim_command(':q') - end, {noremap = true}, - }, - {'i', '', - function(_) - vim.api.nvim_command(':q') - end, {noremap = true}, - }, - {'i', '', - 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', ''}, + {'i', ''}, + {'n', ''}, + {'i', ''}, + } + for _, key in ipairs(exit_keys) do + input:map(key[1], key[2], function() + input:unmount() + end, { noremap = true }) end end