Skip to content

Commit befa31b

Browse files
author
zhangfuwen
committed
feat: clone, create(wip)
1 parent 48b0e3e commit befa31b

File tree

13 files changed

+859
-70
lines changed

13 files changed

+859
-70
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ jobs:
2323
- name: panvimdoc
2424
uses: kdheepak/panvimdoc@main
2525
with:
26-
vimdoc: nvim-plugin-template.nvim
26+
vimdoc: github.nvim.nvim
2727
treesitter: true
2828
- uses: stefanzweifel/git-auto-commit-action@v4
2929
with:
3030
commit_message: "chore(doc): auto generate docs"
3131
commit_user_name: "github-actions[bot]"
3232
commit_user_email: "github-actions[bot]@users.noreply.github.com"
3333
commit_author: "github-actions[bot] <github-actions[bot]@users.noreply.github.com>"
34+
ub.com>"
File renamed without changes.

lua/github_nvim/clone.lua

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
util = require("github_nvim.util")
2+
3+
M = {}
4+
5+
6+
function M.clone(config)
7+
is_repo_public = true
8+
message = "no message"
9+
messageHighlight = "SpecialKey"
10+
repo_input = ""
11+
local Popup = require("nui.popup")
12+
local Layout = require("nui.layout")
13+
local Input = require("nui.input")
14+
local event = require("nui.utils.autocmd").event
15+
local NuiLine = require("nui.line")
16+
local NuiText = require("nui.text")
17+
18+
local clone_input = Input({
19+
position = "50%",
20+
size = {
21+
width = 20,
22+
},
23+
border = {
24+
style = "single",
25+
text = {
26+
top = "clone: (user/repo)",
27+
top_align = "center",
28+
},
29+
},
30+
win_options = {
31+
winhighlight = "Normal:Normal,FloatBorder:Normal",
32+
},
33+
}, {
34+
prompt = "> ",
35+
default_value = repo_input,
36+
on_close = function()
37+
print("Input Closed!")
38+
end,
39+
on_submit = function(value)
40+
print("Input Submitted: " .. value)
41+
end,
42+
on_change = function(value)
43+
repo_input = value
44+
end,
45+
})
46+
47+
local popup_one = Popup({
48+
enter = true,
49+
border = "single",
50+
})
51+
52+
local function set_hints(keymap, linenr)
53+
local line = NuiLine()
54+
line:append(keymap.lhs, "SpecialKey")
55+
line:append("\t")
56+
line:append(keymap.opts.desc)
57+
local bufnr, ns_id, linenr_start = popup_one.bufnr, -1, linenr
58+
line:render(bufnr, ns_id, linenr_start)
59+
end
60+
61+
local function update_status()
62+
local line = NuiLine()
63+
line:append("Options: ")
64+
local bufnr, ns_id, linenr_start = popup_one.bufnr, -1, 1
65+
line:render(bufnr, ns_id, linenr_start)
66+
67+
line = NuiLine()
68+
line:append(" Visibility: ")
69+
line:append(is_repo_public and "public" or "private", "Error")
70+
line:append(" <c-p>", "SpecialKey")
71+
local bufnr, ns_id, linenr_start = popup_one.bufnr, -1, 2
72+
line:render(bufnr, ns_id, linenr_start)
73+
74+
--NuiLine({ NuiText("one"), NuiText("two", "Error")}):render(bufnr, ns_id, 3)
75+
NuiLine({ NuiText("") }):render(bufnr, ns_id, 3)
76+
NuiLine({ NuiText("Message: "), NuiText(string.gsub(message, "\n", ""), messageHighlight) }):render(bufnr, ns_id, 4)
77+
NuiLine({ NuiText("") }):render(bufnr, ns_id, 5)
78+
79+
line = NuiLine()
80+
line:append("Keymaps: ")
81+
local bufnr, ns_id, linenr_start = popup_one.bufnr, -1, 6
82+
line:render(bufnr, ns_id, linenr_start)
83+
end
84+
85+
local layout = Layout(
86+
{
87+
position = "50%",
88+
size = {
89+
width = 80,
90+
height = "60%",
91+
},
92+
},
93+
Layout.Box({
94+
Layout.Box(clone_input, { size = "10%" }),
95+
Layout.Box(popup_one, { size = "40%" }),
96+
}, { dir = "col" })
97+
)
98+
99+
local function update_message(msg, hl)
100+
message = msg
101+
messageHighlight = hl
102+
update_status()
103+
end
104+
105+
local function do_clone(user_name, repo_name)
106+
local user_dir = config.github_dir .. config.sep .. user_name
107+
local repo_dir = user_dir .. config.sep .. repo_name
108+
if util.path_exists(repo_dir) then
109+
update_message("Error: repo exists", "Error")
110+
util.promptYesNo("remove local and retry?", function()
111+
local code = util.rm_rf(repo_dir)
112+
-- print(string.format("rm command returns %d", code))
113+
do_clone(user_name, repo_name)
114+
end)
115+
return
116+
end
117+
118+
if not util.path_exists(user_dir) then
119+
util.mkdir_p(user_dir)
120+
end
121+
122+
local clone_command = "gh repo clone " .. user_name .. config.sep .. repo_name
123+
message = "running command " .. clone_command .. " ..."
124+
update_status()
125+
126+
local function on_command_exit(result)
127+
vim.schedule(function()
128+
if result.code == 0 then
129+
update_message("success, path: ".. repo_dir, "Error")
130+
if config.on_clone_success then
131+
promptYesNo("close window and open it?", function()
132+
layout:unmount()
133+
config.on_clone_success(repo_dir)
134+
end)
135+
else
136+
promptYesNo("close window?", function()
137+
layout:unmount()
138+
end)
139+
end
140+
else
141+
update_message("failed, reason: " .. result.stderr, "Error")
142+
promptYesNo("remove local and retry?", function()
143+
util.rm_rf(repo_dir)
144+
do_clone(user_name, repo_name)
145+
end)
146+
end
147+
end
148+
)
149+
end
150+
151+
152+
vim.system({ "bash", "-c", clone_command }, {
153+
text = true,
154+
cwd = user_dir,
155+
}, on_command_exit)
156+
157+
-- 错误处理
158+
end
159+
local function handle_clone()
160+
local user_name = ""
161+
local repo_name = ""
162+
local tokens = util.mysplit(repo_input, "/")
163+
if #tokens > 2 or #tokens <= 1 then
164+
message = "Error: invalid input, should be user/repo"
165+
messageHighlight = "Error"
166+
update_status()
167+
return
168+
elseif #tokens == 2 then
169+
user_name = tokens[1]
170+
repo_name = tokens[2]
171+
-- elseif #tokens == 1 then
172+
-- repo_name = tokens[1]
173+
end
174+
message = user_name and string.format("user_name:%s, repo:%s", user_name, repo_name) or "user_name: " ..
175+
user_name
176+
messageHighlight = "Error"
177+
update_status()
178+
179+
print(string.format("repo: %s, visibility: %s", repo_input, is_repo_public and "public" or "private"))
180+
do_clone(user_name, repo_name)
181+
end
182+
--gh repo create github.nvim --template nvimdev/nvim-plugin-template --public --clone
183+
184+
185+
186+
update_status()
187+
keymaps = {
188+
{
189+
mode = { "i", "n" },
190+
lhs = "<c-p>",
191+
rhs = function()
192+
is_repo_public = not is_repo_public
193+
update_status()
194+
end,
195+
opts = {
196+
desc = "change visibility"
197+
}
198+
199+
},
200+
{
201+
mode = { "i", "n" },
202+
lhs = "<c-g>",
203+
rhs = handle_clone,
204+
opts = {
205+
desc = "do clone"
206+
}
207+
208+
},
209+
{
210+
mode = { "i" },
211+
lhs = "<cr>",
212+
rhs = handle_clone,
213+
opts = {
214+
desc = "do clone"
215+
}
216+
217+
},
218+
{
219+
mode = { "i", "n" },
220+
lhs = "<c-c>",
221+
rhs = function()
222+
print("close")
223+
layout:unmount()
224+
end,
225+
opts = {
226+
desc = "close window"
227+
}
228+
229+
}
230+
}
231+
232+
for i, keymap in ipairs(keymaps) do
233+
for _, mode in ipairs(keymap.mode) do
234+
clone_input:map(mode, keymap.lhs, keymap.rhs, keymap.opts)
235+
popup_one:map(mode, keymap.lhs, keymap.rhs, keymap.opts)
236+
end
237+
set_hints(keymap, 6 + i)
238+
end
239+
240+
layout:mount()
241+
end
242+
243+
244+
return M

lua/github_nvim/config.lua

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
local sep = vim.fn.has("win32") == 1 and "\\" or "/"
2+
local home = vim.env.HOME or os.getenv("HOME")
3+
local defaults = {
4+
on_clone_success = nil, -- function(local_path)
5+
sep = sep,
6+
home = home,
7+
github_dir = home .. sep .. "Code" .. sep .. "github.com",
8+
spawn_command = {
9+
python = "ipython",
10+
scala = "sbt console",
11+
lua = "ilua",
12+
}
13+
}
14+
15+
local function set(_, key, value)
16+
defaults[key] = value
17+
end
18+
19+
local function get(_, key)
20+
return defaults[key]
21+
end
22+
23+
return {
24+
defaults = defaults,
25+
get = get,
26+
set = set
27+
}

0 commit comments

Comments
 (0)