class Notes

Public Class Methods

new(config) click to toggle source
# File lib/cnote/notes.rb, line 11
def initialize(config)
  @config = config
  @index = 1
  @notes = Hash.new

  notes = Dir[File.join(@config.note_path, "**/*")].select do |f|
    [".txt", ".md"].include?(File.extname(f))
  end

  notes.each do |path|
    note = Note.new(path)
    note.index = @index

    @notes[@index] = note

    @index += 1
  end

  @indent = notes.length.to_s.length + 2

  set_filtered(@notes)
end

Public Instance Methods

await_command(message = nil) click to toggle source

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

REPL type thing         #

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

# File lib/cnote/notes.rb, line 38
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
run_command(action, params) click to toggle source
# File lib/cnote/notes.rb, line 48
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 "tags"
    tags(params)
  when "untag", "ut"
    untag(params)
  when "search", "find", "s", "f"
    search(params.join(" "))
  when "list", "l", "ls"
    list
  when "info", "i"
    info(params)
  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

Private Instance Methods

confirm(message = "Confirm") click to toggle source
# File lib/cnote/notes.rb, line 164
        def confirm(message = "Confirm")
  print "#{message} [y/n]: "
  case gets&.chomp&.strip&.downcase
  when "y", "yes", "yeah", "sure", "yep", "okay", "aye"
    return true
  when "n", "no", "nope", "nay"
    return false
  else
    return confirm("Sorry, didn't quite get that...")
  end
end
does_not_have_tags(note, tags) click to toggle source
# File lib/cnote/notes.rb, line 209
        def does_not_have_tags(note, tags)
  doesnt_have = true
  note_tags = note.tags
  tags.each do |tag|
    if note_tags.include? tag
      doesnt_have = false
      break
    end
  end
  doesnt_have
end
has_tags(note, tags) click to toggle source
# File lib/cnote/notes.rb, line 197
        def has_tags(note, tags)
  has = true
  note_tags = note.tags
  tags.each do |tag|
    if !note_tags.include? tag
      has = false
      break
    end
  end
  has
end
indent() click to toggle source

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

Utilities            #

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

# File lib/cnote/notes.rb, line 88
        def indent
  " " * @indent
end
multi_note(params) click to toggle source
# File lib/cnote/notes.rb, line 176
        def multi_note(params)
  notes = []

  params.first.split(",").each do |num|
    note = @notes[num.to_i]
    if note
      notes << note
    else
      puts "Note #{num} doesn't exist!"
    end
  end

  notes
end
print_list(title, notes, verbose = false) click to toggle source
recently_edited_first(notes) click to toggle source
# File lib/cnote/notes.rb, line 191
        def recently_edited_first(notes)
  ap notes

  notes.sort_by { |note| note.modified }.reverse
end
set_filtered(notes) click to toggle source
# File lib/cnote/notes.rb, line 92
        def set_filtered(notes)
  @filtered = Hash.new

  case notes.class.to_s
  when "Array"
    notes.each do |note|
      @filtered[note.index] = note
    end
  when "Hash"
    notes.each do |num, note|
      @filtered[num] = note
    end
  else
    puts "Unrecognized notes format for set_filtered. Got #{notes.class}!"
  end
  
  @filtered
end