diff options
Diffstat (limited to 'note_tools')
| -rw-r--r-- | note_tools/commands.py | 49 |
1 files changed, 0 insertions, 49 deletions
diff --git a/note_tools/commands.py b/note_tools/commands.py index 755bd5d..5d150a1 100644 --- a/note_tools/commands.py +++ b/note_tools/commands.py | |||
| @@ -2,56 +2,9 @@ import sublime | |||
| 2 | import sublime_plugin | 2 | import sublime_plugin |
| 3 | import datetime | 3 | import datetime |
| 4 | import os | 4 | import os |
| 5 | import sys | ||
| 6 | from pathlib import Path | 5 | from pathlib import Path |
| 7 | from typing import List, Optional, Tuple | 6 | from typing import List, Optional, Tuple |
| 8 | 7 | ||
| 9 | |||
| 10 | class OrganizeNotesCommand(sublime_plugin.WindowCommand): | ||
| 11 | def run(self): | ||
| 12 | |||
| 13 | now = datetime.datetime.now() | ||
| 14 | |||
| 15 | project_data = self.window.project_data() | ||
| 16 | |||
| 17 | notes_path = None | ||
| 18 | |||
| 19 | active_sheet = self.window.active_sheet() | ||
| 20 | |||
| 21 | # Close the active note right away | ||
| 22 | active_sheet.close() | ||
| 23 | |||
| 24 | if project_data and "folders" in project_data: | ||
| 25 | for folder in project_data['folders']: | ||
| 26 | folder_path = folder['path'] | ||
| 27 | print(folder_path) | ||
| 28 | if os.path.basename(folder_path).lower() == "notes": | ||
| 29 | notes_path = folder_path | ||
| 30 | break | ||
| 31 | |||
| 32 | if notes_path is not None: | ||
| 33 | notes = self.list_files(notes_path, ['.sublime-project', '.sublime-workspace']) | ||
| 34 | for note in notes: | ||
| 35 | year, month = self.parse_file_date(note.name) | ||
| 36 | dirs = os.path.join(notes_path, year, month) | ||
| 37 | os.makedirs(dirs, exist_ok=True) | ||
| 38 | new_note = os.path.join(notes_path, year, month, note.name) | ||
| 39 | os.rename(note.resolve(), new_note) | ||
| 40 | new_view = self.window.open_file(new_note) | ||
| 41 | else: | ||
| 42 | sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.") | ||
| 43 | |||
| 44 | def list_files(self, directory: str, ignore_extensions: Optional[List[str]] = None) -> List[Path]: | ||
| 45 | if ignore_extensions is None: | ||
| 46 | ignore_extensions = [] | ||
| 47 | return [file for file in Path(directory).glob("*") if file.is_file() and file.suffix not in ignore_extensions] | ||
| 48 | |||
| 49 | def parse_file_date (self, file_name: str) -> Tuple[str, str]: | ||
| 50 | year = file_name[:4] | ||
| 51 | month = file_name[4:6] | ||
| 52 | return year, month | ||
| 53 | |||
| 54 | |||
| 55 | class NewNoteCommand(sublime_plugin.WindowCommand): | 8 | class NewNoteCommand(sublime_plugin.WindowCommand): |
| 56 | def run(self): | 9 | def run(self): |
| 57 | # Get the current date formatted as YYYYMMDD and YYYY-MM-DD | 10 | # Get the current date formatted as YYYYMMDD and YYYY-MM-DD |
| @@ -74,7 +27,6 @@ class NewNoteCommand(sublime_plugin.WindowCommand): | |||
| 74 | break | 27 | break |
| 75 | 28 | ||
| 76 | if notes_path: | 29 | if notes_path: |
| 77 | file_path = os.path.join(notes_path, file_name) | ||
| 78 | 30 | ||
| 79 | # Check if file already exists | 31 | # Check if file already exists |
| 80 | if os.path.exists(file_path): | 32 | if os.path.exists(file_path): |
| @@ -83,7 +35,6 @@ class NewNoteCommand(sublime_plugin.WindowCommand): | |||
| 83 | # Create the new file and write the date string with a line underneath | 35 | # Create the new file and write the date string with a line underneath |
| 84 | with open(file_path, 'w') as file: | 36 | with open(file_path, 'w') as file: |
| 85 | file.write(f"{date_str_display}\n{underline}\n\n\n") | 37 | file.write(f"{date_str_display}\n{underline}\n\n\n") |
| 86 | |||
| 87 | # Open the file and set the cursor position | 38 | # Open the file and set the cursor position |
| 88 | new_view = self.window.open_file(file_path) | 39 | new_view = self.window.open_file(file_path) |
| 89 | self.set_cursor_position(new_view) | 40 | self.set_cursor_position(new_view) |
