diff --git a/pyn/commands.py b/pyn/commands.py index 8b81938..eadfd50 100644 --- a/pyn/commands.py +++ b/pyn/commands.py @@ -29,6 +29,10 @@ def cmd_add_note(config, title, notebook): if not body: notes.edit_file(config, filename) + notes.run_git( + config, ["add", str(filename.relative_to(config.directory))], quiet=True + ) + add_note = group.add_parser("add", aliases=["a"]) add_note.add_argument("--notebook", "-n", help="add to this notebook [general]") @@ -79,6 +83,9 @@ list_notes.set_defaults(func=cmd_list_notes) def cmd_edit_note(config, title, notebook: str) -> None: filename = notes.search_filenames(config, title, notebook) notes.edit_file(config, filename) + notes.run_git( + config, ["add", str(filename.relative_to(config.directory))], quiet=True + ) edit_note = group.add_parser("edit", aliases=["e"]) @@ -98,3 +105,28 @@ show_note = group.add_parser("show", aliases=["s", "cat"]) show_note.add_argument("title", help="note title", nargs="?") show_note.add_argument("--notebook", "-n", help="limit to this notebook") show_note.set_defaults(func=cmd_show_note) + +### Git + + +def cmd_git(config: Config, cmd: list[str]) -> None: + output = notes.run_git(config, cmd) + sys.stdout.write(output) + + +git = group.add_parser("git", help="run a git command in the note directory") +git.add_argument("cmd", help="git command", nargs="*") +git.set_defaults(func=cmd_git) + +### Sync + + +def cmd_sync(config: Config) -> None: + output = notes.run_git(config, ["pull"]) + sys.stdout.write(f"Pull: {output}\n") + output = notes.run_git(config, ["push"]) + sys.stdout.write(f"Push: {output}\n") + + +git = group.add_parser("sync", help="synchronise the notes") +git.set_defaults(func=cmd_sync) diff --git a/pyn/notes.py b/pyn/notes.py index 45c7cfc..4e95bd9 100644 --- a/pyn/notes.py +++ b/pyn/notes.py @@ -3,6 +3,7 @@ import pathlib import sys from pyn.config import Config +from pyn.common import logger from datetime import datetime from shutil import copyfileobj @@ -123,6 +124,7 @@ def search_contents( if full_line: cmd.append("-x") + logger.debug("Executing: %r", cmd) try: output = subprocess.check_output(cmd) for line in output.decode("utf-8").strip().split("\n"): @@ -132,3 +134,17 @@ def search_contents( if e.returncode == 1: return raise + + +def run_git(config: Config, cmd: list[str], quiet: bool = False) -> str: + if not (config.directory / ".git").is_dir(): + if quiet: + return "" + raise RuntimeError("Notes not currently tracked") + + cmd = ["git", "-C", str(config.directory)] + cmd + try: + output = subprocess.check_output(cmd, stderr=subprocess.STDOUT) + return output.decode("utf-8").strip() + except subprocess.CalledProcessError as e: + raise RuntimeError(str(e)) diff --git a/pyn/todo.py b/pyn/todo.py index 25b2a8e..c595cca 100644 --- a/pyn/todo.py +++ b/pyn/todo.py @@ -35,10 +35,10 @@ def cmd_list_pending(config: Config, notebook: str): entry = notes.file_entry(config, result.filename) if entry.notebook != nb: nb = entry.notebook - print(f"\nNotebook: {entry.notebook}") + print(f"\nNotebook: \033[32m{entry.notebook}\033[0m") name = result.filename.name sys.stdout.write(result.snippet) - sys.stdout.write(" " * (cols - len(result.snippet) - len(name))) + sys.stdout.write(" " * (cols - len(result.snippet) - len(name) - 1)) sys.stdout.write(name) sys.stdout.write("\n") @@ -62,7 +62,7 @@ def cmd_mark_complete( item_str = " ".join(item) results = list( notes.search_contents( - config, f"* [{unchecked}] {item}", notebook, full_line=True + config, f"* [{unchecked}] {item_str}", notebook, full_line=True ) )