Skip to content

Commit 7c4e2d2

Browse files
committed
fix(init): Improve init command UX
The instructions for the "zmk init" command sometimes led to confusion because the first thing it asks you for is a repo URL, but new users need to skip past that prompt to get instructions for creating a new repo. To improve this, we now start with a prompt to ask the user whether they want to create a new repo or clone an existing one. Additionally, opening the page to create a repo from a template in a web browser may fail if the webbrowser module can't identify any installed browsers. If that happens, we now print the link so the user can manually open the page.
1 parent 2852318 commit 7c4e2d2

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ cd ~/Documents
121121
zmk init
122122
```
123123

124-
Follow the instructions it gives you. If you already have a ZMK config repo, you can enter its URL when prompted, for example:
124+
Follow the instructions it gives you. If you already have a ZMK config repo, select "Clone an existing repo" and enter the repo's URL, for example:
125125

126126
```
127127
Repository URL: https://github.com/myusername/zmk-config
128128
```
129129

130-
Otherwise, leave this first prompt blank and press <kbd>Enter</kbd>, and it will walk you through creating a new repo.
130+
Otherwise, select "Create a new ZMK config repo" and it will walk you through creating a new repo.
131131

132132
Once you finish following all the instructions, you will have a copy of the repo stored on your computer. All `zmk` commands will run on this repo (unless the working directory is inside a different repo). If you ever forget where the repo is located, you can run `zmk cd` to find it.
133133

zmk/commands/init.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from ..build import BuildMatrix
2020
from ..config import Config, get_config, set_context_repo
2121
from ..exceptions import FatalError
22+
from ..menu import show_menu
2223
from ..prompt import UrlPrompt
2324
from ..repo import Repo, find_containing_repo, is_repo
2425
from .keyboard.add import keyboard_add
@@ -139,41 +140,51 @@ def _check_for_existing_repo(cfg: Config):
139140

140141
if cfg.home_path and is_repo(cfg.home_path):
141142
rich.print(f'You already have a ZMK config repo at "{cfg.home_path}".')
142-
if not Confirm.ask("Create a new repo?", default=False):
143+
if not Confirm.ask("Initialize a new repo?", default=False):
143144
raise typer.Exit()
144145

146+
rich.print()
145147

146-
def _get_repo_url():
147-
rich.print(
148-
"If you already have a ZMK config repo, enter its URL here. "
149-
"Otherwise, leave this blank to create a new repo."
150-
)
151148

152-
url = Prompt.ask("Repository URL")
153-
if url:
154-
return url
149+
_CREATE_NEW_REPO = "Create a new ZMK config repo"
150+
_CLONE_REPO = "Clone an existing repo"
151+
155152

153+
def _get_repo_url():
156154
console = rich.get_console()
155+
156+
response = show_menu(title=None, items=[_CREATE_NEW_REPO, _CLONE_REPO])
157+
158+
if response == _CLONE_REPO:
159+
console.print("Enter the URL of your ZMK config repo.")
160+
return UrlPrompt.ask("Repository URL")
161+
157162
console.print(
158-
"\n"
159-
"To create your ZMK config repo, we will open a GitHub page in your "
163+
"To create your ZMK config repo, we will open a GitHub page in your web "
160164
"browser to create a repo from a template. Log in to GitHub if necessary, "
161-
"then click the green [green]Create repository[/green] button. "
165+
"then click the green [green]Create repository[/green] button."
162166
"\n\n"
163167
"Once it finishes creating the repo, Click the green [green]<> Code[/green] "
164168
"button, then copy the HTTPS URL and paste it here. "
165169
"Press [green]Enter[/green] to when you're ready.",
166170
width=TEXT_WIDTH,
167171
)
168172
input()
169-
webbrowser.open(TEMPLATE_URL)
170173

171-
url = UrlPrompt.ask("Repository URL")
172-
return url
174+
if not webbrowser.open(TEMPLATE_URL):
175+
console.print(
176+
"[yellow]Failed to open a browser. Please open the following link in a web browser:"
177+
)
178+
console.print("")
179+
console.print(TEMPLATE_URL)
180+
console.print("")
181+
182+
return UrlPrompt.ask("Repository URL")
173183

174184

175185
def _get_directory_name(url: str):
176-
default = urlparse(url).path.split("/")[-1]
186+
# Default name is the last path component of the URL without any ".git" suffix
187+
default = urlparse(url).path.split("/")[-1].removesuffix(".git")
177188

178189
return Prompt.ask("Enter a directory name", default=default)
179190

0 commit comments

Comments
 (0)