summaryrefslogtreecommitdiff
path: root/notes/commands.py
diff options
context:
space:
mode:
Diffstat (limited to 'notes/commands.py')
-rw-r--r--notes/commands.py96
1 files changed, 96 insertions, 0 deletions
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 @@
1import sublime
2import sublime_plugin
3import datetime
4import os
5from pathlib import Path
6from typing import List, Optional, Tuple
7
8
9class 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
46class 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()