From f94fc6165ee09b970f6596f0d937c22555971f31 Mon Sep 17 00:00:00 2001 From: Zach Berwaldt Date: Thu, 20 Feb 2025 18:12:10 -0500 Subject: Move create note logic into command base. --- note_tools/__init__.py | 23 ++++++++++++++++++++++- note_tools/today.py | 33 ++++++++------------------------- note_tools/tomorrow.py | 24 +++++++----------------- 3 files changed, 37 insertions(+), 43 deletions(-) diff --git a/note_tools/__init__.py b/note_tools/__init__.py index 8d6774d..0b4fcbd 100644 --- a/note_tools/__init__.py +++ b/note_tools/__init__.py @@ -3,7 +3,7 @@ import os from typing import Optional, Tuple class NoteBaseCommand(object): - date = None + date = datetime.now() def file_name(self): return f"{self.date.strftime('%Y%m%d')}.md" @@ -35,3 +35,24 @@ class NoteBaseCommand(object): if os.path.basename(folder_path).lower() == "notes": return folder_path return None + + def create_note(self, project_data, callback): + # Get the current date formatted as YYYYMMDD and YYYY-MM-DD + notes_path = self.get_notes_path(project_data) + + if notes_path: + year, month = self.get_date_parts() + directory = os.path.join(notes_path, year, month) + os.makedirs(directory, exist_ok=True) + file_path = os.path.join(directory, self.file_name()) + + # Check if file already exists + if os.path.exists(file_path): + callback(False, f"The file '{self.file_name()}' already exists.") + else: + self.write_file_heading(file_path) + callback(True, None, file_path) + + else: + callback(False, "No 'Notes' directory found in the project. Please add a 'Notes' folder.") + diff --git a/note_tools/today.py b/note_tools/today.py index 20f2e53..9afad5d 100644 --- a/note_tools/today.py +++ b/note_tools/today.py @@ -3,32 +3,20 @@ import os import sublime import sublime_plugin from pathlib import Path -from typing import List, Optional +from typing import 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()) + project_data = 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.") + def open_file(success: bool, message: str, file_path: Optional[str]): + if success: + new_view = self.window.open_file(file_path) + self.set_cursor_position(new_view) else: - self.write_file_heading(file_path) - new_view = self.window.open_file(file_path) - self.set_cursor_position(new_view) + sublime.message_dialog(message) - else: - sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.") + self.create_note(project_data=project_data, callback=open_file) def set_cursor_position(self, view): # Set the cursor position two lines beneath the underline @@ -42,8 +30,3 @@ class NewNoteCommand(NoteBaseCommand, sublime_plugin.WindowCommand): 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] diff --git a/note_tools/tomorrow.py b/note_tools/tomorrow.py index 2198a3b..21b1fd5 100644 --- a/note_tools/tomorrow.py +++ b/note_tools/tomorrow.py @@ -3,25 +3,15 @@ import os from datetime import timedelta import sublime import sublime_plugin +from typing import Optional class NewNoteForTomorrowCommand(NoteBaseCommand, sublime_plugin.WindowCommand): def run(self): - self.date = self.get_date() + timedelta(days=1) + self.date += timedelta(days=1) + project_data = self.window.project_data() - 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) - os.makedirs(directory, exist_ok=True) - file_path = os.path.join(directory, self.file_name()) - - # 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) - - else: - sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.") + def callback(success: bool, message: Optional[str], file_path: Optional[str]): + if not success: + sublime.message_dialog(message) + self.create_note(project_data=project_data, callback=callback) -- cgit v1.1