summaryrefslogtreecommitdiff
path: root/note_tools/commands.py
blob: 97778ba4f288ce8c6268d3a2339087d4f8b2fe87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from . import NoteBaseCommand
import sublime_plugin
import datetime
import os
from typing import List, Optional, Tuple

class NewNoteForTomorrowCommand(NoteBaseCommand, sublime_plugin.WindowCommand):
    def run(self):
      self.date = self.get_date() + timedelta(days=1)

      notes_path = self.get_notes_path(self.window.project_data())

      if notes_path:
          year, month, day = self.get_date_parts()
          directory = os.path.join(notes_path, year, month)
          os.makedirs(directory, exist_ok=True)
          file_path = os.path.join(directory, self.file_name())

          # Check if file already exists
          if os.path.exists(file_path):
            sublime.message_dialog(f"The file '{self.file_name()}' already exists.")
        else:
            self.write_file_heading(file_path)

      else:
          sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.")

class NewNoteCommand(NoteBaseCommand, sublime_plugin.WindowCommand):
    def run(self):
        # Get the current date formatted as YYYYMMDD and YYYY-MM-DD
        self.date = self.get_date()                
        notes_path = self.get_notes_path(self.window.project_data())

        if notes_path:
            year, month, day = self.get_date_parts()
            directory = os.path.join(notes_path, year, month)
            os.makedirs(directory, exist_ok=True)
            file_path = os.path.join(directory, self.file_name())

            # Check if file already exists
            if os.path.exists(file_path):
              sublime.message_dialog(f"The file '{self.file_name()}' already exists.")
            else:
              self.write_file_heading(file_path)
              new_view = self.window.open_file(file_path)
              self.set_cursor_position(new_view)

        else:
            sublime.message_dialog("No 'Notes' directory found in the project. Please add a 'Notes' folder.")

    def set_cursor_position(self, view):
        # Set the cursor position two lines beneath the underline
        def on_load():
            view.run_command("move_to", {"to": "bof"})
            for _ in range(3):
                view.run_command("move", {"by": "lines", "forward": True})
        
        # Add an event listener for when the file is loaded
        if view.is_loading():
            sublime.set_timeout_async(lambda: self.set_cursor_position(view), 100)
        else:
            on_load()

    def list_files(self, directory: str, ignore_extensions: Optional[List[str]] = None) -> List[Path]:
        if ignore_extensions is None:
            ignore_extensions = []
        return [file for file in Path(directory).glob("*") if file.is_file() and file.suffix not in ignore_extensions]