diff options
author | Zach Berwaldt <zberwaldt@tutamail.com> | 2025-02-19 20:01:21 -0500 |
---|---|---|
committer | Zach Berwaldt <zberwaldt@tutamail.com> | 2025-02-19 20:01:21 -0500 |
commit | c6eb143b51b0c105b157e9d408d6e9fcfaa5abfc (patch) | |
tree | 407a42b94798d98a15a49198b65fe8a9a0b46b9e | |
parent | 6cd9cab41db438c3a94bfd441d6e18bec2026385 (diff) |
add new command. Major refactor.
-rw-r--r-- | Default (Windows).sublime-keymap | 4 | ||||
-rw-r--r-- | Notes.sublime-commands | 4 | ||||
-rw-r--r-- | Notes.sublime-menu | 4 | ||||
-rw-r--r-- | main.py | 10 | ||||
-rw-r--r-- | note_tools/__init__.py | 38 | ||||
-rw-r--r-- | 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 @@ | |||
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 index 03b691b..f554bc0 100644 --- a/Notes.sublime-commands +++ b/Notes.sublime-commands | |||
@@ -2,5 +2,9 @@ | |||
2 | { | 2 | { |
3 | "caption": "Notes: New Note for Today", | 3 | "caption": "Notes: New Note for Today", |
4 | "command": "new_note" | 4 | "command": "new_note" |
5 | }, | ||
6 | { | ||
7 | "caption": "Notes: New Note for Tomorrow", | ||
8 | "command": "new_note_for_tomorrow" | ||
5 | } | 9 | } |
6 | ] \ No newline at end of file | 10 | ] \ 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 @@ | |||
8 | { | 8 | { |
9 | "caption": "New Note for Today", | 9 | "caption": "New Note for Today", |
10 | "command": "new_note" | 10 | "command": "new_note" |
11 | }, | ||
12 | { | ||
13 | "caption": "New Note for Tomorrow", | ||
14 | "command": "new_note_for_tomorrow" | ||
11 | } | 15 | } |
12 | ] | 16 | ] |
13 | } | 17 | } |
@@ -1,11 +1,9 @@ | |||
1 | from .note_tools.commands import ( | 1 | from .note_tools.commands import ( |
2 | OrganizeNotesCommand, | 2 | NewNoteCommand, |
3 | NewNoteCommand | 3 | NewNoteForTomorrowCommand |
4 | ) | 4 | ) |
5 | 5 | ||
6 | __all__= [ | 6 | __all__= [ |
7 | "NewNoteCommand" | 7 | "NewNoteCommand", |
8 | "NewNoteForTomorrowCommand" | ||
8 | ] | 9 | ] |
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..69e6e67 100644 --- a/note_tools/__init__.py +++ b/note_tools/__init__.py | |||
@@ -0,0 +1,38 @@ | |||
1 | from datetime import datetime | ||
2 | import os | ||
3 | from typing import List, Optional, Tuple | ||
4 | |||
5 | |||
6 | class NoteBaseCommand(object): | ||
7 | date = None | ||
8 | |||
9 | def file_name(self): | ||
10 | return f"{self.date.strftime('%Y%m%d')}.md" | ||
11 | |||
12 | def get_formatted_dates(self) -> Tuple[str, str]: | ||
13 | return (self.date.strftime("%Y%m%d"), self.date.strftime("%Y-%m-%d")) | ||
14 | |||
15 | def get_date(self) -> datetime: | ||
16 | return datetime.now() | ||
17 | |||
18 | def get_date_parts(self) -> Tuple[str]: | ||
19 | return (self.date.strftime('%Y'), self.date.strftime('%m'), self.date.strftime('%d')) | ||
20 | |||
21 | def write_file_heading(self, file_path): | ||
22 | date_yyyymmdd = self.date.strftime('%Y-%m-%d') | ||
23 | underline = '=' * len(date_yyyymmdd) | ||
24 | with open(file_path, 'w') as file: | ||
25 | file.write(f"{date_yyyymmdd}\n{underline}\n\n\n") | ||
26 | |||
27 | def parse_file_date (self, file_name: str) -> Tuple[str, str]: | ||
28 | year = file_name[:4] | ||
29 | month = file_name[4:6] | ||
30 | return year, month | ||
31 | |||
32 | def get_notes_path(self, project_data) -> Optional[str]: | ||
33 | if project_data and 'folders' in project_data: | ||
34 | for folder in project_data['folders']: | ||
35 | folder_path = folder['path'] | ||
36 | if os.path.basename(folder_path).lower() == "notes": | ||
37 | return folder_path | ||
38 | 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 @@ | |||
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 | ||