-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-project-linker.py
More file actions
150 lines (123 loc) · 4.44 KB
/
Copy pathgithub-project-linker.py
File metadata and controls
150 lines (123 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os
import sys
import json
import re
import msvcrt # Windows-only: lets us detect single keypresses like Ctrl+N
# Enable ANSI escape codes on Windows terminals (needed for the dimmed text)
os.system("")
DIM = "\033[2m"
RESET = "\033[0m"
# USERNAME:kuuh4
# ^ Rewritten automatically by the script — no need to edit by hand.
def get_own_path():
return os.path.abspath(__file__)
def load_username_from_self():
with open(get_own_path(), "r", encoding="utf-8") as f:
content = f.read()
match = re.search(r"^# USERNAME:(.*)$", content, re.MULTILINE)
if match:
value = match.group(1).strip()
return value if value else None
return None
def save_username_to_self(new_username):
path = get_own_path()
with open(path, "r", encoding="utf-8") as f:
content = f.read()
new_content = re.sub(
r"^# USERNAME:.*$",
f"# USERNAME:{new_username}",
content,
count=1,
flags=re.MULTILINE
)
with open(path, "w", encoding="utf-8") as f:
f.write(new_content)
def prompt_new_username():
while True:
new_username = input("Enter your new GitHub username: ").strip()
if new_username:
os.system('cls')
return new_username
print("Username cannot be empty.")
def input_watching_ctrl_n(prompt):
"""
Custom input reader: behaves like input(), but returns None
immediately if the user presses Ctrl+N instead of typing normally.
"""
print(prompt, end="", flush=True)
buffer = ""
while True:
ch = msvcrt.getch()
if ch == b"\r": # Enter
print()
return buffer
elif ch == b"\x0e": # Ctrl+N
print()
return None
elif ch == b"\x03": # Ctrl+C
raise KeyboardInterrupt
elif ch == b"\x08": # Backspace
if buffer:
buffer = buffer[:-1]
print("\b \b", end="", flush=True)
else:
try:
char = ch.decode("utf-8")
buffer += char
print(char, end="", flush=True)
except UnicodeDecodeError:
pass # ignore stray/unsupported bytes (e.g. arrow keys)
def print_header(username):
print("github shortcut creator")
print(f"username: {username} {DIM}(ctrl+n to change){RESET}")
def get_repo_name(username):
while True:
print_header(username)
result = input_watching_ctrl_n("repo name: ")
if result is None:
new_username = prompt_new_username()
save_username_to_self(new_username)
username = new_username
print() # small gap before header redraws
continue
repo_name = result.strip()
if repo_name:
return username, repo_name
print("Repository name cannot be empty.\n")
def create_shortcuts():
script_dir = os.path.dirname(get_own_path())
username = load_username_from_self()
if username is None:
print("No username found. Let's set one up.")
username = prompt_new_username()
save_username_to_self(username)
username, repo_name = get_repo_name(username)
safe_filename = "".join(c for c in repo_name if c.isalnum() or c in (' ', '_', '-', '.')).rstrip()
browser_url = f"https://github.com/{username}/{repo_name}"
browser_shortcut_content = f"[InternetShortcut]\nURL={browser_url}\n"
workspace_data = {
"folders": [
{
"name": f"{repo_name} (Remote)",
"uri": f"vscode-vfs://github/{username}/{repo_name}"
}
],
"settings": {}
}
workspace_file_path = os.path.join(script_dir, f"_Open {safe_filename} in VS Code.code-workspace")
browser_file_path = os.path.join(script_dir, f"_Open {safe_filename} on GitHub.url")
try:
with open(workspace_file_path, "w", encoding="utf-8") as f:
json.dump(workspace_data, f, indent=4)
with open(browser_file_path, "w", encoding="utf-8") as f:
f.write(browser_shortcut_content)
print("\n"+"=-"*15+"=")
print("shortcuts created successfully")
print(f"{DIM}saved directly to: {script_dir}{RESET}")
print("=-"*15+"=")
input("\nPress Enter to close this window...")
except Exception as e:
print(f"An error occurred while writing files: {e}")
input("\nPress Enter to close...")
if __name__ == "__main__":
create_shortcuts()