From 74bac7d2872ba71dc39a2ff284ef51440a87f260 Mon Sep 17 00:00:00 2001 From: komal devnani Date: Sun, 23 Aug 2020 04:02:59 +0530 Subject: [PATCH 1/3] Github code opener from cli --- Basic-Scripts/github_opener.py | 37 ++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Basic-Scripts/github_opener.py diff --git a/Basic-Scripts/github_opener.py b/Basic-Scripts/github_opener.py new file mode 100644 index 00000000..b72e574f --- /dev/null +++ b/Basic-Scripts/github_opener.py @@ -0,0 +1,37 @@ +import os +import sys +import pyperclip + +# Install pyperclip module. Linux OS : sudo apt install python3-pyperclip +# otherwise : pip3 install pyperclip + +def github_opener (filename, line_no): + """ + github_opener( filename, line_no ) : + Generates a perma link to the code inside your Github repo which is remote for your current git project. + And copies it to clipboard. + + Parameters: + github_opener ( filename, line_no ) : takes two arguments. + Filename in which the code is and line no which you want to highlight. + eg. github_opener("hello.py", 8) + + Returns: + Perma link copied to clipboard + + """ + + stream = os.popen('git remote -v | grep "origin"') + cmd_output = stream.readline() #cmd_otput => origin git@github:username/proj/name.git (fetch) + info = cmd_output.split(":")[1].split(".")[0] #info => username/proj_name + + curr_dir = os.getcwd() + + proj_name = info.split("/")[1] #proj_name + index = curr_dir.find(proj_name) #start index of proj name in current path + perma_link = "https://github.com/" + info + "/blob/master" + curr_dir[(index+len(proj_name)):] + "/" + filename + "/#L" + line_no + pyperclip.copy(perma_link) + + +if __name__ == "__main__": + github_opener(sys.argv[1], sys.argv[2]) \ No newline at end of file From 149b024dd9d641002b441483468f5bd8812365d6 Mon Sep 17 00:00:00 2001 From: komal devnani Date: Mon, 24 Aug 2020 19:17:11 +0530 Subject: [PATCH 2/3] Improve documentation --- Basic-Scripts/github_opener.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/Basic-Scripts/github_opener.py b/Basic-Scripts/github_opener.py index b72e574f..186dfb15 100644 --- a/Basic-Scripts/github_opener.py +++ b/Basic-Scripts/github_opener.py @@ -22,7 +22,7 @@ def github_opener (filename, line_no): """ stream = os.popen('git remote -v | grep "origin"') - cmd_output = stream.readline() #cmd_otput => origin git@github:username/proj/name.git (fetch) + cmd_output = stream.readline() #cmd_output => origin git@github:username/proj/name.git (fetch) info = cmd_output.split(":")[1].split(".")[0] #info => username/proj_name curr_dir = os.getcwd() @@ -31,7 +31,17 @@ def github_opener (filename, line_no): index = curr_dir.find(proj_name) #start index of proj name in current path perma_link = "https://github.com/" + info + "/blob/master" + curr_dir[(index+len(proj_name)):] + "/" + filename + "/#L" + line_no pyperclip.copy(perma_link) + print("Link copied!") - if __name__ == "__main__": + """ + It takes two command line arguments. + Name of the file (Not complete path with directory) in which the desired line of code is present and + the line number to high light. + + In terminal, navigate to the directory in which the file is present. + + Run command : github_opener.py filename.xyz 23 + + """ github_opener(sys.argv[1], sys.argv[2]) \ No newline at end of file From 22aa4ad7cc3a3f14f6260168444cff70e3afd3f4 Mon Sep 17 00:00:00 2001 From: komal devnani Date: Mon, 24 Aug 2020 19:28:28 +0530 Subject: [PATCH 3/3] Improve documentation --- Basic-Scripts/github_opener.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Basic-Scripts/github_opener.py b/Basic-Scripts/github_opener.py index 186dfb15..bd61a6f9 100644 --- a/Basic-Scripts/github_opener.py +++ b/Basic-Scripts/github_opener.py @@ -38,10 +38,16 @@ def github_opener (filename, line_no): It takes two command line arguments. Name of the file (Not complete path with directory) in which the desired line of code is present and the line number to high light. + Eg. + python github_opener.py mode.py 9 + output: + Link copied! + (Link => https://github.com/username/proj_name/blob/master/directory/mode.py/#L9) + In terminal, navigate to the directory in which the file is present. - Run command : github_opener.py filename.xyz 23 + Run command : python github_opener.py filename.xyz 23 """ github_opener(sys.argv[1], sys.argv[2]) \ No newline at end of file