diff options
Diffstat (limited to 'note_tools')
-rw-r--r-- | note_tools/__init__.py | 37 | ||||
-rw-r--r-- | note_tools/commands.py | 56 | ||||
-rw-r--r-- | note_tools/today.py | 49 | ||||
-rw-r--r-- | note_tools/tomorrow.py | 27 |
4 files changed, 113 insertions, 56 deletions
diff --git a/note_tools/__init__.py b/note_tools/__init__.py index e69de29..8d6774d 100644 --- a/note_tools/__init__.py +++ b/note_tools/__init__.py | |||
@@ -0,0 +1,37 @@ | |||
1 | from datetime import datetime | ||
2 | import os | ||
3 | from typing import Optional, Tuple | ||
4 | |||
5 | class NoteBaseCommand(object): | ||
6 | date = None | ||
7 | |||
8 | def file_name(self): | ||
9 | return f"{self.date.strftime('%Y%m%d')}.md" | ||
10 | |||
11 | def get_formatted_dates(self) -> Tuple[str, str]: | ||
12 | return (self.date.strftime("%Y%m%d"), self.date.strftime("%Y-%m-%d")) | ||
13 | |||
14 | def get_date(self) -> datetime: | ||
15 | return datetime.now() | ||
16 | |||
17 | def get_date_parts(self) -> Tuple[str]: | ||
18 | return (self.date.strftime('%Y'), self.date.strftime('%m')) | ||
19 | |||
20 | def write_file_heading(self, file_path): | ||
21 | date_yyyymmdd = self.date.strftime('%Y-%m-%d') | ||
22 | underline = '=' * len(date_yyyymmdd) | ||
23 | with open(file_path, 'w') as file: | ||
24 | file.write(f"{date_yyyymmdd}\n{underline}\n\n\n") | ||
25 | |||
26 | def parse_file_date (self, file_name: str) -> Tuple[str, str]: | ||
27 | year = file_name[:4] | ||
28 | month = file_name[4:6] | ||
29 | return year, month | ||
30 | |||
31 | def get_notes_path(self, project_data) -> Optional[str]: | ||
32 | if project_data and 'folders' in project_data: | ||
33 | for folder in project_data['folders']: | ||
34 | folder_path = folder['path'] | ||
35 | if os.path.basename(folder_path).lower() == "notes": | ||
36 | return folder_path | ||
37 | return None | ||
diff --git a/note_tools/commands.py b/note_tools/commands.py index 5d150a1..e69de29 100644 --- a/note_tools/commands.py +++ b/note_tools/commands.py | |||
@@ -1,56 +0,0 @@ | |||
1 | import sublime | ||
2 | import sublime_plugin | ||
3 | import datetime | ||
4 | import os | ||
5 | from pathlib import Path | ||
6 | from typing import List, Optional, Tuple | ||
7 | |||
8 | class NewNoteCommand(sublime_plugin.WindowCommand): | ||
9 | def run(self): | ||
10 | # Get the current date formatted as YYYYMMDD and YYYY-MM-DD | ||
11 | date_str_yyyymmdd = datetime.datetime.now().strftime("%Y%m%d") | ||
12 | date_str_display = datetime.datetime.now().strftime("%Y-%m-%d") | ||
13 | file_name = f"{date_str_yyyymmdd}.md" | ||
14 | |||
15 | # Create the underline based on the length of date_str_display | ||
16 | underline = '=' * len(date_str_display) | ||
17 | |||
18 | # Get the project directory and look for a "Notes" or "notes" folder | ||
19 | project_data = self.window.project_data() | ||
20 | notes_path = None | ||
21 | |||
22 | if project_data and 'folders' in project_data: | ||
23 | for folder in project_data['folders']: | ||
24 | folder_path = folder['path'] | ||
25 | if os.path.basename(folder_path).lower() == "notes": | ||
26 | notes_path = folder_path | ||
27 | break | ||
28 | |||
29 | if notes_path: | ||
30 | |||
31 | # Check if file already exists | ||
32 | if os.path.exists(file_path): | ||
33 | sublime.message_dialog(f"The file '{file_name}' already exists.") | ||
34 | else: | ||
35 | # Create the new file and write the date string with a line underneath | ||
36 | with open(file_path, 'w') as file: | ||
37 | file.write(f"{date_str_display}\n{underline}\n\n\n") | ||
38 | # Open the file and set the cursor position | ||
39 | new_view = self.window.open_file(file_path) | ||
40 | self.set_cursor_position(new_view) | ||
41 | |||
42 | else: | ||
43 | sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.") | ||
44 | |||
45 | def set_cursor_position(self, view): | ||
46 | # Set the cursor position two lines beneath the underline | ||
47 | def on_load(): | ||
48 | view.run_command("move_to", {"to": "bof"}) | ||
49 | for _ in range(3): | ||
50 | view.run_command("move", {"by": "lines", "forward": True}) | ||
51 | |||
52 | # Add an event listener for when the file is loaded | ||
53 | if view.is_loading(): | ||
54 | sublime.set_timeout_async(lambda: self.set_cursor_position(view), 100) | ||
55 | else: | ||
56 | on_load() | ||
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 @@ | |||
1 | from . import NoteBaseCommand | ||
2 | import os | ||
3 | import sublime | ||
4 | import sublime_plugin | ||
5 | from pathlib import Path | ||
6 | from typing import List, Optional | ||
7 | |||
8 | class NewNoteCommand(NoteBaseCommand, sublime_plugin.WindowCommand): | ||
9 | def run(self): | ||
10 | # Get the current date formatted as YYYYMMDD and YYYY-MM-DD | ||
11 | self.date = self.get_date() | ||
12 | notes_path = self.get_notes_path(self.window.project_data()) | ||
13 | |||
14 | if notes_path: | ||
15 | year, month = self.get_date_parts() | ||
16 | directory = os.path.join(notes_path, year, month) | ||
17 | print("directory: ", directory) | ||
18 | os.makedirs(directory, exist_ok=True) | ||
19 | file_path = os.path.join(directory, self.file_name()) | ||
20 | print('path: ', file_path) | ||
21 | |||
22 | # Check if file already exists | ||
23 | if os.path.exists(file_path): | ||
24 | sublime.message_dialog(f"The file '{self.file_name()}' already exists.") | ||
25 | else: | ||
26 | self.write_file_heading(file_path) | ||
27 | new_view = self.window.open_file(file_path) | ||
28 | self.set_cursor_position(new_view) | ||
29 | |||
30 | else: | ||
31 | sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.") | ||
32 | |||
33 | def set_cursor_position(self, view): | ||
34 | # Set the cursor position two lines beneath the underline | ||
35 | def on_load(): | ||
36 | view.run_command("move_to", {"to": "bof"}) | ||
37 | for _ in range(3): | ||
38 | view.run_command("move", {"by": "lines", "forward": True}) | ||
39 | |||
40 | # Add an event listener for when the file is loaded | ||
41 | if view.is_loading(): | ||
42 | sublime.set_timeout_async(lambda: self.set_cursor_position(view), 100) | ||
43 | else: | ||
44 | on_load() | ||
45 | |||
46 | def list_files(self, directory: str, ignore_extensions: Optional[List[str]] = None) -> List[Path]: | ||
47 | if ignore_extensions is None: | ||
48 | ignore_extensions = [] | ||
49 | 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 @@ | |||
1 | from . import NoteBaseCommand | ||
2 | import os | ||
3 | from datetime import timedelta | ||
4 | import sublime | ||
5 | import sublime_plugin | ||
6 | |||
7 | class NewNoteForTomorrowCommand(NoteBaseCommand, sublime_plugin.WindowCommand): | ||
8 | def run(self): | ||
9 | self.date = self.get_date() + timedelta(days=1) | ||
10 | |||
11 | notes_path = self.get_notes_path(self.window.project_data()) | ||
12 | |||
13 | if notes_path: | ||
14 | year, month = self.get_date_parts() | ||
15 | directory = os.path.join(notes_path, year, month) | ||
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 | |||