Skip to content

Commit 94dce26

Browse files
authored
Merge pull request #8 from lnc3l0t/list-gists
feat: added ListGists command
2 parents 13dc9cb + 794b2f3 commit 94dce26

File tree

5 files changed

+152
-3
lines changed

5 files changed

+152
-3
lines changed

README.md

+35-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,39 @@ The plugin uses the gh command-line tool to create the Gist and provides a simpl
77

88
To use `gist.nvim`, you need to have Neovim installed on your system.
99
You also need to have the gh command-line tool installed and configured with your GitHub account.
10+
If you intend to use the `ListGists` command to list and edit all your gists, I suggest the `nvim-unception` plugin.
1011

1112
Once you have Neovim and gh installed, you can install `gist.nvim` using your favorite plugin manager.
12-
For example, if you are using packer, you can add the following line to your init.vim file:
1313

14+
#### Using [lazy.nvim](https://github.com/folke/lazy.nvim):
1415
```lua
15-
use "rawnly/gist.nvim"
16+
return {
17+
{
18+
"Rawnly/gist.nvim",
19+
cmd = { "CreateGist", "CreateGistFromFile", "ListGists" },
20+
init = function()
21+
vim.g.gist_is_private = false -- All gists will be private, you won't be prompted again
22+
vim.g.gist_clipboard = "+" -- The registry to use for copying the Gist URL
23+
end,
24+
},
25+
-- `ListGists` opens the selected gif in a terminal buffer,
26+
-- nvim-unception uses neovim remote rpc functionality to open the gist in an actual buffer
27+
-- and prevents neovim buffer inception
28+
{
29+
"samjwill/nvim-unception",
30+
lazy = false,
31+
init = function() vim.g.unception_block_while_host_edits = true end
32+
}
33+
}
34+
```
35+
#### Using [packer.nvim](https://github.com/wbthomason/packer.nvim):
36+
```lua
37+
use {
38+
"rawnly/gist.nvim",
39+
-- `ListGists` opens the selected gif in a terminal buffer,
40+
-- this plugin uses neovim remote rpc functionality to open the gist in an actual buffer and not have buffer inception
41+
requires = { "samjwill/nvim-unception", setup = function() vim.g.unception_block_while_host_edits = true end }
42+
}
1643
```
1744

1845
## Usage
@@ -34,6 +61,12 @@ If you pass `[public=true]` it won't prompt for privacy later.
3461

3562
After you enter the description and privacy settings, the plugin ask for confirmation and will create the Gist using the gh command-line tool and copy the Gist's URL to the given clipboard registry.
3663

64+
You can also list your gists and edit their files on the fly.
65+
```vim
66+
:ListGists
67+
```
68+
- `:ListGists` will list all your gists and after you select one it will open a buffer to edit it
69+
3770
## Configuration
3871

3972
`gist.nvim` provides a few configuration options that you can set as global params:

doc/gist.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ NAME
66
SYNOPSIS
77
:CreateGist
88
:CreateGistFromFile
9+
:ListGists
910

1011
DESCRIPTION
1112
The `:CreateGist` command creates a GitHub Gist from the buffer selection using the `gh` command-line tool.
1213
The `:CreateGistFile` command creates a GitHub Gist from the current file using the `gh` command-line tool.
14+
The `:ListGists` command lists all the GitHub Gists from your account and allows you to edit them directly inside neovim.
1315

14-
The plugin prompts you for a description and privacy settings for the Gist, and then copies the URL of the created Gist to the system clipboard.
16+
The plugin prompts you for a description and privacy settings for the Gist creation, and then copies the URL of the created Gist to the system clipboard.
1517

1618
OPTIONS
1719
<description> You can provide description for the Gist as an argument to the command. If you don't provide a description, the plugin will prompt you for one.

lua/gist/core/gh.lua

+28
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,34 @@ function M.create_gist(filename, content, description, private)
4747
return url, nil
4848
end
4949

50+
--- List all Github gists
51+
--
52+
-- @return [string]|nil The URLs of all the Gists
53+
function M.list_gists()
54+
local cmd = "gh gist list"
55+
56+
local output = utils.exec(cmd)
57+
if type(output) == "string" then
58+
local list = {}
59+
60+
local gists = vim.split(output, "\n")
61+
table.remove(gists, #gists)
62+
63+
for _, gist in ipairs(gists) do
64+
local g = vim.split(gist, "\t")
65+
66+
table.insert(list, {
67+
hash = g[1],
68+
name = g[2],
69+
files = tonumber(g[3]:sub(1,1)),
70+
privacy = g[4],
71+
date = g[5]
72+
})
73+
end
74+
return list
75+
end
76+
end
77+
5078
--- Reads the configuration from the user's vimrc
5179
--
5280
-- @return table A table with the configuration properties

lua/gist/init.lua

+82
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,86 @@ function M.create_from_file(opts)
6969
})
7070
end
7171

72+
local function create_split_terminal(command)
73+
vim.cmd.vsplit()
74+
local win = vim.api.nvim_get_current_win()
75+
local buf = vim.api.nvim_create_buf(false, true)
76+
vim.api.nvim_win_set_buf(win, buf)
77+
vim.api.nvim_win_set_option(win, "number", false)
78+
vim.api.nvim_win_set_option(win, "relativenumber", false)
79+
vim.api.nvim_buf_set_name(buf, ("term://%s/%s"):format(buf, command[1]))
80+
vim.keymap.set("t", "<C-n>", "<Down>", { buffer = buf })
81+
vim.keymap.set("t", "<C-p>", "<Up>", { buffer = buf })
82+
vim.api.nvim_win_set_option(win, "winbar", "%=Use CTRL-{n,p} to cycle")
83+
vim.cmd.startinsert()
84+
return buf
85+
end
86+
87+
local function format_gist(g)
88+
return string.format("%s (%s) |%s 📃| [%s]",
89+
g.name, -- Gist name
90+
g.hash, -- Gist hash
91+
g.files, -- Gist files number
92+
g.privacy == "public" and "" or "" -- Gist privacy setting (public/private)
93+
)
94+
end
95+
96+
--- List user gists via telescope or vim.select.
97+
function M.list_gists()
98+
if pcall(require, "unception") and not vim.g.unception_block_while_host_edits then
99+
print("You need to set this option `:h g:unception_block_while_host_edits`")
100+
return
101+
end
102+
103+
local list = core.list_gists()
104+
if #list == 0 then
105+
print("No gists. You can create one from current buffer with `CreateGist`")
106+
return
107+
end
108+
109+
vim.ui.select(list, {
110+
prompt = "Select a gist to edit",
111+
format_item = format_gist
112+
}, function(gist)
113+
if not gist then return end
114+
115+
local job_id
116+
117+
local command = { "gh", "gist", "edit", gist.hash }
118+
local buf = create_split_terminal(command)
119+
120+
local term_chan_id = vim.api.nvim_open_term(buf, {
121+
on_input = function(_, _, _, data)
122+
vim.api.nvim_chan_send(job_id, data)
123+
end
124+
})
125+
126+
job_id = vim.fn.jobstart(command, vim.tbl_extend("force", {
127+
on_stdout = function(_, data)
128+
vim.api.nvim_chan_send(term_chan_id, table.concat(data, "\r\n"))
129+
130+
local changed = vim.fn.bufnr() ~= buf
131+
if changed then
132+
vim.api.nvim_buf_set_option(vim.fn.bufnr(), "bufhidden", "wipe")
133+
vim.api.nvim_buf_set_option(vim.fn.bufnr(), "swapfile", true)
134+
135+
local winbar = ("%sGIST `%s`"):format("%=", gist.name)
136+
vim.api.nvim_win_set_option(vim.fn.win_getid(), "winbar", winbar)
137+
end
138+
if gist.files > 1 and changed then
139+
vim.api.nvim_create_autocmd({ "BufDelete" }, {
140+
buffer = vim.fn.bufnr(),
141+
group = vim.api.nvim_create_augroup("gist_save", {}),
142+
callback = function() vim.cmd.startinsert() end
143+
})
144+
end
145+
end,
146+
on_exit = function()
147+
vim.api.nvim_buf_delete(buf, { force = true })
148+
end,
149+
pty = true,
150+
}, {}))
151+
end)
152+
end
153+
72154
return M

plugin/gist.lua

+4
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ vim.api.nvim_create_user_command("CreateGist", gist.create, {
1111
desc = "Create a Gist from the current selection.",
1212
range = true,
1313
})
14+
15+
vim.api.nvim_create_user_command("ListGists", gist.list_gists, {
16+
desc = "List user Gists.",
17+
})

0 commit comments

Comments
 (0)