Skip to content

Commit

Permalink
Merge pull request #33 from grapp-dev/feature/buffer-component
Browse files Browse the repository at this point in the history
feat: add Buffer component
  • Loading branch information
mobily authored Apr 19, 2024
2 parents 657cae2 + 71f906e commit 7e43726
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 0 deletions.
100 changes: 100 additions & 0 deletions docs/pages/docs/components/buffer.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { Callout } from 'nextra/components';
import { Property } from '../../../components/Property'

## Buffer

`Buffer` is a component that handles window layout only and renders the contents of a given buffer.

![](/gifs/buffer-1.gif)

### Usage Example

<Callout type="info">
The code example is using the [`gen.nvim`](https://github.com/David-Kunz/gen.nvim) plugin.
</Callout>

```lua
local renderer = n.create_renderer({
width = 60,
height = 3,
})

local signal = n.create_signal({
chat = "",
is_preview_visible = false,
})

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

local body = function()
return n.rows(
n.text_input({
border_label = "Chat",
autofocus = true,
wrap = true,
on_change = function(value)
signal.chat = value
end,
}),
n.buffer({
id = "preview",
flex = 1,
buf = buf,
autoscroll = true,
border_label = "Preview",
hidden = signal.is_preview_visible:negate(),
})
)
end

renderer:add_mappings({
{
mode = { "n", "i" },
key = "<D-CR>",
handler = function()
local gen = require("gen")
local state = signal:get_value()

renderer:set_size({ height = 20 })
signal.is_preview_visible = true

renderer:schedule(function()
gen.float_win = renderer:get_component_by_id("preview").winid
gen.result_buffer = buf
gen.exec({ prompt = state.chat })
end)
end,
},
})

renderer:render(body)
```

### Properties

#### buf

<Property
required={true}
types={['number']}
/>

#### autoscroll

<Property
defaultValue="false"
types={['boolean']}
/>

#### filetype

<Property
types={['string']}
/>

#### linebreak

<Property
defaultValue="true"
types={['boolean']}
/>
Binary file added docs/public/gifs/buffer-1.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions lua/nui-components/buffer.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
local Component = require("nui-components.component")
local buf_storage = require("nui.utils.buf_storage")

local fn = require("nui-components.utils.fn")

local Buffer = Component:extend("Buffer")

function Buffer:init(props, popup_options)
Buffer.super.init(
self,
fn.merge({
buf = nil,
autoscroll = false,
}, props),
fn.deep_merge({
buf_options = {
filetype = props.filetype or "",
},
win_options = {
linebreak = fn.default_to(props.linebreak, true),
},
}, popup_options)
)
end

function Buffer:prop_types()
return {
buf = "number",
autoscroll = "boolean",
filetype = { "string", "nil" },
}
end

function Buffer:_buf_create()
local props = self:get_props()

self.bufnr = props.buf

if props.autoscroll then
self._private.autoscroll_id = vim.api.nvim_create_autocmd("TextChanged", {
buffer = self.bufnr,
callback = function()
if not self.winid or not vim.api.nvim_win_is_valid(self.winid) then
return
end

if not self.bufnr or not vim.api.nvim_buf_is_valid(self.bufnr) then
return true
end

vim.api.nvim_win_set_cursor(self.winid, {
vim.api.nvim_buf_line_count(self.bufnr),
9999,
})
end,
})
end
end

function Buffer:_buf_destroy()
buf_storage.cleanup(self.bufnr)

if self._private.autoscroll_id then
pcall(vim.api.nvim_del_autocmd, self._private.autoscroll_id)
self._private.autoscroll_id = nil
end

self.bufnr = nil
end

function Buffer:on_update()
local props = self:get_props()

if props.buf and self.bufnr ~= props.buf and self._.layout_ready then
local is_focused = vim.api.nvim_get_current_win() == self.winid

self:unmount()
self:mount()

if is_focused then
vim.api.nvim_set_current_win(self.winid)
end
end
end

return Buffer
1 change: 1 addition & 0 deletions lua/nui-components/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ M.checkbox = require("nui-components.checkbox")
M.paragraph = require("nui-components.paragraph")
M.spinner = require("nui-components.spinner")
M.animated_text = require("nui-components.animated-text")
M.buffer = require("nui-components.buffer")

M.tabs = add_children_prop(Tabs)
M.tab = add_children_prop(Tab)
Expand Down

0 comments on commit 7e43726

Please sign in to comment.