diff options
| author | Zach Berwaldt <zberwaldt@tutamail.com> | 2025-02-20 12:40:49 -0500 |
|---|---|---|
| committer | Zach Berwaldt <zberwaldt@tutamail.com> | 2025-02-20 12:40:49 -0500 |
| commit | 8978a195055844e22ef9ad736f1494c82a5fc235 (patch) | |
| tree | db033ca0f5e414faf9e3c1a7835c438f35a52780 | |
| parent | 116c309b2efc394123f2cc67625effc3b2426a22 (diff) | |
| parent | 590e84a29672f5ecf458d53aa1d4ca587f7c47a6 (diff) | |
Merge branch 'develop'
| -rw-r--r-- | Default (Windows).sublime-keymap | 4 | ||||
| -rw-r--r-- | Notes.sublime-commands | 10 | ||||
| -rw-r--r-- | Notes.sublime-menu | 20 | ||||
| -rw-r--r-- | main.py | 14 | ||||
| -rw-r--r-- | note_tools/__init__.py | 37 | ||||
| -rw-r--r-- | note_tools/commands.py | 56 | ||||
| -rw-r--r-- | note_tools/today.py | 49 | ||||
| -rw-r--r-- | note_tools/tomorrow.py | 27 |
8 files changed, 152 insertions, 65 deletions
diff --git a/Default (Windows).sublime-keymap b/Default (Windows).sublime-keymap index 58f6d3c..1f79167 100644 --- a/Default (Windows).sublime-keymap +++ b/Default (Windows).sublime-keymap | |||
| @@ -2,5 +2,9 @@ | |||
| 2 | { | 2 | { |
| 3 | "keys": ["alt+n", "n"], | 3 | "keys": ["alt+n", "n"], |
| 4 | "command": "new_note" | 4 | "command": "new_note" |
| 5 | }, | ||
| 6 | { | ||
| 7 | "keys": ["alt+n", "t"], | ||
| 8 | "command": "new_note_for_tomorrow" | ||
| 5 | } | 9 | } |
| 6 | ] | 10 | ] |
diff --git a/Notes.sublime-commands b/Notes.sublime-commands new file mode 100644 index 0000000..f554bc0 --- /dev/null +++ b/Notes.sublime-commands | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | [ | ||
| 2 | { | ||
| 3 | "caption": "Notes: New Note for Today", | ||
| 4 | "command": "new_note" | ||
| 5 | }, | ||
| 6 | { | ||
| 7 | "caption": "Notes: New Note for Tomorrow", | ||
| 8 | "command": "new_note_for_tomorrow" | ||
| 9 | } | ||
| 10 | ] \ No newline at end of file | ||
diff --git a/Notes.sublime-menu b/Notes.sublime-menu new file mode 100644 index 0000000..cb87c38 --- /dev/null +++ b/Notes.sublime-menu | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | [ | ||
| 2 | { | ||
| 3 | "id": "tools", | ||
| 4 | "children": [ | ||
| 5 | { | ||
| 6 | "caption": "Notes", | ||
| 7 | "children": [ | ||
| 8 | { | ||
| 9 | "caption": "New Note for Today", | ||
| 10 | "command": "new_note" | ||
| 11 | }, | ||
| 12 | { | ||
| 13 | "caption": "New Note for Tomorrow", | ||
| 14 | "command": "new_note_for_tomorrow" | ||
| 15 | } | ||
| 16 | ] | ||
| 17 | } | ||
| 18 | ] | ||
| 19 | } | ||
| 20 | ] \ No newline at end of file | ||
| @@ -1,11 +1,7 @@ | |||
| 1 | from .note_tools.commands import ( | 1 | from .note_tools.today import NewNoteCommand |
| 2 | OrganizeNotesCommand, | 2 | from .note_tools.tomorrow import NewNoteForTomorrowCommand |
| 3 | NewNoteCommand | ||
| 4 | ) | ||
| 5 | 3 | ||
| 6 | __all__= [ | 4 | __all__ = [ |
| 7 | "NewNoteCommand" | 5 | 'NewNoteCommand', |
| 6 | 'NewNoteForTomorrowCommand' | ||
| 8 | ] | 7 | ] |
| 9 | |||
| 10 | def plugin_loaded(): | ||
| 11 | print("Note tools have been loaded.") | ||
diff --git a/note_tools/__init__.py b/note_tools/__init__.py index e69de29..8d6774d 100644 --- a/note_tools/__init__.py +++ b/note_tools/__init__.py | |||
| @@ -0,0 +1,37 @@ | |||
| 1 | from datetime import datetime | ||
| 2 | import os | ||
| 3 | from typing import Optional, Tuple | ||
| 4 | |||
| 5 | class NoteBaseCommand(object): | ||
| 6 | date = None | ||
| 7 | |||
| 8 | def file_name(self): | ||
| 9 | return f"{self.date.strftime('%Y%m%d')}.md" | ||
| 10 | |||
| 11 | def get_formatted_dates(self) -> Tuple[str, str]: | ||
| 12 | return (self.date.strftime("%Y%m%d"), self.date.strftime("%Y-%m-%d")) | ||
| 13 | |||
| 14 | def get_date(self) -> datetime: | ||
| 15 | return datetime.now() | ||
| 16 | |||
| 17 | def get_date_parts(self) -> Tuple[str]: | ||
| 18 | return (self.date.strftime('%Y'), self.date.strftime('%m')) | ||
| 19 | |||
| 20 | def write_file_heading(self, file_path): | ||
| 21 | date_yyyymmdd = self.date.strftime('%Y-%m-%d') | ||
| 22 | underline = '=' * len(date_yyyymmdd) | ||
| 23 | with open(file_path, 'w') as file: | ||
| 24 | file.write(f"{date_yyyymmdd}\n{underline}\n\n\n") | ||
| 25 | |||
| 26 | def parse_file_date (self, file_name: str) -> Tuple[str, str]: | ||
| 27 | year = file_name[:4] | ||
| 28 | month = file_name[4:6] | ||
| 29 | return year, month | ||
| 30 | |||
| 31 | def get_notes_path(self, project_data) -> Optional[str]: | ||
| 32 | if project_data and 'folders' in project_data: | ||
| 33 | for folder in project_data['folders']: | ||
| 34 | folder_path = folder['path'] | ||
| 35 | if os.path.basename(folder_path).lower() == "notes": | ||
| 36 | return folder_path | ||
| 37 | return None | ||
diff --git a/note_tools/commands.py b/note_tools/commands.py index 5d150a1..e69de29 100644 --- a/note_tools/commands.py +++ b/note_tools/commands.py | |||
| @@ -1,56 +0,0 @@ | |||
| 1 | import sublime | ||
| 2 | import sublime_plugin | ||
| 3 | import datetime | ||
| 4 | import os | ||
| 5 | from pathlib import Path | ||
| 6 | from typing import List, Optional, Tuple | ||
| 7 | |||
| 8 | class NewNoteCommand(sublime_plugin.WindowCommand): | ||
| 9 | def run(self): | ||
| 10 | # Get the current date formatted as YYYYMMDD and YYYY-MM-DD | ||
| 11 | date_str_yyyymmdd = datetime.datetime.now().strftime("%Y%m%d") | ||
| 12 | date_str_display = datetime.datetime.now().strftime("%Y-%m-%d") | ||
| 13 | file_name = f"{date_str_yyyymmdd}.md" | ||
| 14 | |||
| 15 | # Create the underline based on the length of date_str_display | ||
| 16 | underline = '=' * len(date_str_display) | ||
| 17 | |||
| 18 | # Get the project directory and look for a "Notes" or "notes" folder | ||
| 19 | project_data = self.window.project_data() | ||
| 20 | notes_path = None | ||
| 21 | |||
| 22 | if project_data and 'folders' in project_data: | ||
| 23 | for folder in project_data['folders']: | ||
| 24 | folder_path = folder['path'] | ||
| 25 | if os.path.basename(folder_path).lower() == "notes": | ||
| 26 | notes_path = folder_path | ||
| 27 | break | ||
| 28 | |||
| 29 | if notes_path: | ||
| 30 | |||
| 31 | # Check if file already exists | ||
| 32 | if os.path.exists(file_path): | ||
| 33 | sublime.message_dialog(f"The file '{file_name}' already exists.") | ||
| 34 | else: | ||
| 35 | # Create the new file and write the date string with a line underneath | ||
| 36 | with open(file_path, 'w') as file: | ||
| 37 | file.write(f"{date_str_display}\n{underline}\n\n\n") | ||
| 38 | # Open the file and set the cursor position | ||
| 39 | new_view = self.window.open_file(file_path) | ||
| 40 | self.set_cursor_position(new_view) | ||
| 41 | |||
| 42 | else: | ||
| 43 | sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.") | ||
| 44 | |||
| 45 | def set_cursor_position(self, view): | ||
| 46 | # Set the cursor position two lines beneath the underline | ||
| 47 | def on_load(): | ||
| 48 | view.run_command("move_to", {"to": "bof"}) | ||
| 49 | for _ in range(3): | ||
| 50 | view.run_command("move", {"by": "lines", "forward": True}) | ||
| 51 | |||
| 52 | # Add an event listener for when the file is loaded | ||
| 53 | if view.is_loading(): | ||
| 54 | sublime.set_timeout_async(lambda: self.set_cursor_position(view), 100) | ||
| 55 | else: | ||
| 56 | on_load() | ||
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 @@ | |||
| 1 | from . import NoteBaseCommand | ||
| 2 | import os | ||
| 3 | import sublime | ||
| 4 | import sublime_plugin | ||
| 5 | from pathlib import Path | ||
| 6 | from typing import List, Optional | ||
| 7 | |||
| 8 | class NewNoteCommand(NoteBaseCommand, sublime_plugin.WindowCommand): | ||
| 9 | def run(self): | ||
| 10 | # Get the current date formatted as YYYYMMDD and YYYY-MM-DD | ||
| 11 | self.date = self.get_date() | ||
| 12 | notes_path = self.get_notes_path(self.window.project_data()) | ||
| 13 | |||
| 14 | if notes_path: | ||
| 15 | year, month = self.get_date_parts() | ||
| 16 | directory = os.path.join(notes_path, year, month) | ||
| 17 | print("directory: ", directory) | ||
| 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: | ||
| 26 | self.write_file_heading(file_path) | ||
| 27 | new_view = self.window.open_file(file_path) | ||
| 28 | self.set_cursor_position(new_view) | ||
| 29 | |||
| 30 | else: | ||
| 31 | sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.") | ||
| 32 | |||
| 33 | def set_cursor_position(self, view): | ||
| 34 | # Set the cursor position two lines beneath the underline | ||
| 35 | def on_load(): | ||
| 36 | view.run_command("move_to", {"to": "bof"}) | ||
| 37 | for _ in range(3): | ||
| 38 | view.run_command("move", {"by": "lines", "forward": True}) | ||
| 39 | |||
| 40 | # Add an event listener for when the file is loaded | ||
| 41 | if view.is_loading(): | ||
| 42 | sublime.set_timeout_async(lambda: self.set_cursor_position(view), 100) | ||
| 43 | else: | ||
| 44 | 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 new file mode 100644 index 0000000..2198a3b --- /dev/null +++ b/note_tools/tomorrow.py | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | from . import NoteBaseCommand | ||
| 2 | import os | ||
| 3 | from datetime import timedelta | ||
| 4 | import sublime | ||
| 5 | import sublime_plugin | ||
| 6 | |||
| 7 | class NewNoteForTomorrowCommand(NoteBaseCommand, sublime_plugin.WindowCommand): | ||
| 8 | def run(self): | ||
| 9 | self.date = self.get_date() + timedelta(days=1) | ||
| 10 | |||
| 11 | notes_path = self.get_notes_path(self.window.project_data()) | ||
| 12 | |||
| 13 | if notes_path: | ||
| 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 | |||
