diff options
| -rw-r--r-- | note_tools/__init__.py | 23 | ||||
| -rw-r--r-- | note_tools/today.py | 33 | ||||
| -rw-r--r-- | 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 | |||
| 3 | from typing import Optional, Tuple | 3 | from typing import Optional, Tuple |
| 4 | 4 | ||
| 5 | class NoteBaseCommand(object): | 5 | class NoteBaseCommand(object): |
| 6 | date = None | 6 | date = datetime.now() |
| 7 | 7 | ||
| 8 | def file_name(self): | 8 | def file_name(self): |
| 9 | return f"{self.date.strftime('%Y%m%d')}.md" | 9 | return f"{self.date.strftime('%Y%m%d')}.md" |
| @@ -35,3 +35,24 @@ class NoteBaseCommand(object): | |||
| 35 | if os.path.basename(folder_path).lower() == "notes": | 35 | if os.path.basename(folder_path).lower() == "notes": |
| 36 | return folder_path | 36 | return folder_path |
| 37 | return None | 37 | return None |
| 38 | |||
| 39 | def create_note(self, project_data, callback): | ||
| 40 | # Get the current date formatted as YYYYMMDD and YYYY-MM-DD | ||
| 41 | notes_path = self.get_notes_path(project_data) | ||
| 42 | |||
| 43 | if notes_path: | ||
| 44 | year, month = self.get_date_parts() | ||
| 45 | directory = os.path.join(notes_path, year, month) | ||
| 46 | os.makedirs(directory, exist_ok=True) | ||
| 47 | file_path = os.path.join(directory, self.file_name()) | ||
| 48 | |||
| 49 | # Check if file already exists | ||
| 50 | if os.path.exists(file_path): | ||
| 51 | callback(False, f"The file '{self.file_name()}' already exists.") | ||
| 52 | else: | ||
| 53 | self.write_file_heading(file_path) | ||
| 54 | callback(True, None, file_path) | ||
| 55 | |||
| 56 | else: | ||
| 57 | callback(False, "No 'Notes' directory found in the project. Please add a 'Notes' folder.") | ||
| 58 | |||
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 | |||
| 3 | import sublime | 3 | import sublime |
| 4 | import sublime_plugin | 4 | import sublime_plugin |
| 5 | from pathlib import Path | 5 | from pathlib import Path |
| 6 | from typing import List, Optional | 6 | from typing import Optional |
| 7 | 7 | ||
| 8 | class NewNoteCommand(NoteBaseCommand, sublime_plugin.WindowCommand): | 8 | class NewNoteCommand(NoteBaseCommand, sublime_plugin.WindowCommand): |
| 9 | def run(self): | 9 | def run(self): |
| 10 | # Get the current date formatted as YYYYMMDD and YYYY-MM-DD | 10 | project_data = self.window.project_data() |
| 11 | self.date = self.get_date() | ||
| 12 | notes_path = self.get_notes_path(self.window.project_data()) | ||
| 13 | 11 | ||
| 14 | if notes_path: | 12 | def open_file(success: bool, message: str, file_path: Optional[str]): |
| 15 | year, month = self.get_date_parts() | 13 | if success: |
| 16 | directory = os.path.join(notes_path, year, month) | 14 | new_view = self.window.open_file(file_path) |
| 17 | print("directory: ", directory) | 15 | self.set_cursor_position(new_view) |
| 18 | os.makedirs(directory, exist_ok=True) | ||
| 19 | file_path = os.path.join(directory, self.file_name()) | ||
| 20 | print('path: ', file_path) | ||
| 21 | |||
| 22 | # Check if file already exists | ||
| 23 | if os.path.exists(file_path): | ||
| 24 | sublime.message_dialog(f"The file '{self.file_name()}' already exists.") | ||
| 25 | else: | 16 | else: |
| 26 | self.write_file_heading(file_path) | 17 | sublime.message_dialog(message) |
| 27 | new_view = self.window.open_file(file_path) | ||
| 28 | self.set_cursor_position(new_view) | ||
| 29 | 18 | ||
| 30 | else: | 19 | self.create_note(project_data=project_data, callback=open_file) |
| 31 | sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.") | ||
| 32 | 20 | ||
| 33 | def set_cursor_position(self, view): | 21 | def set_cursor_position(self, view): |
| 34 | # Set the cursor position two lines beneath the underline | 22 | # Set the cursor position two lines beneath the underline |
| @@ -42,8 +30,3 @@ class NewNoteCommand(NoteBaseCommand, sublime_plugin.WindowCommand): | |||
| 42 | sublime.set_timeout_async(lambda: self.set_cursor_position(view), 100) | 30 | sublime.set_timeout_async(lambda: self.set_cursor_position(view), 100) |
| 43 | else: | 31 | else: |
| 44 | on_load() | 32 | on_load() |
| 45 | |||
| 46 | def list_files(self, directory: str, ignore_extensions: Optional[List[str]] = None) -> List[Path]: | ||
| 47 | if ignore_extensions is None: | ||
| 48 | ignore_extensions = [] | ||
| 49 | 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 | |||
| 3 | from datetime import timedelta | 3 | from datetime import timedelta |
| 4 | import sublime | 4 | import sublime |
| 5 | import sublime_plugin | 5 | import sublime_plugin |
| 6 | from typing import Optional | ||
| 6 | 7 | ||
| 7 | class NewNoteForTomorrowCommand(NoteBaseCommand, sublime_plugin.WindowCommand): | 8 | class NewNoteForTomorrowCommand(NoteBaseCommand, sublime_plugin.WindowCommand): |
| 8 | def run(self): | 9 | def run(self): |
| 9 | self.date = self.get_date() + timedelta(days=1) | 10 | self.date += timedelta(days=1) |
| 11 | project_data = self.window.project_data() | ||
| 10 | 12 | ||
| 11 | notes_path = self.get_notes_path(self.window.project_data()) | 13 | def callback(success: bool, message: Optional[str], file_path: Optional[str]): |
| 12 | 14 | if not success: | |
| 13 | if notes_path: | 15 | sublime.message_dialog(message) |
| 14 | year, month = self.get_date_parts() | ||
| 15 | directory = os.path.join(notes_path, year, month) | ||
| 16 | os.makedirs(directory, exist_ok=True) | ||
| 17 | file_path = os.path.join(directory, self.file_name()) | ||
| 18 | |||
| 19 | # Check if file already exists | ||
| 20 | if os.path.exists(file_path): | ||
| 21 | sublime.message_dialog(f"The file '{self.file_name()}' already exists.") | ||
| 22 | else: | ||
| 23 | self.write_file_heading(file_path) | ||
| 24 | |||
| 25 | else: | ||
| 26 | sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.") | ||
| 27 | 16 | ||
| 17 | self.create_note(project_data=project_data, callback=callback) | ||
