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.py60
1 files changed, 12 insertions, 48 deletions
diff --git a/note_tools/commands.py b/note_tools/commands.py
index 755bd5d..610d9be 100644
--- a/note_tools/commands.py
+++ b/note_tools/commands.py
@@ -6,52 +6,6 @@ import sys
6from pathlib import Path 6from pathlib import Path
7from typing import List, Optional, Tuple 7from typing import List, Optional, Tuple
8 8
9
10class OrganizeNotesCommand(sublime_plugin.WindowCommand):
11 def run(self):
12
13 now = datetime.datetime.now()
14
15 project_data = self.window.project_data()
16
17 notes_path = None
18
19 active_sheet = self.window.active_sheet()
20
21 # Close the active note right away
22 active_sheet.close()
23
24 if project_data and "folders" in project_data:
25 for folder in project_data['folders']:
26 folder_path = folder['path']
27 print(folder_path)
28 if os.path.basename(folder_path).lower() == "notes":
29 notes_path = folder_path
30 break
31
32 if notes_path is not None:
33 notes = self.list_files(notes_path, ['.sublime-project', '.sublime-workspace'])
34 for note in notes:
35 year, month = self.parse_file_date(note.name)
36 dirs = os.path.join(notes_path, year, month)
37 os.makedirs(dirs, exist_ok=True)
38 new_note = os.path.join(notes_path, year, month, note.name)
39 os.rename(note.resolve(), new_note)
40 new_view = self.window.open_file(new_note)
41 else:
42 sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.")
43
44 def list_files(self, directory: str, ignore_extensions: Optional[List[str]] = None) -> List[Path]:
45 if ignore_extensions is None:
46 ignore_extensions = []
47 return [file for file in Path(directory).glob("*") if file.is_file() and file.suffix not in ignore_extensions]
48
49 def parse_file_date (self, file_name: str) -> Tuple[str, str]:
50 year = file_name[:4]
51 month = file_name[4:6]
52 return year, month
53
54
55class NewNoteCommand(sublime_plugin.WindowCommand): 9class NewNoteCommand(sublime_plugin.WindowCommand):
56 def run(self): 10 def run(self):
57 # Get the current date formatted as YYYYMMDD and YYYY-MM-DD 11 # Get the current date formatted as YYYYMMDD and YYYY-MM-DD
@@ -74,7 +28,8 @@ class NewNoteCommand(sublime_plugin.WindowCommand):
74 break 28 break
75 29
76 if notes_path: 30 if notes_path:
77 file_path = os.path.join(notes_path, file_name) 31 year, month, day = date_str_display.split('-')
32 file_path = os.path.join(notes_path, year, month, file_name)
78 33
79 # Check if file already exists 34 # Check if file already exists
80 if os.path.exists(file_path): 35 if os.path.exists(file_path):
@@ -83,7 +38,6 @@ class NewNoteCommand(sublime_plugin.WindowCommand):
83 # Create the new file and write the date string with a line underneath 38 # Create the new file and write the date string with a line underneath
84 with open(file_path, 'w') as file: 39 with open(file_path, 'w') as file:
85 file.write(f"{date_str_display}\n{underline}\n\n\n") 40 file.write(f"{date_str_display}\n{underline}\n\n\n")
86
87 # Open the file and set the cursor position 41 # Open the file and set the cursor position
88 new_view = self.window.open_file(file_path) 42 new_view = self.window.open_file(file_path)
89 self.set_cursor_position(new_view) 43 self.set_cursor_position(new_view)
@@ -103,3 +57,13 @@ class NewNoteCommand(sublime_plugin.WindowCommand):
103 sublime.set_timeout_async(lambda: self.set_cursor_position(view), 100) 57 sublime.set_timeout_async(lambda: self.set_cursor_position(view), 100)
104 else: 58 else:
105 on_load() 59 on_load()
60
61 def list_files(self, directory: str, ignore_extensions: Optional[List[str]] = None) -> List[Path]:
62 if ignore_extensions is None:
63 ignore_extensions = []
64 return [file for file in Path(directory).glob("*") if file.is_file() and file.suffix not in ignore_extensions]
65
66 def parse_file_date (self, file_name: str) -> Tuple[str, str]:
67 year = file_name[:4]
68 month = file_name[4:6]
69 return year, month