chore: implement OpenInGhRepo with utils

This commit is contained in:
Ali Almohaya 2022-09-26 02:09:22 +03:00
parent ba9664ce7b
commit 095ff55a15
No known key found for this signature in database
GPG key ID: 4B80BC43FC6007E0
3 changed files with 106 additions and 23 deletions

View file

@ -1,21 +1,47 @@
local utils = require("openingh.utils")
local M = {}
local M = {
setup = function (debug)
-- TODO - 1: add the repo path to global var
if (debug) then
print("debugging openingh.nvim")
end
end,
function M.setup()
-- TODO - 1: add the repo path to global var
M.cwd = vim.fn.getcwd()
local repo_url = vim.fn.system("git config --get remote.origin.url")
openFile = function ()
-- TODO - 1: get the url from git folder
-- TODO - 2: get the current line in the buffer and add it to the file url
-- TODO - 3: get the selected range in the buffer and add it to the file url
end,
openRepo = function ()
-- TODO - 1: get the remote url and open it
if repo_url:len() == 0 then
M.is_no_git_origin = true
vim.g.openingh = false
return
end
}
local username_and_reponame = utils.get_username_reponame(repo_url)
M.username = username_and_reponame.username
M.reponame = username_and_reponame.reponame
M.gh_url = "https://github.com/"
end
function M.openFile()
-- TODO - 1: get the url from git folder
-- TODO - 2: get the current line in the buffer and add it to the file url
-- TODO - 3: get the selected range in the buffer and add it to the file url
if M.is_no_git_origin then
utils.print_no_remote_message()
return
end
end
function M.openRepo()
if M.is_no_git_origin then
utils.print_no_remote_message()
return
end
local repo_url = M.gh_url .. M.username .. "/" .. M.reponame
local result = utils.open_url(repo_url)
if result then
print("Opening url " .. repo_url)
else
print("Unknown OS please open report")
end
end
return M

View file

@ -1,8 +1,67 @@
local M = {
get_git_dir = function ()
end,
get_selection_numbers = function ()
local M = {}
-- the missing split lua method to split a string
function M.split(string, char)
local array = {}
local reg = string.format("([^%s]+)", char)
for mem in string.gmatch(string, reg) do
table.insert(array, mem)
end
}
return array
end
-- returns the username and the reponame form the origin url in a table
function M.get_username_reponame(url)
-- ssh has an @ in the url
if string.find(url, "@") then
local splitted_user_repo = M.split(url, ":")[2]
local splitted_username_and_reponame = M.split(splitted_user_repo, "/")
local username_and_reponame = {
username = splitted_username_and_reponame[1],
reponame = M.split(splitted_username_and_reponame[2], ".")[1],
}
return username_and_reponame
else
local splitted_username_and_reponame = M.split(url, "/")
local username_and_reponame = {
username = splitted_username_and_reponame[3],
reponame = M.split(splitted_username_and_reponame[4], ".")[1],
}
return username_and_reponame
end
end
-- opens a url in the correct OS
function M.open_url(url)
local os = M.get_os()
if os == "Darwin" then
io.popen("open " .. url)
return true
end
if os == "Windows" then
io.popen("start " .. url)
return true
end
if os == "Linux" then
io.popen("xdg-open " .. url)
return true
end
return false
end
function M.get_os()
local os_name = vim.loop.os_uname().sysname
return os_name
end
function M.print_no_remote_message()
print("There is no git origin in this repo!")
end
return M

View file

@ -3,8 +3,6 @@ if vim.g.openingh then
end
vim.g.openingh = true
require("openingh").setup()
vim.api.nvim_create_user_command("OpenInGHFile", function()
require("openingh").openFile()
end, {})