From 6a80f48b1c3c0245bbf78bcd8ebf52e4ca2cbc0a Mon Sep 17 00:00:00 2001 From: Zach Berwaldt Date: Sat, 15 Jun 2024 11:01:13 -0400 Subject: Add project files --- .gitignore | 4 ++ .python-version | 1 + Default (Linux).sublime-keymap | 10 +++++ Default (Windows).sublime-keymap | 10 +++++ main.py | 12 +++++ notes/__init__.py | 0 notes/commands.py | 96 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 133 insertions(+) create mode 100644 .gitignore create mode 100644 .python-version create mode 100644 Default (Linux).sublime-keymap create mode 100644 Default (Windows).sublime-keymap create mode 100644 main.py create mode 100644 notes/__init__.py create mode 100644 notes/commands.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7cad780 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.pyc +*.cache +*.sublime-project +*.sublime-workspace diff --git a/.python-version b/.python-version new file mode 100644 index 0000000..98fccd6 --- /dev/null +++ b/.python-version @@ -0,0 +1 @@ +3.8 \ No newline at end of file diff --git a/Default (Linux).sublime-keymap b/Default (Linux).sublime-keymap new file mode 100644 index 0000000..52ebbf1 --- /dev/null +++ b/Default (Linux).sublime-keymap @@ -0,0 +1,10 @@ +[ + { + "keys": ["alt+n", "o"], + "command": "organize_notes" + }, + { + "keys": ["alt+n", "n"], + "command": "new_note" + } +] diff --git a/Default (Windows).sublime-keymap b/Default (Windows).sublime-keymap new file mode 100644 index 0000000..52ebbf1 --- /dev/null +++ b/Default (Windows).sublime-keymap @@ -0,0 +1,10 @@ +[ + { + "keys": ["alt+n", "o"], + "command": "organize_notes" + }, + { + "keys": ["alt+n", "n"], + "command": "new_note" + } +] diff --git a/main.py b/main.py new file mode 100644 index 0000000..c108273 --- /dev/null +++ b/main.py @@ -0,0 +1,12 @@ +from .notes.commands import ( + OrganizeNotesCommand, + NewNoteCommand +) + +__all__= [ + "NewNoteCommand", + "OrganizeNotesCommand" +] + +def plugin_loaded(): + print("NOTES HAS BEEN LOADED") \ No newline at end of file diff --git a/notes/__init__.py b/notes/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/notes/commands.py b/notes/commands.py new file mode 100644 index 0000000..2a89a32 --- /dev/null +++ b/notes/commands.py @@ -0,0 +1,96 @@ +import sublime +import sublime_plugin +import datetime +import os +from pathlib import Path +from typing import List, Optional, Tuple + + +class OrganizeNotesCommand(sublime_plugin.WindowCommand): + def run(self): + + now = datetime.datetime.now() + + project_data = self.window.project_data() + + if project_data and "folders" in project_data: + for folder in project_data['folders']: + folder_path = folder['path'] + print(folder_path) + if os.path.basename(folder_path).lower() == "notes": + notes_path = folder_path + break + + if notes_path: + notes = self.list_files(notes_path, ['.sublime-project', '.sublime-workspace']) + for note in notes: + year, month = self.parse_file_date(note.name) + dirs = os.path.join(notes_path, year, month) + os.makedirs(dirs, exist_ok=True) + new_note = os.path.join(notes_path, year, month, note.name) + os.rename(note.resolve(), new_note) + else: + sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.") + + 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] + + def parse_file_date (self, file_name: str) -> Tuple[str, str]: + year = file_name[:4] + month = file_name[4:6] + return year, month + + +class NewNoteCommand(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 + + 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 + + if notes_path: + file_path = os.path.join(notes_path, file_name) + + # Check if file already exists + if os.path.exists(file_path): + sublime.message_dialog(f"The file '{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) + + 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() -- cgit v1.1 From 38bb828fc32d9a1c61b20bcdc576c6d8566717d2 Mon Sep 17 00:00:00 2001 From: Zach Berwaldt Date: Sat, 15 Jun 2024 11:03:30 -0400 Subject: rename notes module to note_tools --- main.py | 4 +-- note_tools/__init__.py | 0 note_tools/commands.py | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++ notes/__init__.py | 0 notes/commands.py | 96 -------------------------------------------------- 5 files changed, 98 insertions(+), 98 deletions(-) create mode 100644 note_tools/__init__.py create mode 100644 note_tools/commands.py delete mode 100644 notes/__init__.py delete mode 100644 notes/commands.py diff --git a/main.py b/main.py index c108273..a01c84d 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,4 @@ -from .notes.commands import ( +from .note_tools.commands import ( OrganizeNotesCommand, NewNoteCommand ) @@ -9,4 +9,4 @@ __all__= [ ] def plugin_loaded(): - print("NOTES HAS BEEN LOADED") \ No newline at end of file + print("Note tools have been loaded.") \ No newline at end of file diff --git a/note_tools/__init__.py b/note_tools/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/note_tools/commands.py b/note_tools/commands.py new file mode 100644 index 0000000..2a89a32 --- /dev/null +++ b/note_tools/commands.py @@ -0,0 +1,96 @@ +import sublime +import sublime_plugin +import datetime +import os +from pathlib import Path +from typing import List, Optional, Tuple + + +class OrganizeNotesCommand(sublime_plugin.WindowCommand): + def run(self): + + now = datetime.datetime.now() + + project_data = self.window.project_data() + + if project_data and "folders" in project_data: + for folder in project_data['folders']: + folder_path = folder['path'] + print(folder_path) + if os.path.basename(folder_path).lower() == "notes": + notes_path = folder_path + break + + if notes_path: + notes = self.list_files(notes_path, ['.sublime-project', '.sublime-workspace']) + for note in notes: + year, month = self.parse_file_date(note.name) + dirs = os.path.join(notes_path, year, month) + os.makedirs(dirs, exist_ok=True) + new_note = os.path.join(notes_path, year, month, note.name) + os.rename(note.resolve(), new_note) + else: + sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.") + + 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] + + def parse_file_date (self, file_name: str) -> Tuple[str, str]: + year = file_name[:4] + month = file_name[4:6] + return year, month + + +class NewNoteCommand(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 + + 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 + + if notes_path: + file_path = os.path.join(notes_path, file_name) + + # Check if file already exists + if os.path.exists(file_path): + sublime.message_dialog(f"The file '{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) + + 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() diff --git a/notes/__init__.py b/notes/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/notes/commands.py b/notes/commands.py deleted file mode 100644 index 2a89a32..0000000 --- a/notes/commands.py +++ /dev/null @@ -1,96 +0,0 @@ -import sublime -import sublime_plugin -import datetime -import os -from pathlib import Path -from typing import List, Optional, Tuple - - -class OrganizeNotesCommand(sublime_plugin.WindowCommand): - def run(self): - - now = datetime.datetime.now() - - project_data = self.window.project_data() - - if project_data and "folders" in project_data: - for folder in project_data['folders']: - folder_path = folder['path'] - print(folder_path) - if os.path.basename(folder_path).lower() == "notes": - notes_path = folder_path - break - - if notes_path: - notes = self.list_files(notes_path, ['.sublime-project', '.sublime-workspace']) - for note in notes: - year, month = self.parse_file_date(note.name) - dirs = os.path.join(notes_path, year, month) - os.makedirs(dirs, exist_ok=True) - new_note = os.path.join(notes_path, year, month, note.name) - os.rename(note.resolve(), new_note) - else: - sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.") - - 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] - - def parse_file_date (self, file_name: str) -> Tuple[str, str]: - year = file_name[:4] - month = file_name[4:6] - return year, month - - -class NewNoteCommand(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 - - 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 - - if notes_path: - file_path = os.path.join(notes_path, file_name) - - # Check if file already exists - if os.path.exists(file_path): - sublime.message_dialog(f"The file '{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) - - 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() -- cgit v1.1 From c7aadf89c80324d1ff1d47fcb80d2b47d03182c6 Mon Sep 17 00:00:00 2001 From: Zach Berwaldt Date: Sat, 15 Jun 2024 11:13:29 -0400 Subject: refine Organize command --- note_tools/commands.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/note_tools/commands.py b/note_tools/commands.py index 2a89a32..3c26915 100644 --- a/note_tools/commands.py +++ b/note_tools/commands.py @@ -13,6 +13,8 @@ class OrganizeNotesCommand(sublime_plugin.WindowCommand): project_data = self.window.project_data() + notes_path = None + if project_data and "folders" in project_data: for folder in project_data['folders']: folder_path = folder['path'] @@ -21,7 +23,7 @@ class OrganizeNotesCommand(sublime_plugin.WindowCommand): notes_path = folder_path break - if notes_path: + if notes_path is not None: notes = self.list_files(notes_path, ['.sublime-project', '.sublime-workspace']) for note in notes: year, month = self.parse_file_date(note.name) -- cgit v1.1 From 65dea987ae4217606d0a682ec49128a7a2c18c25 Mon Sep 17 00:00:00 2001 From: Zach Berwaldt Date: Sun, 16 Jun 2024 14:56:14 -0400 Subject: add new lines --- .python-version | 2 +- main.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.python-version b/.python-version index 98fccd6..cc1923a 100644 --- a/.python-version +++ b/.python-version @@ -1 +1 @@ -3.8 \ No newline at end of file +3.8 diff --git a/main.py b/main.py index a01c84d..d388de5 100644 --- a/main.py +++ b/main.py @@ -9,4 +9,5 @@ __all__= [ ] def plugin_loaded(): - print("Note tools have been loaded.") \ No newline at end of file + print("Note tools have been loaded.") + \ No newline at end of file -- cgit v1.1