lib/cnote/notes.rb in cnote-0.1.3 vs lib/cnote/notes.rb in cnote-0.2.0

- old
+ new

@@ -1,8 +1,9 @@ require "colorize" require "fileutils" require "time" +require "ap" require "cnote/note" class Notes def initialize(config) @config = config @@ -14,11 +15,11 @@ # REPL type thing # #\================================/# def await_command(message = nil) puts message if message - print "#{@config.cursor} ".magenta + print "#{@config.prompt} ".magenta input = STDIN.gets.chomp # Strip and process action, *params = input.strip.gsub(/\s{2,}/, " ").split(" ") run_command(action || "help", params) @@ -42,10 +43,12 @@ 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 @@ -121,23 +124,30 @@ 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}'" - note = Note.new(full_path) - note.add_tags(tags) if tags.length > 0 - note.created = Time.new - note.update + 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) + @notes << Note.new(full_path) - print_list("Created", [note]) - @filtered = [note] + 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 @@ -170,10 +180,14 @@ puts "Looks like my job is done here, since note #{num} doesn't exist anyway!" end end 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 @@ -218,23 +232,56 @@ @filtered = notes puts "Removed #{params.length - 1} tag#{"s" if params.length != 2} from #{notes.length} note#{"s" if notes.length != 1}." end + 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 + def help puts puts "Enter a command with the structure:" - puts " #{@config.cursor} action parameter(s)" + 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." \ No newline at end of file