Skip to content

Commit

Permalink
Merge branch 'topic/default/editor' into 'branch/default'
Browse files Browse the repository at this point in the history
Better default editor

See merge request fluiddyn/hg-setup!6
  • Loading branch information
paugier committed Dec 20, 2024
2 parents 219f160 + c779ddf commit 4d53535
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
5 changes: 4 additions & 1 deletion src/hg_setup/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ def init(name, email, auto, force):
init_shell_completions()
exists, path_config = check_hg_conf_file()
if exists and not force:
click.echo(f"File {path_config} already exists. Nothing to do.")
click.echo(
f"File {path_config} already exists. Nothing to do.\n"
"Run `hg-setup init -f` to launch the user interface."
)
return

if auto:
Expand Down
20 changes: 14 additions & 6 deletions src/hg_setup/init_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os

from datetime import datetime
from shutil import which

from textual.app import App, ComposeResult, Screen
from textual import on, work
Expand All @@ -24,10 +25,17 @@

from .hgrcs import HgrcCodeMaker, check_hg_conf_file, name_default

editors_possible = ["nano", "notepad", "emacs", "vim", "vi"]
editors_avail = [editor for editor in editors_possible if which(editor) is not None]
try:
editor_default = editors_avail[0]
except IndexError:
editor_default = ""

inputs = {
"name": dict(placeholder="Firstname Lastname"),
"email": dict(placeholder="Email"),
"editor": dict(placeholder="nano", value="nano"),
"editor": dict(value=editor_default),
}

checkboxs = {
Expand Down Expand Up @@ -77,7 +85,10 @@ def compose(self) -> ComposeResult:
for key in ["name", "email"]:
yield self.inputs[key]

yield Label("[b]Your preferred editor?[/b]")
question_editor = "[b]Your preferred editor?[/b]"
if "emacs" in editors_avail:
question_editor += ' (could be "emacs -nw -Q")'
yield Label(question_editor)
yield self.inputs["editor"]

yield Label("[b]Get improvements to the UI over time?[/b] (recommended)")
Expand Down Expand Up @@ -205,17 +216,14 @@ def save_existing_file(path):
def init_auto(name, email, force, path_hgrc):
"""init without user interaction"""

# TODO: good default editor depending on what is available
editor = "nano"

if force:
save_existing_file(path_hgrc)

if path_hgrc.exists():
click.echo(f"{path_hgrc} already exists. Nothing to do.")
return

text = HgrcCodeMaker().make_text(name, email, editor)
text = HgrcCodeMaker().make_text(name, email, editor_default)
path_hgrc.write_text(text)

click.echo(f"configuration written in {path_hgrc}.")
2 changes: 1 addition & 1 deletion tests/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_auto_with_options(tmp_path):

result = runner.invoke(main, command, env=env)
assert result.exit_code == 0
assert result.output.endswith("already exists. Nothing to do.\n")
assert "already exists. Nothing to do.\n" in result.output

command = ["init", "-n", "toto", "-e", "toto.lastname@me", "--auto", "--force"]
result = runner.invoke(main, command, env=env)
Expand Down

0 comments on commit 4d53535

Please sign in to comment.