diff options
author | Zach Berwaldt <zberwaldt@tutamail.com> | 2024-06-15 11:01:13 -0400 |
---|---|---|
committer | Zach Berwaldt <zberwaldt@tutamail.com> | 2024-06-15 11:01:13 -0400 |
commit | 6a80f48b1c3c0245bbf78bcd8ebf52e4ca2cbc0a (patch) | |
tree | 82182a9deacef1e9e077126ee1244a7e1621ff86 | |
parent | 09923f0a79d6c567c5cda77c756a122a8651a246 (diff) |
Add project files
-rw-r--r-- | .gitignore | 4 | ||||
-rw-r--r-- | .python-version | 1 | ||||
-rw-r--r-- | Default (Linux).sublime-keymap | 10 | ||||
-rw-r--r-- | Default (Windows).sublime-keymap | 10 | ||||
-rw-r--r-- | main.py | 12 | ||||
-rw-r--r-- | notes/__init__.py | 0 | ||||
-rw-r--r-- | notes/commands.py | 96 |
7 files changed, 133 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7cad780 --- /dev/null +++ b/.gitignore | |||
@@ -0,0 +1,4 @@ | |||
1 | *.pyc | ||
2 | *.cache | ||
3 | *.sublime-project | ||
4 | *.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 @@ | |||
1 | [ | ||
2 | { | ||
3 | "keys": ["alt+n", "o"], | ||
4 | "command": "organize_notes" | ||
5 | }, | ||
6 | { | ||
7 | "keys": ["alt+n", "n"], | ||
8 | "command": "new_note" | ||
9 | } | ||
10 | ] | ||
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 @@ | |||
1 | [ | ||
2 | { | ||
3 | "keys": ["alt+n", "o"], | ||
4 | "command": "organize_notes" | ||
5 | }, | ||
6 | { | ||
7 | "keys": ["alt+n", "n"], | ||
8 | "command": "new_note" | ||
9 | } | ||
10 | ] | ||
@@ -0,0 +1,12 @@ | |||
1 | from .notes.commands import ( | ||
2 | OrganizeNotesCommand, | ||
3 | NewNoteCommand | ||
4 | ) | ||
5 | |||
6 | __all__= [ | ||
7 | "NewNoteCommand", | ||
8 | "OrganizeNotesCommand" | ||
9 | ] | ||
10 | |||
11 | def plugin_loaded(): | ||
12 | 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 --- /dev/null +++ b/notes/__init__.py | |||
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 @@ | |||
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 | |||
9 | class OrganizeNotesCommand(sublime_plugin.WindowCommand): | ||
10 | def run(self): | ||
11 | |||
12 | now = datetime.datetime.now() | ||
13 | |||
14 | project_data = self.window.project_data() | ||
15 | |||
16 | if project_data and "folders" in project_data: | ||
17 | for folder in project_data['folders']: | ||
18 | folder_path = folder['path'] | ||
19 | print(folder_path) | ||
20 | if os.path.basename(folder_path).lower() == "notes": | ||
21 | notes_path = folder_path | ||
22 | break | ||
23 | |||
24 | if notes_path: | ||
25 | notes = self.list_files(notes_path, ['.sublime-project', '.sublime-workspace']) | ||
26 | for note in notes: | ||
27 | year, month = self.parse_file_date(note.name) | ||
28 | dirs = os.path.join(notes_path, year, month) | ||
29 | os.makedirs(dirs, exist_ok=True) | ||
30 | new_note = os.path.join(notes_path, year, month, note.name) | ||
31 | os.rename(note.resolve(), new_note) | ||
32 | else: | ||
33 | sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.") | ||
34 | |||
35 | def list_files(self, directory: str, ignore_extensions: Optional[List[str]] = None) -> List[Path]: | ||
36 | if ignore_extensions is None: | ||
37 | ignore_extensions = [] | ||
38 | return [file for file in Path(directory).glob("*") if file.is_file() and file.suffix not in ignore_extensions] | ||
39 | |||
40 | def parse_file_date (self, file_name: str) -> Tuple[str, str]: | ||
41 | year = file_name[:4] | ||
42 | month = file_name[4:6] | ||
43 | return year, month | ||
44 | |||
45 | |||
46 | class NewNoteCommand(sublime_plugin.WindowCommand): | ||
47 | def run(self): | ||
48 | # Get the current date formatted as YYYYMMDD and YYYY-MM-DD | ||
49 | date_str_yyyymmdd = datetime.datetime.now().strftime("%Y%m%d") | ||
50 | date_str_display = datetime.datetime.now().strftime("%Y-%m-%d") | ||
51 | file_name = f"{date_str_yyyymmdd}.md" | ||
52 | |||
53 | # Create the underline based on the length of date_str_display | ||
54 | underline = '=' * len(date_str_display) | ||
55 | |||
56 | # Get the project directory and look for a "Notes" or "notes" folder | ||
57 | project_data = self.window.project_data() | ||
58 | notes_path = None | ||
59 | |||
60 | if project_data and 'folders' in project_data: | ||
61 | for folder in project_data['folders']: | ||
62 | folder_path = folder['path'] | ||
63 | if os.path.basename(folder_path).lower() == "notes": | ||
64 | notes_path = folder_path | ||
65 | break | ||
66 | |||
67 | if notes_path: | ||
68 | file_path = os.path.join(notes_path, file_name) | ||
69 | |||
70 | # Check if file already exists | ||
71 | if os.path.exists(file_path): | ||
72 | sublime.message_dialog(f"The file '{file_name}' already exists.") | ||
73 | else: | ||
74 | # Create the new file and write the date string with a line underneath | ||
75 | with open(file_path, 'w') as file: | ||
76 | file.write(f"{date_str_display}\n{underline}\n\n\n") | ||
77 | |||
78 | # Open the file and set the cursor position | ||
79 | new_view = self.window.open_file(file_path) | ||
80 | self.set_cursor_position(new_view) | ||
81 | |||
82 | else: | ||
83 | sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.") | ||
84 | |||
85 | def set_cursor_position(self, view): | ||
86 | # Set the cursor position two lines beneath the underline | ||
87 | def on_load(): | ||
88 | view.run_command("move_to", {"to": "bof"}) | ||
89 | for _ in range(3): | ||
90 | view.run_command("move", {"by": "lines", "forward": True}) | ||
91 | |||
92 | # Add an event listener for when the file is loaded | ||
93 | if view.is_loading(): | ||
94 | sublime.set_timeout_async(lambda: self.set_cursor_position(view), 100) | ||
95 | else: | ||
96 | on_load() | ||