From 095ff55a15251b4db7f97e9a0d9d4bce20001c30 Mon Sep 17 00:00:00 2001 From: Ali Almohaya Date: Mon, 26 Sep 2022 02:09:22 +0300 Subject: [PATCH] chore: implement OpenInGhRepo with utils --- lua/openingh/init.lua | 58 +++++++++++++++++++++++++---------- lua/openingh/utils.lua | 69 +++++++++++++++++++++++++++++++++++++++--- plugin/openingh.lua | 2 -- 3 files changed, 106 insertions(+), 23 deletions(-) diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index 7ab538c..9e03ecc 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -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 diff --git a/lua/openingh/utils.lua b/lua/openingh/utils.lua index 3a2036d..e79cab9 100644 --- a/lua/openingh/utils.lua +++ b/lua/openingh/utils.lua @@ -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 diff --git a/plugin/openingh.lua b/plugin/openingh.lua index 63a4470..354f9b7 100644 --- a/plugin/openingh.lua +++ b/plugin/openingh.lua @@ -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, {})