From 9cde3624920cb1ddd563b8addfcc65ccc79e080a Mon Sep 17 00:00:00 2001 From: Zach Berwaldt Date: Wed, 19 Feb 2025 20:35:50 -0500 Subject: Finish refactor --- note_tools/today.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 note_tools/today.py (limited to 'note_tools/today.py') diff --git a/note_tools/today.py b/note_tools/today.py new file mode 100644 index 0000000..20f2e53 --- /dev/null +++ b/note_tools/today.py @@ -0,0 +1,49 @@ +from . import NoteBaseCommand +import os +import sublime +import sublime_plugin +from pathlib import Path +from typing import List, Optional + +class NewNoteCommand(NoteBaseCommand, sublime_plugin.WindowCommand): + def run(self): + # Get the current date formatted as YYYYMMDD and YYYY-MM-DD + self.date = self.get_date() + notes_path = self.get_notes_path(self.window.project_data()) + + if notes_path: + year, month = self.get_date_parts() + directory = os.path.join(notes_path, year, month) + print("directory: ", directory) + os.makedirs(directory, exist_ok=True) + file_path = os.path.join(directory, self.file_name()) + print('path: ', file_path) + + # Check if file already exists + if os.path.exists(file_path): + sublime.message_dialog(f"The file '{self.file_name()}' already exists.") + else: + self.write_file_heading(file_path) + new_view = self.window.open_file(file_path) + self.set_cursor_position(new_view) + + else: + sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.") + + def set_cursor_position(self, view): + # Set the cursor position two lines beneath the underline + def on_load(): + view.run_command("move_to", {"to": "bof"}) + for _ in range(3): + view.run_command("move", {"by": "lines", "forward": True}) + + # Add an event listener for when the file is loaded + if view.is_loading(): + sublime.set_timeout_async(lambda: self.set_cursor_position(view), 100) + else: + on_load() + + def list_files(self, directory: str, ignore_extensions: Optional[List[str]] = None) -> List[Path]: + if ignore_extensions is None: + ignore_extensions = [] + return [file for file in Path(directory).glob("*") if file.is_file() and file.suffix not in ignore_extensions] -- cgit v1.1