import glob import os from datetime import datetime from shutil import move from typing import Generator import click import typer from rich import print from rich.markdown import Markdown from rich.panel import Panel from streamer.parse.attach_markdown import StreamFileWithMarkdown, attach_markdown from streamer.parse.parse import parse_markdown_file from streamer.query.task import find_by_markers from streamer.settings import Settings app = typer.Typer() def all_files() -> Generator[StreamFileWithMarkdown]: for file_name in glob.glob(f"{glob.escape(Settings().base_folder)}/*.md"): with open(file_name, "r") as file: file_content = file.read() yield attach_markdown( parse_markdown_file(file_name, file_content), file_content ) @app.command() def todo() -> None: for sharded_document in all_files(): if sharded_document.shard: open_tasks = find_by_markers(sharded_document.shard, ["Task"], ["Done"]) for task_shard in open_tasks: print( Panel( Markdown(task_shard.markdown_content), title=f"{sharded_document.filename}:{task_shard.start_line}", ) ) @app.command() def new() -> None: streamer_directory = Settings().base_folder timestamp = datetime.now().strftime("%Y%m%d-%H%M%S") preliminary_file_name = f"{timestamp}_wip.md" prelimary_path = os.path.join(streamer_directory, preliminary_file_name) content = "# " with open(prelimary_path, "w") as file: _ = file.write(content) click.edit(None, filename=prelimary_path) with open(prelimary_path, "r") as file: content = file.read() parsed_content = parse_markdown_file(prelimary_path, content) final_file_name = f"{timestamp}.md" if parsed_content.shard is not None and len( markers := parsed_content.shard.markers ): final_file_name = f"{timestamp} {' '.join(markers)}.md" final_path = os.path.join(streamer_directory, final_file_name) _ = move(prelimary_path, final_path) print(f"Saved as [yellow]{final_file_name}") @app.callback(invoke_without_command=True) def main(ctx: typer.Context): if ctx.invoked_subcommand is None: new() if __name__ == "__main__": app()