Module: Commands

Included in:
Notes
Defined in:
lib/cnote/commands.rb

Instance Method Summary collapse

Instance Method Details

#config(params = []) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/cnote/commands.rb', line 239

def config(params = [])
  if params.length == 0
    system "#{@config.editor} #{@config.path}"
    @config.load
    return
  end

  action, key, *value = params
  value = value.join(" ")

  if action == "get"
    if key
      puts "#{key}: \"#{@config.get(key)}\""
    else
      @config.print
    end
  elsif action == "set"
    if key
      if value
        puts "Config: #{key} changed from '#{@config.get(key)}' to '#{value}'"
        @config.set(key, value)
      else
        puts "Can't set a key to a value if no value is given."
      end
    else
      puts "Can't set a key if one wasn't given."
    end
  else
    puts "Invalid action: #{action}"
  end
end

#create(params) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
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
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cnote/commands.rb', line 53

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 |num, note|
          if note.path == full_path
            @notes[num] = nil
            puts "Removed!"
          end
        end
      else
        return
      end
    else
      # Make sure the directory actually exists.
      FileUtils.mkdir_p(File.join(@config.note_path, rel_path))
    end

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

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

      note.index = @index
      @notes[@index] = note
      @index += 1

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

#delete(params) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/cnote/commands.rb', line 124

def delete(params)
  notes = multi_note(params)

  notes.each do |note|
    if note and File.exists? note.path
      num = note.index
      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[num] = nil
        @filtered[num] = nil
        puts "Deleted!"
      else
        puts "Whew! That was close."
      end
    end
  end
end

#helpObject



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/cnote/commands.rb', line 271

def help
  puts
  puts "Enter a command with the structure:"
  puts "    #{@config.prompt} action parameter(s)"
  puts
  puts "Actions:"
  puts "    - #{"new".bold.white} #{"filename".italic}"
  puts "        Create a new note.".italic
  puts
  puts "    - #{"edit".bold.white} #{"note_number".italic}"
  puts "        Open a note for editing or reading.".italic
  puts
  puts "    - #{"delete".bold.white} #{"note_number".italic}"
  puts "        Delete a note.".italic
  puts
  puts "    - #{"peek".bold.white} #{"note_number".italic}"
  puts "        Print the first few lines of a note.".italic
  puts
  puts "    - #{"tag".bold.white} #{"note_number".italic} #{"tag".italic} #{"tag".italic} #{"tag".italic} ..."
  puts "        Add the space-delimited list of tags to a given note.".italic
  puts
  puts "    - #{"untag".bold.white} #{"note_number".italic} #{"tag".italic} #{"tag".italic} ..."
  puts "        Remove the space-delimited list of tags from a given note.".italic
  puts
  puts "    - #{"tags".bold.white}"
  puts "        List all tags and the number of notes each one is applied to.".italic
  puts
  puts "    - #{"search".bold.white} #{"search_term".italic} ( +t/-t #{"tag".italic} #{"tag".italic} ... )"
  puts "        Find notes that match the search term, or the specified tags.".italic
  puts
  puts "    - #{"info".bold.white} #{"note_number(s)".italic}"
  puts "        Display detailed metadata for one or more (comma-delimited) note numbers.".italic
  puts
  puts "    - #{"list".bold.white}"
  puts "        List every single note in your notes folder.".italic
  puts
  puts "    - #{"config".bold.white} #{"(set/get)".italic} #{"key".italic} [#{"value".italic}]"
  puts "        Manage your CNote configuration, either by setting keys through commands,".italic
  puts "        or by just writing 'config' to open the file in your editor.".italic
  puts
  puts "    - #{"exit".bold.white}"
  puts "        Leave CNote.".italic
  puts
  puts "    - #{"help".bold.white}"
  puts "        You are here!".italic
  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 "    - info: i"
  puts "    - list: l, ls"
  puts "    - exit: quit, q, close"
  puts "    - help: h"
  puts
end

#info(params) ⇒ Object



231
232
233
234
235
236
237
# File 'lib/cnote/commands.rb', line 231

def info(params)
  # Shows metadata about a note or list of notes.
  notes = multi_note(params)
  set_filtered(notes)
  print_list("Note Info", @filtered, true)
  puts "Printed info for #{notes.length} notes."
end

#listObject



334
335
336
337
# File 'lib/cnote/commands.rb', line 334

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

#list_tagsObject



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/cnote/commands.rb', line 201

def list_tags
  tags = Hash.new(0)
  longest = 0
  sorted = []

  @notes.each do |num, note|
    note.tags.each do |tag|
      tags[tag] += 1;
    end
  end

  tags.each do |tag, count|
    longest = tag.length if tag.length > longest
    sorted << [tag, count]
  end

  # Sort alphabetically
  sorted.sort_by! { |item| item[0] }

  puts
  puts "#{indent}All Tags".bold
  puts "#{indent}--------"
  puts
  sorted.each do |entry|
    tag, count = entry
    puts "#{indent}#{tag.bold} #{"." * (longest + 3 - tag.length)} #{count} notes"
  end
  puts
end

#open(params) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cnote/commands.rb', line 112

def open(params)
  num = params.first.to_i
  note = @notes[num]

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

#peek(params) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/cnote/commands.rb', line 142

def peek(params)
  if params&.first&.downcase == 'config'
    return @config.print
  end

  note = @notes[params.first.to_i]
  if note
    lines = note.content.lines
    puts
    puts "-" * 40
    puts note.title.bold.white
    puts lines.slice(0, 15)
    if lines.length > 15
      puts
      puts "(#{lines.length - 15} more line#{'s' if lines.length != 16}...)".italic
    end
    puts "-" * 40
    puts
  else
    puts "Note doesn't exist!"
  end
end

#search(term) ⇒ Object

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

The Commands           #

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



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

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

  if term.include? "+t"
    term, tags = term.split("+t")
    if tags
      tags = tags.split(" ")
      matches = matches.select do |num, note|
        note != nil && has_tags(note, tags)
      end
    else
      # +t but no tags - return all results that have at least one tag
      matches = matches.select do |num, note|
        note != nil && note.tags.length > 0
      end
    end
  elsif term.include? "-t"
    term, tags = term.split("-t")
    if tags
      tags = tags.split(" ")
      matches = matches.select do |num, note|
        note != nil && does_not_have_tags(note, tags)
      end
    else
      # Likewise, return all results with no tags
      matches = matches.select do |num, note|
        note != nil && note.tags.length == 0
      end
    end
  end

  if term && term != ""
    matches = matches.select do |num, note|
      note.title.downcase.include?(term) || note.content.downcase.include?(term)
    end
  end

  set_filtered(matches)

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

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

#tag(params) ⇒ Object



165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/cnote/commands.rb', line 165

def tag(params)
  notes = multi_note(params)

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

  set_filtered(notes)
  print_list("Changed", @filtered)

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

#tags(params) ⇒ Object



193
194
195
196
197
198
199
# File 'lib/cnote/commands.rb', line 193

def tags(params)
  if params.empty?
    list_tags
  else
    puts "NOT YET IMPLEMENTED"
  end
end

#untag(params) ⇒ Object



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

def untag(params)
  notes = multi_note(params)

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

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