Class: FuzzyNotes::Notes

Inherits:
Object
  • Object
show all
Includes:
Logger
Defined in:
lib/fuzzy_notes/notes.rb

Constant Summary collapse

VALID_PARAMS =
[:log_level, :color, :editor, :viewer, :note_paths,
:valid_extensions, :custom_extensions, :full_text_search,
:keywords, :evernote_params].freeze

Constants included from Logger::Colors

Logger::Colors::CREATE_COLOR, Logger::Colors::DEFAULT_COLOR, Logger::Colors::DELETE_COLOR, Logger::Colors::EXPORT_COLOR, Logger::Colors::IMPORT_COLOR, Logger::Colors::NOTE_COLOR, Logger::Colors::NUMBER_COLOR, Logger::Colors::PATH_COLOR, Logger::Colors::USER_COLOR

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logger

#log

Constructor Details

#initialize(params = {}) ⇒ Notes

Returns a new instance of Notes.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fuzzy_notes/notes.rb', line 28

def initialize(params = {})
  @params = params
  parse_init_params
  
  init_logger
  init_cipher
  init_finder
  
  @all_notes = @finder.files_matching_extension 
  @matching_notes = @finder.files_matching_all
end

Instance Attribute Details

#all_notesObject (readonly)

Returns the value of attribute all_notes.



26
27
28
# File 'lib/fuzzy_notes/notes.rb', line 26

def all_notes
  @all_notes
end

Instance Method Details

#catObject

dump all matching notes to stdout



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fuzzy_notes/notes.rb', line 42

def cat
  unless encrypted_notes.empty?
    print_notes(:encrypted => true)
    decrypted_notes = @cipher.decrypt_files(encrypted_notes)
  end

  matching_notes.each do |note_path|
    contents = \
      if FuzzyNotes::Cipher.encrypted?(note_path)
        decrypted_notes.shift
      elsif FuzzyNotes::EvernoteSync.evernote?(note_path)
        FuzzyNotes::EvernoteSync.sanitize_evernote(note_path)
      elsif FuzzyNotes::ImageViewer.image?(note_path)
        FuzzyNotes::ImageViewer.display(@viewer, note_path)
      else
        FuzzyNotes::TextViewer.read(note_path)
      end

    if contents
      log.info "=== #{note_path} ===\n\n"
      puts "#{contents}\n"
    end
  end
end

#decryptObject

decrypt matching notes



109
110
111
112
113
114
115
# File 'lib/fuzzy_notes/notes.rb', line 109

def decrypt
  return if encrypted_notes.empty?
  print_notes(:encrypted => true)
  log.indent do
    @cipher.decrypt_files(encrypted_notes, :replace => true)
  end
end

#editObject

edit all matching notes in EDITOR



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/fuzzy_notes/notes.rb', line 69

def edit
  notes_to_edit = \
    unless encrypted_notes.empty?
      print_notes(:encrypted => true)
      decrypted_tempfiles = @cipher.decrypt_to_tempfiles(encrypted_notes)
      successfully_decrypted_files = decrypted_tempfiles.compact 
      plaintext_notes + successfully_decrypted_files
    else plaintext_notes
    end

  # edit decrypted files
  unless notes_to_edit.empty?
    system("#{@editor} #{bashify_note_paths(notes_to_edit)}")
  end

  # reencrypt decrypted notes
  unless encrypted_notes.empty? || successfully_decrypted_files.empty? 
    log.info "#{CREATE_COLOR} re-encrypting edited notes:"
    tempfiles_notes = decrypted_tempfiles.zip(encrypted_notes)
    log.indent do
      tempfiles_notes.each do |(tmpfile, note_path)|
        log.info "#{PATH_COLOR} #{note_path}" if note_path
      end
    end
    log.indent { @cipher.encrypt_from_tempfiles(tempfiles_notes) } 
  end
end

#encryptObject

encrypt matching notes



99
100
101
102
103
104
105
# File 'lib/fuzzy_notes/notes.rb', line 99

def encrypt
  return if plaintext_notes.empty?
  print_notes(:plaintext => true)
  log.indent do 
    @cipher.encrypt_files(plaintext_notes, :replace => true)
  end
end

#evernote_syncObject



128
129
130
131
# File 'lib/fuzzy_notes/notes.rb', line 128

def evernote_sync
  return unless evernote_params_found?
  FuzzyNotes::EvernoteSync.new(@evernote_params).sync
end

#infoObject

view WC info for all/matching notes



119
120
121
122
# File 'lib/fuzzy_notes/notes.rb', line 119

def info
  paths = bashify_note_paths(matching_notes(:all_if_empty => true))
  puts `wc $(find #{paths} -type f)` 
end

#listObject



124
125
126
# File 'lib/fuzzy_notes/notes.rb', line 124

def list
  print_notes(:all_if_empty => true)
end