Skip to content

Commit 8c7035a

Browse files
authored
CLI improvements (#20)
1 parent ffa2676 commit 8c7035a

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

src/render_engine_cli/cli.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
get_editor,
1919
get_site,
2020
get_site_content_paths,
21+
handle_content_file,
2122
remove_output_folder,
2223
split_args,
2324
split_module_site,
@@ -207,9 +208,11 @@ def serve(module_site: str, clean: bool, reload: bool, port: int):
207208
)
208209
@click.option(
209210
"--content-file",
210-
type=click.File("r"),
211+
type=click.STRING,
212+
callback=handle_content_file,
211213
default=None,
212-
help="Path to a file containing the desired content. Either this or `--content` may be provided but not both",
214+
help="Path to a file containing the desired content. Using 'stdin' will ask you to enter the content in "
215+
"the terminal. Either this or `--content` may be provided but not both",
213216
)
214217
@click.option(
215218
"-t",
@@ -259,7 +262,7 @@ def new_entry(
259262
module_site: str,
260263
collection: str,
261264
content: str,
262-
content_file: click.File,
265+
content_file: str,
263266
title: str,
264267
slug: str,
265268
include_date: bool,
@@ -308,7 +311,7 @@ def new_entry(
308311
if content and content_file:
309312
raise TypeError("Both content and content_file provided. At most one may be provided.")
310313
if content_file:
311-
content = content_file.read()
314+
content = content_file
312315
entry = create_collection_entry(content=content or "", collection=_collection, **parsed_args)
313316
if title:
314317
# If we had a title earlier this is where we replace the default that is added by the template handler with

src/render_engine_cli/event.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,12 @@ def watch(self) -> None:
124124

125125
console.print(f"[yellow]Serving {self.output_path}[/yellow]")
126126
while not self.stop_watcher():
127-
if self.dirs_to_watch:
128-
for _ in watchfiles.watch(*self.dirs_to_watch):
129-
self.rebuild()
127+
try:
128+
if self.dirs_to_watch:
129+
for _ in watchfiles.watch(*self.dirs_to_watch):
130+
self.rebuild()
131+
except KeyboardInterrupt:
132+
break
130133

131134
def __enter__(self):
132135
"""Starting Context manager for the class"""

src/render_engine_cli/utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,19 @@ def get_editor(ctx: click.Context, param: click.Option, value: str) -> str | Non
212212
return None
213213
case _:
214214
return value
215+
216+
217+
def handle_content_file(ctx: click.Context, param: click.Option, value: str) -> str | None:
218+
"""Handle the content file"""
219+
if value == "stdin":
220+
content = list()
221+
click.secho('Please enter the content. To finish, put a "." on a blank line.', fg="green")
222+
while (line := input("")) != ".":
223+
content.append(line)
224+
return "\n".join(content)
225+
path = Path(value)
226+
if not path.exists():
227+
raise click.exceptions.BadParameter(
228+
f'Either the path to a file or "stdin" must be provided. {repr(value)} is invalid.'
229+
)
230+
return path.read_text()

0 commit comments

Comments
 (0)