diff options
Diffstat (limited to 'note_tools/commands.py')
-rw-r--r-- | note_tools/commands.py | 68 |
1 files changed, 33 insertions, 35 deletions
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 @@ | |||
1 | import sublime | 1 | from . import NoteBaseCommand |
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 | ||
7 | from typing import List, Optional, Tuple | 5 | from typing import List, Optional, Tuple |
8 | 6 | ||
9 | class NewNoteCommand(sublime_plugin.WindowCommand): | 7 | class NewNoteForTomorrowCommand(NoteBaseCommand, sublime_plugin.WindowCommand): |
10 | def run(self): | 8 | def run(self): |
11 | # Get the current date formatted as YYYYMMDD and YYYY-MM-DD | 9 | self.date = self.get_date() + timedelta(days=1) |
12 | date_str_yyyymmdd = datetime.datetime.now().strftime("%Y%m%d") | ||
13 | date_str_display = datetime.datetime.now().strftime("%Y-%m-%d") | ||
14 | file_name = f"{date_str_yyyymmdd}.md" | ||
15 | |||
16 | # Create the underline based on the length of date_str_display | ||
17 | underline = '=' * len(date_str_display) | ||
18 | |||
19 | # Get the project directory and look for a "Notes" or "notes" folder | ||
20 | project_data = self.window.project_data() | ||
21 | notes_path = None | ||
22 | 10 | ||
23 | if project_data and 'folders' in project_data: | 11 | notes_path = self.get_notes_path(self.window.project_data()) |
24 | for folder in project_data['folders']: | 12 | |
25 | folder_path = folder['path'] | 13 | if notes_path: |
26 | if os.path.basename(folder_path).lower() == "notes": | 14 | year, month, day = self.get_date_parts() |
27 | notes_path = folder_path | 15 | directory = os.path.join(notes_path, year, month) |
28 | break | 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 | |||
28 | class NewNoteCommand(NoteBaseCommand, sublime_plugin.WindowCommand): | ||
29 | def run(self): | ||
30 | # Get the current date formatted as YYYYMMDD and YYYY-MM-DD | ||
31 | self.date = self.get_date() | ||
32 | notes_path = self.get_notes_path(self.window.project_data()) | ||
29 | 33 | ||
30 | if notes_path: | 34 | if notes_path: |
31 | year, month, day = date_str_display.split('-') | 35 | year, month, day = self.get_date_parts() |
32 | file_path = os.path.join(notes_path, year, month, file_name) | 36 | directory = os.path.join(notes_path, year, month) |
37 | os.makedirs(directory, exist_ok=True) | ||
38 | file_path = os.path.join(directory, self.file_name()) | ||
33 | 39 | ||
34 | # Check if file already exists | 40 | # Check if file already exists |
35 | if os.path.exists(file_path): | 41 | if os.path.exists(file_path): |
36 | sublime.message_dialog(f"The file '{file_name}' already exists.") | 42 | sublime.message_dialog(f"The file '{self.file_name()}' already exists.") |
37 | else: | 43 | else: |
38 | # Create the new file and write the date string with a line underneath | 44 | self.write_file_heading(file_path) |
39 | with open(file_path, 'w') as file: | 45 | new_view = self.window.open_file(file_path) |
40 | file.write(f"{date_str_display}\n{underline}\n\n\n") | 46 | self.set_cursor_position(new_view) |
41 | # Open the file and set the cursor position | ||
42 | new_view = self.window.open_file(file_path) | ||
43 | self.set_cursor_position(new_view) | ||
44 | 47 | ||
45 | else: | 48 | else: |
46 | sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.") | 49 | sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.") |
@@ -62,8 +65,3 @@ class NewNoteCommand(sublime_plugin.WindowCommand): | |||
62 | if ignore_extensions is None: | 65 | if ignore_extensions is None: |
63 | ignore_extensions = [] | 66 | ignore_extensions = [] |
64 | return [file for file in Path(directory).glob("*") if file.is_file() and file.suffix not in ignore_extensions] | 67 | return [file for file in Path(directory).glob("*") if file.is_file() and file.suffix not in ignore_extensions] |
65 | |||
66 | def parse_file_date (self, file_name: str) -> Tuple[str, str]: | ||
67 | year = file_name[:4] | ||
68 | month = file_name[4:6] | ||
69 | return year, month | ||