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