summaryrefslogtreecommitdiff
path: root/note_tools/today.py
diff options
context:
space:
mode:
Diffstat (limited to 'note_tools/today.py')
-rw-r--r--note_tools/today.py49
1 files changed, 49 insertions, 0 deletions
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 @@
1from . import NoteBaseCommand
2import os
3import sublime
4import sublime_plugin
5from pathlib import Path
6from typing import List, Optional
7
8class 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]