Skip to content

Commit f6c4998

Browse files
author
zhangfuwen
committed
almost work
1 parent 224be46 commit f6c4998

File tree

10 files changed

+362
-456
lines changed

10 files changed

+362
-456
lines changed

README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
1-
# nvim-plugin-template
1+
# github_nvim
22

3-
Neovim plugin template; includes automatic documentation generation from README, integration tests with Busted, and linting with Stylua
43

54
## Usage
65

7-
1. Click `use this template` button generate a repo on your github.
8-
2. Clone your plugin repo. Open terminal then cd plugin directory.
9-
3. Run `python3 rename.py your-plugin-name`. This will replace all `nvim-plugin-template` to your `plugin-name`.
10-
Then it will prompt you input `y` or `n` to remove example codes in `init.lua` and
11-
`test/plugin_spec.lua`. If you are familiar this repo just input `y`. If you are looking at this template for the first time I suggest you inspect the contents. After this step `rename.py` will also auto-remove.
126

13-
Now you have a clean plugin environment. Enjoy!
7+
```lua
8+
9+
github_nvim = require("github_nvim")
10+
github_nvim.setup({})
11+
require('telescope').load_extension('github_repos')
12+
13+
vim.keymap.set("n", "<leader>ghr", function()
14+
vim.cmd("Telescope github_repos")
15+
end, { desc = "List github repos", buffer = bufnr })
16+
17+
vim.keymap.set("n", "<leader>ghc", function()
18+
require("github_nvim").clone()
19+
end, { desc = "Clone a github repo", buffer = bufnr })
20+
21+
vim.keymap.set("n", "<leader>ghn", function()
22+
require("github_nvim").create()
23+
end, { desc = "New github repo", buffer = bufnr })
24+
25+
```
1426

1527
## Format
1628

lua/github_nvim/clone.lua

Lines changed: 25 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
util = require("github_nvim.util")
2+
gh = require("github_nvim.gh")
23

34
M = {}
45

56

67
function M.clone(config)
7-
print(vim.inspect(config))
8-
is_repo_public = true
9-
message = "no message"
10-
messageHighlight = "SpecialKey"
11-
repo_input = ""
8+
local repo_input = ""
129
local Popup = require("nui.popup")
1310
local Layout = require("nui.layout")
1411
local Input = require("nui.input")
@@ -35,10 +32,8 @@ function M.clone(config)
3532
prompt = "> ",
3633
default_value = repo_input,
3734
on_close = function()
38-
print("Input Closed!")
3935
end,
4036
on_submit = function(value)
41-
print("Input Submitted: " .. value)
4237
end,
4338
on_change = function(value)
4439
repo_input = value
@@ -61,25 +56,8 @@ function M.clone(config)
6156

6257
local function update_status()
6358
local line = NuiLine()
64-
line:append("Options: ")
65-
local bufnr, ns_id, linenr_start = popup_one.bufnr, -1, 1
66-
line:render(bufnr, ns_id, linenr_start)
67-
68-
line = NuiLine()
69-
line:append(" Visibility: ")
70-
line:append(is_repo_public and "public" or "private", "Error")
71-
line:append(" <c-p>", "SpecialKey")
72-
local bufnr, ns_id, linenr_start = popup_one.bufnr, -1, 2
73-
line:render(bufnr, ns_id, linenr_start)
74-
75-
--NuiLine({ NuiText("one"), NuiText("two", "Error")}):render(bufnr, ns_id, 3)
76-
NuiLine({ NuiText("") }):render(bufnr, ns_id, 3)
77-
NuiLine({ NuiText("Message: "), NuiText(string.gsub(message, "\n", ""), messageHighlight) }):render(bufnr, ns_id, 4)
78-
NuiLine({ NuiText("") }):render(bufnr, ns_id, 5)
79-
80-
line = NuiLine()
8159
line:append("Keymaps: ")
82-
local bufnr, ns_id, linenr_start = popup_one.bufnr, -1, 6
60+
local bufnr, ns_id, linenr_start = popup_one.bufnr, -1, 1
8361
line:render(bufnr, ns_id, linenr_start)
8462
end
8563

@@ -97,67 +75,6 @@ function M.clone(config)
9775
}, { dir = "col" })
9876
)
9977

100-
local function update_message(msg, hl)
101-
message = msg
102-
messageHighlight = hl
103-
update_status()
104-
end
105-
106-
local function do_clone(user_name, repo_name)
107-
local user_dir = config.github_dir .. config.sep .. user_name
108-
local repo_dir = user_dir .. config.sep .. repo_name
109-
if util.path_exists(repo_dir) then
110-
update_message("Error: repo exists", "Error")
111-
util.promptYesNo("remove local and retry?", function()
112-
local code = util.rm_rf(repo_dir)
113-
-- print(string.format("rm command returns %d", code))
114-
do_clone(user_name, repo_name)
115-
end)
116-
return
117-
end
118-
119-
if not util.path_exists(user_dir) then
120-
print("making user_dir "..user_dir)
121-
util.mkdir_p(user_dir)
122-
end
123-
124-
local clone_command = "gh repo clone " .. user_name .. config.sep .. repo_name
125-
message = "running command " .. clone_command .. " ..."
126-
update_status()
127-
128-
local function on_command_exit(result)
129-
vim.schedule(function()
130-
if result.code == 0 then
131-
update_message("success, path: ".. repo_dir, "Error")
132-
if config.on_clone_success then
133-
util.promptYesNo("close window and open it?", function()
134-
layout:unmount()
135-
config.on_clone_success(repo_dir)
136-
end)
137-
else
138-
util.promptYesNo("close window?", function()
139-
layout:unmount()
140-
end)
141-
end
142-
else
143-
update_message("failed, reason: " .. result.stderr, "Error")
144-
util.promptYesNo("remove local and retry?", function()
145-
util.rm_rf(repo_dir)
146-
do_clone(user_name, repo_name)
147-
end)
148-
end
149-
end
150-
)
151-
end
152-
153-
154-
vim.system({ "bash", "-c", clone_command }, {
155-
text = true,
156-
cwd = user_dir,
157-
}, on_command_exit)
158-
159-
-- 错误处理
160-
end
16178
local function handle_clone()
16279
local user_name = ""
16380
local repo_name = ""
@@ -178,27 +95,23 @@ function M.clone(config)
17895
messageHighlight = "Error"
17996
update_status()
18097

181-
print(string.format("repo: %s, visibility: %s", repo_input, is_repo_public and "public" or "private"))
182-
do_clone(user_name, repo_name)
98+
gh.do_clone(config, user_name, repo_name, {
99+
on_success = function(repo_dir)
100+
layout:unmount()
101+
util.open_project(repo_dir)
102+
end,
103+
on_fail = function()
104+
layout:unmount()
105+
end
106+
107+
})
183108
end
184109
--gh repo create github.nvim --template nvimdev/nvim-plugin-template --public --clone
185110

186111

187112

188113
update_status()
189114
keymaps = {
190-
{
191-
mode = { "i", "n" },
192-
lhs = "<c-p>",
193-
rhs = function()
194-
is_repo_public = not is_repo_public
195-
update_status()
196-
end,
197-
opts = {
198-
desc = "change visibility"
199-
}
200-
201-
},
202115
{
203116
mode = { "i", "n" },
204117
lhs = "<c-g>",
@@ -221,7 +134,17 @@ function M.clone(config)
221134
mode = { "i", "n" },
222135
lhs = "<c-c>",
223136
rhs = function()
224-
print("close")
137+
layout:unmount()
138+
end,
139+
opts = {
140+
desc = "close window"
141+
}
142+
143+
},
144+
{
145+
mode = { "i", "n" },
146+
lhs = "<Esc>",
147+
rhs = function()
225148
layout:unmount()
226149
end,
227150
opts = {
@@ -236,11 +159,10 @@ function M.clone(config)
236159
clone_input:map(mode, keymap.lhs, keymap.rhs, keymap.opts)
237160
popup_one:map(mode, keymap.lhs, keymap.rhs, keymap.opts)
238161
end
239-
set_hints(keymap, 6 + i)
162+
set_hints(keymap, 1 + i)
240163
end
241164

242165
layout:mount()
243166
end
244167

245-
246168
return M

lua/github_nvim/config.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
local sep = vim.fn.has("win32") == 1 and "\\" or "/"
22
local home = vim.env.HOME or os.getenv("HOME")
33
local defaults = {
4-
on_clone_success = nil, -- function(local_path)
54
sep = sep,
65
home = home,
76
github_dir = home .. sep .. "Code" .. sep .. "github.com",

lua/github_nvim/create.lua

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function M.create(config)
2727
border = {
2828
style = "single",
2929
text = {
30-
top = "clone: (user/repo)",
30+
top = "create: (user/repo)",
3131
top_align = "center",
3232
},
3333
},
@@ -180,16 +180,10 @@ function M.create(config)
180180
vim.schedule(function()
181181
if result.code == 0 then
182182
update_message("success, path: " .. repo_dir, "Error")
183-
if config.on_clone_success then
184-
util.promptYesNo("close window and open it?", function()
185-
layout:unmount()
186-
config.on_clone_success(repo_dir)
187-
end)
188-
else
189-
util.promptYesNo("close window?", function()
190-
layout:unmount()
191-
end)
192-
end
183+
util.promptYesNo("close window and open it?", function()
184+
layout:unmount()
185+
util.open_project(repo_dir)
186+
end)
193187
else
194188
update_message("failed, reason: " .. result.stderr, "Error")
195189
util.promptYesNo("remove local and retry?", function()
@@ -306,6 +300,17 @@ function M.create(config)
306300
opts = {
307301
desc = "close window"
308302
}
303+
},
304+
{
305+
mode = { "i", "n" },
306+
lhs = "<Esc>",
307+
rhs = function()
308+
print("close")
309+
layout:unmount()
310+
end,
311+
opts = {
312+
desc = "close window"
313+
}
309314

310315
}
311316
}

lua/github_nvim/gh.lua

Lines changed: 76 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,82 @@
1+
local vim = vim
2+
local util = require("github_nvim.util")
13
M = {}
24

35
function M.get_github_username()
4-
local result = vim.fn.system("gh api user --jq '.login' 2>/dev/null")
5-
if vim.v.shell_error == 0 then
6-
return string.gsub(result, "\n$", "") -- remove newline
7-
else
8-
return nil
9-
end
6+
local result = vim.fn.system("gh api user --jq '.login' 2>/dev/null")
7+
if vim.v.shell_error == 0 then
8+
return string.gsub(result, "\n$", "") -- remove newline
9+
else
10+
return nil
11+
end
1012
end
1113

14+
local plugin = util.plugin_name
15+
local function do_clone(config, user_name, repo_name, opts)
16+
opts = opts or {}
17+
local user_dir = config.github_dir .. config.sep .. user_name
18+
local repo_dir = user_dir .. config.sep .. repo_name
19+
if util.path_exists(repo_dir) then
20+
vim.notify("Error: repo exists", vim.log.levels.ERROR, { title = plugin })
21+
util.promptYesNo("remove local and retry?", function()
22+
util.rm_rf(repo_dir)
23+
do_clone(config, user_name, repo_name, opts)
24+
end)
25+
return
26+
end
27+
28+
if not util.path_exists(user_dir) then
29+
print("making user_dir " .. user_dir)
30+
util.mkdir_p(user_dir)
31+
end
32+
33+
34+
35+
local clone_command = "gh repo clone " .. user_name .. config.sep .. repo_name
36+
local message = "running command " .. clone_command .. " ..."
37+
vim.notify(message, vim.log.levels.INFO, {
38+
title = plugin,
39+
on_open = function()
40+
local function on_command_exit(result)
41+
vim.schedule(function()
42+
if result.code == 0 then
43+
vim.notify("success, path: " .. repo_dir, vim.log.levels.INFO, { title = plugin })
44+
if opts.on_success then opts.on_success(repo_dir) end
45+
else
46+
vim.notify("failed, reason: " .. result.stderr, "Error", { title = plugin })
47+
util.promptYesNo("retry(remove local)?", function()
48+
util.rm_rf(repo_dir)
49+
do_clone(config, user_name, repo_name, opts)
50+
end, function()
51+
if opts.on_fail then opts.on_fail() end
52+
end
53+
)
54+
end
55+
end
56+
)
57+
end
58+
59+
60+
vim.system({ "bash", "-c", clone_command }, {
61+
text = true,
62+
cwd = user_dir,
63+
}, on_command_exit)
64+
65+
-- local timer = vim.loop.new_timer()
66+
-- timer:start(2000, 0, function()
67+
-- vim.notify({ "executing ", "Please wait..." }, "info", {
68+
-- title = plugin,
69+
-- timeout = 3000,
70+
-- on_close = function()
71+
-- vim.notify("Problem solved", nil, { title = plugin })
72+
-- vim.notify("Error code 0x0395AF", 1, { title = plugin })
73+
-- end,
74+
-- })
75+
-- end)
76+
end,
77+
})
78+
end
79+
80+
M.do_clone = do_clone
81+
1282
return M

lua/github_nvim/init.lua

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
local M = {}
22
local config = require("github_nvim.config")
33

4-
5-
-- M.github_repos = require('github_nvim.pickers.github_repos')
6-
-- local telescope = require("telescope")
7-
-- telescope.register_extension {
8-
-- exports = {
9-
-- github_repos = require('github_nvim/pickers/github_repos')
10-
-- }
11-
-- }
12-
require('telescope').load_extension('github_repos')
13-
144
function M.setup(options)
155
setmetatable(M, {
166
__newindex = config.set,

0 commit comments

Comments
 (0)