From 6cd9cab41db438c3a94bfd441d6e18bec2026385 Mon Sep 17 00:00:00 2001 From: Zach Berwaldt Date: Wed, 19 Feb 2025 18:56:45 -0500 Subject: Add command file and menu file. --- Notes.sublime-commands | 6 ++++++ Notes.sublime-menu | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 Notes.sublime-commands create mode 100644 Notes.sublime-menu diff --git a/Notes.sublime-commands b/Notes.sublime-commands new file mode 100644 index 0000000..03b691b --- /dev/null +++ b/Notes.sublime-commands @@ -0,0 +1,6 @@ +[ + { + "caption": "Notes: New Note for Today", + "command": "new_note" + } +] \ No newline at end of file diff --git a/Notes.sublime-menu b/Notes.sublime-menu new file mode 100644 index 0000000..cf9560f --- /dev/null +++ b/Notes.sublime-menu @@ -0,0 +1,16 @@ +[ + { + "id": "tools", + "children": [ + { + "caption": "Notes", + "children": [ + { + "caption": "New Note for Today", + "command": "new_note" + } + ] + } + ] + } +] \ No newline at end of file -- cgit v1.1 From c6eb143b51b0c105b157e9d408d6e9fcfaa5abfc Mon Sep 17 00:00:00 2001 From: Zach Berwaldt Date: Wed, 19 Feb 2025 20:01:21 -0500 Subject: add new command. Major refactor. --- Default (Windows).sublime-keymap | 4 +++ Notes.sublime-commands | 4 +++ Notes.sublime-menu | 4 +++ main.py | 10 +++--- note_tools/__init__.py | 38 ++++++++++++++++++++++ note_tools/commands.py | 68 +++++++++++++++++++--------------------- 6 files changed, 87 insertions(+), 41 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 @@ { "keys": ["alt+n", "n"], "command": "new_note" + }, + { + "keys": ["alt+n", "t"], + "command": "new_note_for_tomorrow" } ] diff --git a/Notes.sublime-commands b/Notes.sublime-commands index 03b691b..f554bc0 100644 --- a/Notes.sublime-commands +++ b/Notes.sublime-commands @@ -2,5 +2,9 @@ { "caption": "Notes: New Note for Today", "command": "new_note" + }, + { + "caption": "Notes: New Note for Tomorrow", + "command": "new_note_for_tomorrow" } ] \ No newline at end of file diff --git a/Notes.sublime-menu b/Notes.sublime-menu index cf9560f..cb87c38 100644 --- a/Notes.sublime-menu +++ b/Notes.sublime-menu @@ -8,6 +8,10 @@ { "caption": "New Note for Today", "command": "new_note" + }, + { + "caption": "New Note for Tomorrow", + "command": "new_note_for_tomorrow" } ] } diff --git a/main.py b/main.py index 7095cf2..67e7a35 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,9 @@ from .note_tools.commands import ( - OrganizeNotesCommand, - NewNoteCommand + NewNoteCommand, + NewNoteForTomorrowCommand ) __all__= [ - "NewNoteCommand" + "NewNoteCommand", + "NewNoteForTomorrowCommand" ] - -def plugin_loaded(): - print("Note tools have been loaded.") diff --git a/note_tools/__init__.py b/note_tools/__init__.py index e69de29..69e6e67 100644 --- a/note_tools/__init__.py +++ b/note_tools/__init__.py @@ -0,0 +1,38 @@ +from datetime import datetime +import os +from typing import List, Optional, Tuple + + +class NoteBaseCommand(object): + date = None + + def file_name(self): + return f"{self.date.strftime('%Y%m%d')}.md" + + def get_formatted_dates(self) -> Tuple[str, str]: + return (self.date.strftime("%Y%m%d"), self.date.strftime("%Y-%m-%d")) + + def get_date(self) -> datetime: + return datetime.now() + + def get_date_parts(self) -> Tuple[str]: + return (self.date.strftime('%Y'), self.date.strftime('%m'), self.date.strftime('%d')) + + def write_file_heading(self, file_path): + date_yyyymmdd = self.date.strftime('%Y-%m-%d') + underline = '=' * len(date_yyyymmdd) + with open(file_path, 'w') as file: + file.write(f"{date_yyyymmdd}\n{underline}\n\n\n") + + def parse_file_date (self, file_name: str) -> Tuple[str, str]: + year = file_name[:4] + month = file_name[4:6] + return year, month + + def get_notes_path(self, project_data) -> Optional[str]: + if project_data and 'folders' in project_data: + for folder in project_data['folders']: + folder_path = folder['path'] + if os.path.basename(folder_path).lower() == "notes": + return folder_path + return None diff --git a/note_tools/commands.py b/note_tools/commands.py index 610d9be..97778ba 100644 --- a/note_tools/commands.py +++ b/note_tools/commands.py @@ -1,46 +1,49 @@ -import sublime +from . import NoteBaseCommand import sublime_plugin import datetime import os -import sys -from pathlib import Path from typing import List, Optional, Tuple -class NewNoteCommand(sublime_plugin.WindowCommand): +class NewNoteForTomorrowCommand(NoteBaseCommand, sublime_plugin.WindowCommand): def run(self): - # Get the current date formatted as YYYYMMDD and YYYY-MM-DD - date_str_yyyymmdd = datetime.datetime.now().strftime("%Y%m%d") - date_str_display = datetime.datetime.now().strftime("%Y-%m-%d") - file_name = f"{date_str_yyyymmdd}.md" - - # Create the underline based on the length of date_str_display - underline = '=' * len(date_str_display) - - # Get the project directory and look for a "Notes" or "notes" folder - project_data = self.window.project_data() - notes_path = None + self.date = self.get_date() + timedelta(days=1) - if project_data and 'folders' in project_data: - for folder in project_data['folders']: - folder_path = folder['path'] - if os.path.basename(folder_path).lower() == "notes": - notes_path = folder_path - break + notes_path = self.get_notes_path(self.window.project_data()) + + if notes_path: + year, month, day = 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.") + +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, day = date_str_display.split('-') - file_path = os.path.join(notes_path, year, month, file_name) + year, month, day = 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 '{file_name}' already exists.") + sublime.message_dialog(f"The file '{self.file_name()}' already exists.") else: - # Create the new file and write the date string with a line underneath - with open(file_path, 'w') as file: - file.write(f"{date_str_display}\n{underline}\n\n\n") - # Open the file and set the cursor position - new_view = self.window.open_file(file_path) - self.set_cursor_position(new_view) + 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.") @@ -62,8 +65,3 @@ class NewNoteCommand(sublime_plugin.WindowCommand): 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] - - def parse_file_date (self, file_name: str) -> Tuple[str, str]: - year = file_name[:4] - month = file_name[4:6] - return year, month -- cgit v1.1 From 9cde3624920cb1ddd563b8addfcc65ccc79e080a Mon Sep 17 00:00:00 2001 From: Zach Berwaldt Date: Wed, 19 Feb 2025 20:35:50 -0500 Subject: Finish refactor --- main.py | 14 +++++------ note_tools/__init__.py | 5 ++-- note_tools/commands.py | 67 -------------------------------------------------- note_tools/today.py | 49 ++++++++++++++++++++++++++++++++++++ note_tools/tomorrow.py | 27 ++++++++++++++++++++ 5 files changed, 84 insertions(+), 78 deletions(-) delete mode 100644 note_tools/commands.py create mode 100644 note_tools/today.py create mode 100644 note_tools/tomorrow.py diff --git a/main.py b/main.py index 67e7a35..816c721 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,7 @@ -from .note_tools.commands import ( - NewNoteCommand, - NewNoteForTomorrowCommand -) +from .note_tools.today import NewNoteCommand +from .note_tools.tomorrow import NewNoteForTomorrowCommand -__all__= [ - "NewNoteCommand", - "NewNoteForTomorrowCommand" -] +__all__ = [ + 'NewNoteCommand', + 'NewNoteForTomorrowCommand' +] \ No newline at end of file diff --git a/note_tools/__init__.py b/note_tools/__init__.py index 69e6e67..8d6774d 100644 --- a/note_tools/__init__.py +++ b/note_tools/__init__.py @@ -1,7 +1,6 @@ from datetime import datetime import os -from typing import List, Optional, Tuple - +from typing import Optional, Tuple class NoteBaseCommand(object): date = None @@ -16,7 +15,7 @@ class NoteBaseCommand(object): return datetime.now() def get_date_parts(self) -> Tuple[str]: - return (self.date.strftime('%Y'), self.date.strftime('%m'), self.date.strftime('%d')) + return (self.date.strftime('%Y'), self.date.strftime('%m')) def write_file_heading(self, file_path): date_yyyymmdd = self.date.strftime('%Y-%m-%d') diff --git a/note_tools/commands.py b/note_tools/commands.py deleted file mode 100644 index 97778ba..0000000 --- a/note_tools/commands.py +++ /dev/null @@ -1,67 +0,0 @@ -from . import NoteBaseCommand -import sublime_plugin -import datetime -import os -from typing import List, Optional, Tuple - -class NewNoteForTomorrowCommand(NoteBaseCommand, sublime_plugin.WindowCommand): - def run(self): - self.date = self.get_date() + timedelta(days=1) - - notes_path = self.get_notes_path(self.window.project_data()) - - if notes_path: - year, month, day = 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.") - -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, day = 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) - 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] 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] 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 @@ +from . import NoteBaseCommand +import os +from datetime import timedelta +import sublime +import sublime_plugin + +class NewNoteForTomorrowCommand(NoteBaseCommand, sublime_plugin.WindowCommand): + def run(self): + self.date = self.get_date() + timedelta(days=1) + + 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.") + -- cgit v1.1 From acde589ba4d3c70af66a08cc83cf9f387103c947 Mon Sep 17 00:00:00 2001 From: Zach Berwaldt Date: Thu, 13 Feb 2025 20:59:08 -0500 Subject: update organize notes command to close and reopen the active note. --- note_tools/commands.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 note_tools/commands.py diff --git a/note_tools/commands.py b/note_tools/commands.py new file mode 100644 index 0000000..e69de29 -- cgit v1.1 From 779228adc67a23d08ac1a2d5846917614b2690c1 Mon Sep 17 00:00:00 2001 From: Zach Berwaldt Date: Wed, 19 Feb 2025 20:01:21 -0500 Subject: add new command. Major refactor. --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index 816c721..39b6387 100644 --- a/main.py +++ b/main.py @@ -4,4 +4,4 @@ from .note_tools.tomorrow import NewNoteForTomorrowCommand __all__ = [ 'NewNoteCommand', 'NewNoteForTomorrowCommand' -] \ No newline at end of file +] -- cgit v1.1 From 590e84a29672f5ecf458d53aa1d4ca587f7c47a6 Mon Sep 17 00:00:00 2001 From: Zach Berwaldt Date: Wed, 19 Feb 2025 20:35:50 -0500 Subject: Finish refactor --- note_tools/commands.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 note_tools/commands.py diff --git a/note_tools/commands.py b/note_tools/commands.py deleted file mode 100644 index e69de29..0000000 -- cgit v1.1