Class: QuickNote

Inherits:
Object
  • Object
show all
Defined in:
lib/quick_note.rb

Overview

Author: Pedro Jafet Avalos Jimenez Date: 2019-12-22

Constant Summary collapse

@@parent_folder =

Actual parent folder that is given by the user.

''

Class Method Summary collapse

Class Method Details

.clrtodoObject

Clear all the tasks in the tasks.txt file.



150
151
152
153
154
155
156
157
158
159
# File 'lib/quick_note.rb', line 150

def self.clrtodo()
    # Check to make sure the parent folder exists (in case the user did not use mkdir first).

    mkdir() if @@parent_folder == ''

    # File directory.

    file = "#{@@parent_folder}/general/tasks.txt"

    # Clear the text file.

    File.write(file, '')
end

.delfolder(folder = '') ⇒ Object

Delete a folder.



118
119
120
121
122
123
124
125
# File 'lib/quick_note.rb', line 118

def self.delfolder(folder='')
    # Check to make sure the parent folder exists (in case the user did not use mkdir first).

    mkdir() if @@parent_folder == ''

    # Delete the folder if it exists, and if the user inputs something.

    # Otherwise it would delete the whole directory.

    Dir.delete("#{@@parent_folder}/#{folder}") if Dir.exist?("#{@@parent_folder}/#{folder}") && folder != ''
end

.delnote(name, folder = 'general') ⇒ Object

Delete a note.



109
110
111
112
113
114
115
# File 'lib/quick_note.rb', line 109

def self.delnote(name, folder='general')
    # Check to make sure the parent folder exists (in case the user did not use mkdir first).

    mkdir() if @@parent_folder == ''

    # Delete the note if it exists.

    File.delete("#{@@parent_folder}/#{folder}/#{name}.txt") if File.exist?("#{@@parent_folder}/#{folder}/#{name}.txt")
end

.lstfoldersObject

Command to list all the folders in the directory. Should print out the names of them.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/quick_note.rb', line 41

def self.lstfolders()
    # Check to make sure the parent folder exists (in case the user did not use mkdir first).

    mkdir() if @@parent_folder == ''
    # Make the directory the working directory.

    Dir.chdir(@@parent_folder)

    # The folders in the directory.

    folders = Dir.glob('*').select {|f| File.directory?(f)}
    
    # Make the terminal print out the various folders.

    folders.each {|f| system "echo #{f}"}
end

.lstnotes(folder = 'general') ⇒ Object

List all the notes in a folder (general by default).



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/quick_note.rb', line 94

def self.lstnotes(folder='general')
    # Check to make sure the parent folder exists (in case the user did not use mkdir first).

    mkdir() if @@parent_folder == ''

    # Make the directory the working directory.

    Dir.chdir("#{@@parent_folder}/#{folder}")

    # The folders in the directory.

    files = Dir.glob('*.txt')
    
    # Make the terminal print out the various folders.

    files.each {|f| system "echo #{f}"}
end

.mkdir(directory = 'c:/quick_notes') ⇒ Object

First command to use. Creates the directory at the given directory (or the default of c:/quick_notes).



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/quick_note.rb', line 10

def self.mkdir(directory='c:/quick_notes')
    # Remove the '/' at the end of the directory if it was given by the user.

    # Makes the strings in my code easier to read in my opinion.

    directory = directory[0..-2] if directory[-1] == '/'

    # Saves the given directory as the actual parent folder being used.

    @@parent_folder = directory

    # Create the directory if it doesn't already exist

    Dir.mkdir(@@parent_folder) if !Dir.exist?(@@parent_folder)
    # Creates the 'general' folder under the directory if it doesn't already exist.

    Dir.mkdir("#{@@parent_folder}/general") if !Dir.exist?("#{@@parent_folder}/general")
end

.mkfolder(folder) ⇒ Object

Command to create a folder.



32
33
34
35
36
37
# File 'lib/quick_note.rb', line 32

def self.mkfolder(folder)
    # Check to make sure the parent folder exists (in case the user did not use mkdir first).

    mkdir() if @@parent_folder == ''
    # Create the folder if it doesn't exist.

    Dir.mkdir("#{@@parent_folder}/#{folder}") if !Dir.exist?("#{@@parent_folder}/#{folder}")
end

.mknote(name, folder = 'general') ⇒ Object

Create a note with the given name and in a given folder If it exists already, then open it. By default, the folder is general.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/quick_note.rb', line 75

def self.mknote(name, folder='general')
    # Check to make sure the parent folder exists (in case the user did not use mkdir first).

    mkdir() if @@parent_folder == ''

    if name != ''
        # The note file to open.

        file = "#{@@parent_folder}/#{folder}/#{name}.txt"

        # Create the folder given if it doesn't already exist.

        Dir.mkdir("#{@@parent_folder}/#{folder}") if !Dir.exist?("#{@@parent_folder}/#{folder}")
        # Create the file with the given information if it doesn't already exist.

        File.new("#{file}", File::CREAT) if !File.exist?("#{file}")

        # Tell the terminal to start the file.

        system %{cmd /c "start #{file}"}
    end
end

.mktask(task, date = Time.now.strftime('%Y-%m-%d'), due_date = Time.now.strftime('%Y-%m-%d')) ⇒ Object

Create a todo-type of task in a default tasks.txt file in the directory.



55
56
57
58
59
60
61
62
# File 'lib/quick_note.rb', line 55

def self.mktask(task, date=Time.now.strftime('%Y-%m-%d'), due_date=Time.now.strftime('%Y-%m-%d'))
    # Check to make sure the parent folder exists (in case the user did not use mkdir first).

    mkdir() if @@parent_folder == ''

    # Open (or create) the file for tasks (tasks.txt under the general folder).

    # Append the task to do in the tasks.txt file.

    File.open("#{@@parent_folder}/general/tasks.txt", 'a') {|f| f.write "date: #{date} | due: #{due_date} | task: #{task}\n"}
end

.opdirObject

Same as opfolder, but more readable. In case the user wants to open the parent folder.



26
27
28
29
# File 'lib/quick_note.rb', line 26

def self.opdir()
    # Default parameter for opfolder() already opens the parent folder. 

    opfolder()
end

.opfolder(folder = '') ⇒ Object

Open a folder file explorer. If none is specified, then open the directory.



129
130
131
132
133
134
135
# File 'lib/quick_note.rb', line 129

def self.opfolder(folder='')
    # Check to make sure the parent folder exists (in case the user did not use mkdir first).

    mkdir() if @@parent_folder == ''

    # Terminal opens the folder with file explorer.

    system %{cmd /c "start #{@@parent_folder}/#{folder}"}
end

.opnote(name = 'tasks', folder = 'general') ⇒ Object

Open a file (tasks by default) in notepad if it exists.



138
139
140
141
142
143
144
145
146
147
# File 'lib/quick_note.rb', line 138

def self.opnote(name='tasks', folder='general')
    # Check to make sure the parent folder exists (in case the user did not use mkdir first).

    mkdir() if @@parent_folder == ''

    # The note file to open.

    file = "#{@@parent_folder}/#{folder}/#{name}.txt"

    # Tell the terminal to start the file if it exists.

    system %{cmd /c "start #{file}"} if File.exist?(file)
end

.optodoObject

Check all the tasks to do. Opens the tasks.txt file with notepad. An easier to read option instead of opfile()



67
68
69
70
# File 'lib/quick_note.rb', line 67

def self.optodo()
    # The default parameters for opnote() are already tasks.txt and general

    opnote()
end