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
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
# File lib/cnote/notes.rb, line 111 def print_list(title, notes, verbose = false) path_length = @config.note_path.split("/").length count = 0; puts puts "#{indent}#{title}".bold puts "#{indent}#{"-" * title.length}" puts if verbose notes.each do |num, note| count += 1 if !note puts "#{num}.".ljust(@indent) + " DELETED".italic next end puts "#{num}.".ljust(4) + note.title_limit(70).bold puts "#{indent}#{note.path.gsub(@config.note_path, "")}".italic.light_magenta if note.tags.length > 0 tags = note.tags.map { |tag| tag.yellow } puts "#{indent}tags: " + "[#{tags.join('] [')}]" else puts "#{indent}<no tags>" end puts "#{indent}modified: " + note.modified.strftime("%a, %b %e %Y, %l:%M%P").italic puts "#{indent}created: " + note.created.strftime("%a, %b %e %Y, %l:%M%P").italic puts end else notes.each do |num, note| count += 1 if !note puts "#{num}.".ljust(@indent) + "DELETED".colorize(color: :white, background: :red).italic next end print "#{num}.".ljust(@indent) + note.title_limit(70).bold if note.tags.length > 0 tags = note.tags.map { |tag| tag.yellow } print " [#{tags.join('] [')}]" end print "\n" end end puts puts "#{indent}Listed #{count.to_s.bold} Notes" puts end
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