77 lines
2.3 KiB
Python
77 lines
2.3 KiB
Python
import glob
|
|
import os
|
|
import typer
|
|
import click
|
|
from rich import print
|
|
from rich.markdown import Markdown
|
|
from rich.panel import Panel
|
|
from shutil import move
|
|
|
|
from datetime import datetime
|
|
|
|
from streamer.parse.parse import parse_markdown_file
|
|
from streamer.query.task import find_by_markers
|
|
from streamer.settings import Settings
|
|
|
|
app = typer.Typer()
|
|
|
|
|
|
@app.command()
|
|
def todo() -> None:
|
|
for file_name in glob.glob(f"{glob.escape(Settings().base_folder)}/*.md"):
|
|
with open(file_name, "r") as file:
|
|
file_content = file.read()
|
|
sharded_document = parse_markdown_file(file_name, file_content)
|
|
if sharded_document.shard:
|
|
open_tasks = find_by_markers(sharded_document.shard, ["Task"], ["Done"])
|
|
|
|
for task_shard in open_tasks:
|
|
print(
|
|
Panel(
|
|
Markdown(
|
|
"\n".join(
|
|
file_content.splitlines()[
|
|
task_shard.start_line - 1 : task_shard.end_line
|
|
]
|
|
)
|
|
),
|
|
title=f"{file_name}:{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 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()
|