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

def initialize(config)
  @config = config
  @notes = Dir[File.join(@config.note_path, "**/*.md")].map { |f| Note.new(f) }
  @filtered = @notes
end

Instance Method Details

#await_command(message = nil) ⇒ Object

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

REPL type thing         #

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



18
19
20
21
22
23
24
25
26
# File 'lib/cnote/notes.rb', line 18

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

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

#config(params = []) ⇒ Object



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

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



97
98
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
148
149
150
151
152
# File 'lib/cnote/notes.rb', line 97

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
    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

      @notes << Note.new(full_path)

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

#delete(params) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/cnote/notes.rb', line 166

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

  if note and File.exists? note.path
    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



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

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 "    - #{"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 "    - #{"config".bold.white} #{"(set/get)".italic} #{"key".italic} [#{"value".italic}]"
  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



303
304
305
306
# File 'lib/cnote/notes.rb', line 303

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

#open(params) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/cnote/notes.rb', line 154

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



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/cnote/notes.rb', line 184

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

  note = @filtered[params.first.to_i - 1]
  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

#run_command(action, params) ⇒ Object



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

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 "config", "conf"
    config(params)
  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           #

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



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

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



207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/cnote/notes.rb', line 207

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



222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/cnote/notes.rb', line 222

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