Class: Notes

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

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Notes

Returns a new instance of Notes.



8
9
10
11
12
13
14
15
16
# File 'lib/cnote/notes.rb', line 8

def initialize(config)
  @config = config
  @notes = Dir[File.join(@config.note_path, "**", "*")].select do |file|
    File.extname(file) == ".md"
  end.map do |file|
    Note.new(file)
  end
  @filtered = @notes
end

Instance Method Details

#await_command(message = nil) ⇒ Object

/================================#

REPL type thing         #

================================/#



22
23
24
25
26
27
28
29
30
# File 'lib/cnote/notes.rb', line 22

def await_command(message = nil)
  puts message if message
  print "#{@config.cursor} ".magenta
  input = STDIN.gets.chomp

  # Strip and process
  action, *params = input.strip.gsub(/\s{2,}/, " ").split(" ")
  run_command(action || "help", params)
end

#create(params) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/cnote/notes.rb', line 99

def create(params)
  if params.first
    dirname = File.dirname(params.first)
    new_filename = File.basename(params.first, File.extname(params.first)) + ".md"
    rel_path = ""
    tags = []

    if params.include? "+t"
      tags = params.slice(params.index("+t") + 1, params.length)
      puts "CREATING WITH TAGS: #{tags}"
    end

    if dirname != "."
      rel_path = dirname
        .gsub(@config.note_path, "")
        .gsub(File.basename(params.first), "")
    end

    full_path = File.join(@config.note_path, rel_path, new_filename)

    if File.exists?(full_path)
      if confirm("#{"Whoa!".bold.red} That file already exists. Overwrite it?")
        File.delete(full_path)
        @notes.each do |note|
          if note.path == full_path
            @notes.delete(note)
            puts "Removed!"
          end
        end
      else
        return
      end
    end

    system "#{@config.editor} '#{full_path}'"

    note = Note.new(full_path)
    note.add_tags(tags) if tags.length > 0
    note.created = Time.new
    note.update

    @notes << Note.new(full_path)

    print_list("Created", [note])
    @filtered = [note]
  else
    puts "Please enter a filename as the first parameter"
  end
end

#delete(params) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/cnote/notes.rb', line 161

def delete(params)
  num = params.first.to_i
  note = @filtered[num - 1]

  if note
    if confirm("You're #{"sure".italic} you want to delete note #{num.to_s.bold.white} with title #{note.title.bold.white}?")
      FileUtils.rm(note.path)
      @notes.delete(note)
      @filtered.delete(note)
      puts "Deleted!"
    else
      puts "Whew! That was close."
    end
  else
    puts "Looks like my job is done here, since note #{num} doesn't exist anyway!"
  end
end

#helpObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/cnote/notes.rb', line 225

def help
  puts
  puts "Enter a command with the structure:"
  puts "    #{@config.cursor} action parameter(s)"
  puts
  puts "Actions:"
  puts "    - #{"new".bold.white} #{"filename".italic}"
  puts "    - #{"edit".bold.white} #{"note_number".italic}"
  puts "    - #{"delete".bold.white} #{"note_number".italic}"
  puts "    - #{"peek".bold.white} #{"note_number".italic}"
  puts "    - #{"tag".bold.white} #{"note_number".italic}"
  puts "    - #{"untag".bold.white} #{"note_number".italic}"
  puts "    - #{"search".bold.white} #{"search_term".italic}"
  puts "    - #{"list".bold.white}"
  puts "    - #{"exit".bold.white}"
  puts "    - #{"help".bold.white}"
  puts
  puts "Alternate actions:"
  puts "  Most actions also have aliases that do the same thing."
  puts "  These are listed for each command:"
  puts "    - new: create, c, n"
  puts "    - edit: e, open, o"
  puts "    - delete: d, rm"
  puts "    - peek: p"
  puts "    - tag: t"
  puts "    - untag: ut"
  puts "    - search: find, f, s"
  puts "    - list: l, ls"
  puts "    - exit: quit, q, close"
  puts "    - help: h"
  puts
end

#listObject



258
259
260
261
# File 'lib/cnote/notes.rb', line 258

def list
  @filtered = recently_edited_first(@notes)
  print_list("All Notes", @filtered)
end

#open(params) ⇒ Object



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

def open(params)
  num = params.first.to_i
  note = @filtered[num - 1]

  if note
    system "#{@config.editor} '#{note.path}'"
    note.update
  else
    puts "Hey! There is no note #{num}! Nice try."
  end
end

#peek(params) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/cnote/notes.rb', line 179

def peek(params)
  note = @filtered[params.first.to_i - 1]
  if note
    puts
    puts "-" * 40
    puts note.title.bold.white
    puts note.content.split("\n").slice(0, 10)
    puts
    puts "... (cont'd) ...".italic.gray
    puts "-" * 40
    puts
  else
    puts "Note doesn't exist!"
  end
end

#run_command(action, params) ⇒ Object



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
# File 'lib/cnote/notes.rb', line 32

def run_command(action, params)
  case action.downcase
  when "new", "create", "n", "c"
    create(params)
  when "edit", "open", "e", "o"
    open(params)
  when "delete", "d", "rm"
    delete(params)
  when "peek", "p"
    peek(params)
  when "tag", "t"
    tag(params)
  when "untag", "ut"
    untag(params)
  when "search", "find", "s", "f"
    search(params.join(" "))
  when "list", "l", "ls"
    list
  when "help", "h"
    help
  when "quit", "exit", "close", "q"
    exit
  else
    puts "Sorry, didn't quite get that..."
    help
  end

  await_command # Drop back to REPL
end

#search(term) ⇒ Object

/================================#

The Commands           #

================================/#



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cnote/notes.rb', line 66

def search(term)    
  term = term.downcase # Search is case insensitive
  matches = @notes

  if term.include? "+t "
    term, tags = term.split("+t ")
    tags = tags.split(" ")
    puts "\n    Searching: '#{term.strip}' with tags: #{tags}"
    matches = matches.select do |note|
     has_all_tags(note, tags)
    end
  elsif term.include? "-t "
    term, tags = term.split("-t ")
    tags = tags.split(" ")
    puts "\n    Searching: '#{term.strip}' without tags: #{tags}"
    matches = matches.select do |note|
      has_none_tags(note, tags)
    end
  end

  term.strip!

  @filtered = matches.select do |note|
    note.title.downcase.include?(term) || note.content.downcase.include?(term)
  end

  # TODO: Sort by most relevant
  # TODO: Highlight keywords where found
  len = @filtered.length

  print_list("Found #{len} Match#{"es" if len != 1}", @filtered)
end

#tag(params) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/cnote/notes.rb', line 195

def tag(params)
  notes = multi_note(params)

  notes.each do |note|
    tags = params.slice(1, params.length)
    note.add_tags(tags)
  end

  print_list("Changed", notes)

  @filtered = notes

  puts "Added #{params.length - 1} tag#{"s" if params.length != 2} to #{notes.length} note#{"s" if notes.length != 1}."
end

#untag(params) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/cnote/notes.rb', line 210

def untag(params)
  notes = multi_note(params)

  notes.each do |note|
    tags = params.slice(1, params.length)
    note.remove_tags(tags)
  end
  
  print_list("Changed", notes)

  @filtered = notes

  puts "Removed #{params.length - 1} tag#{"s" if params.length != 2} from #{notes.length} note#{"s" if notes.length != 1}."
end